Created
January 10, 2019 15:03
-
-
Save bearloga/2456e3893ea047afdebb92cbb246fa4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This is an R port of the [pomegranate](https://pomegranate.readthedocs.io/en/latest/)-based example of a [Bayesian network](https://en.wikipedia.org/wiki/Bayesian_network), using [reticulate](https://rstudio.github.io/reticulate/index.html).\n", | |
"\n", | |
"The [wet pavement](https://gist.github.com/bearloga/cded2130fddd673a4d034b6b1e20100c) model:\n", | |
"\n", | |
"![](pavement.png)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# My config:\n", | |
"Sys.setenv(\"RETICULATE_PYTHON\" = \"~/envs/probabilistic/bin/python\")\n", | |
"reticulate::use_virtualenv(\"~/envs/probabilistic\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"library(reticulate)\n", | |
"\n", | |
"pg <- import(\"pomegranate\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"raining <- pg$DiscreteDistribution(dict(\"Yes\" = 0.25, \"No\" = 0.75))\n", | |
"\n", | |
"sprinkler <- pg$ConditionalProbabilityTable(\n", | |
" list(\n", | |
" list(\"Yes\", \"On\", 0.001),\n", | |
" list(\"Yes\", \"Off\", 0.999),\n", | |
" list(\"No\", \"On\", 0.8),\n", | |
" list(\"No\", \"Off\", 0.2)\n", | |
" ),\n", | |
" list(raining)\n", | |
")\n", | |
"\n", | |
"pavement <- pg$ConditionalProbabilityTable(\n", | |
" list(\n", | |
" list(\"Yes\", \"On\", \"Wet\", 1.0),\n", | |
" list(\"Yes\", \"On\", \"Dry\", 0.0),\n", | |
" list(\"Yes\", \"Off\", \"Wet\", 1.0),\n", | |
" list(\"Yes\", \"Off\", \"Dry\", 0.0),\n", | |
" list(\"No\", \"On\", \"Wet\", 0.75),\n", | |
" list(\"No\", \"On\", \"Dry\", 0.25),\n", | |
" list(\"No\", \"Off\", \"Wet\", 0.01),\n", | |
" list(\"No\", \"Off\", \"Dry\", 0.99)\n", | |
" ),\n", | |
" list(raining, sprinkler)\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"s1 <- pg$Node(raining, name = \"raining\")\n", | |
"s2 <- pg$Node(sprinkler, name = \"sprinkler\")\n", | |
"s3 <- pg$Node(pavement, name = \"pavement\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model = pg$BayesianNetwork(\"wet pavement model\")\n", | |
"model$add_states(s1, s2, s3)\n", | |
"model$add_edge(s1, s2)\n", | |
"model$add_edge(s1, s3)\n", | |
"model$add_edge(s2, s3)\n", | |
"model$bake()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[1]]\n", | |
"[1] \"No\"\n", | |
"\n", | |
"[[2]]\n", | |
"{\n", | |
" \"class\" :\"Distribution\",\n", | |
" \"dtype\" :\"str\",\n", | |
" \"name\" :\"DiscreteDistribution\",\n", | |
" \"parameters\" :[\n", | |
" {\n", | |
" \"On\" :0.9966777408637874,\n", | |
" \"Off\" :0.0033222591362126854\n", | |
" }\n", | |
" ],\n", | |
" \"frozen\" :false\n", | |
"}\n", | |
"\n", | |
"[[3]]\n", | |
"[1] \"Wet\"\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"probabilities <- model$predict_proba(list(\"No\", NULL, \"Wet\"))\n", | |
"probabilities" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$On</dt>\n", | |
"\t\t<dd>0.996677740863787</dd>\n", | |
"\t<dt>$Off</dt>\n", | |
"\t\t<dd>0.00332225913621269</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$On] 0.996677740863787\n", | |
"\\item[\\$Off] 0.00332225913621269\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$On\n", | |
": 0.996677740863787\n", | |
"$Off\n", | |
": 0.00332225913621269\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$On\n", | |
"[1] 0.9966777\n", | |
"\n", | |
"$Off\n", | |
"[1] 0.003322259\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"probabilities[[2]]$parameters[[1]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "ir" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment