Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a121360

Browse files
BayesNet Enumeration
1 parent 5bdcfb3 commit a121360

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

probability.ipynb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,75 @@
609609
"source": [
610610
"burglary.variable_node('Alarm').cpt"
611611
]
612+
},
613+
{
614+
"cell_type": "markdown",
615+
"metadata": {},
616+
"source": [
617+
"## Exact Inference in Bayesian Networks\n",
618+
"\n",
619+
"A Bayes Network is a more compact representation of the full joint distribution and like full joint distributions allows us to do inference i.e. answer questions about probability distributions of random variables given some evidence.\n",
620+
"\n",
621+
"Exact algorithms don't scale well for larger networks. Approximate algorithms are explained in the next section.\n",
622+
"\n",
623+
"### Inference by Enumeration\n",
624+
"\n",
625+
"We apply techniques similar to those used for **enumerate_joint_ask** and **enumerate_joint** to draw inference from Bayesian Networks. **enumeration_ask** and **enumerate_all** implement the algorithm described in **Figure 14.9** of the book."
626+
]
627+
},
628+
{
629+
"cell_type": "code",
630+
"execution_count": null,
631+
"metadata": {
632+
"collapsed": true
633+
},
634+
"outputs": [],
635+
"source": [
636+
"%psource enumerate_all"
637+
]
638+
},
639+
{
640+
"cell_type": "markdown",
641+
"metadata": {},
642+
"source": [
643+
"**enumerate__all** recursively evaluates a general form of the **Equation 14.4** in the book.\n",
644+
"\n",
645+
"$$\\textbf{P}(X | \\textbf{e}) = α \\textbf{P}(X, \\textbf{e}) = α \\sum_{y} \\textbf{P}(X, \\textbf{e}, \\textbf{y})$$ \n",
646+
"\n",
647+
"such that **P(X, e, y)** is written in the form of product of conditional probabilities **P(variable | parents(variable))** from the Bayesian Network.\n",
648+
"\n",
649+
"**enumeration_ask** calls **enumerate_all** on each value of query variable **X** and finally normalizes them. \n"
650+
]
651+
},
652+
{
653+
"cell_type": "code",
654+
"execution_count": null,
655+
"metadata": {
656+
"collapsed": true
657+
},
658+
"outputs": [],
659+
"source": [
660+
"%psource enumeration_ask"
661+
]
662+
},
663+
{
664+
"cell_type": "markdown",
665+
"metadata": {},
666+
"source": [
667+
"Let us solve the problem of finding out **P(Burglary=True | JohnCalls=True, MaryCalls=True)** using the **burglary** network.**enumeration_ask** takes three arguments **X** = variable name, **e** = Evidence (in form a dict like previously explained), **bn** = The Bayes Net to do inference on."
668+
]
669+
},
670+
{
671+
"cell_type": "code",
672+
"execution_count": null,
673+
"metadata": {
674+
"collapsed": false
675+
},
676+
"outputs": [],
677+
"source": [
678+
"ans_dist = enumeration_ask('Burglary', {'JohnCalls': True, 'MaryCalls': True}, burglary)\n",
679+
"ans_dist[True]"
680+
]
612681
}
613682
],
614683
"metadata": {

0 commit comments

Comments
 (0)