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

Skip to content

Commit 5bdcfb3

Browse files
Added section on Bayesian Networks
1 parent 5c0f23c commit 5bdcfb3

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

images/bayesnet.png

79.1 KB
Loading

probability.ipynb

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,190 @@
425425
"source": [
426426
"You can verify that the first value is the same as we obtained earlier by manual calculation."
427427
]
428+
},
429+
{
430+
"cell_type": "markdown",
431+
"metadata": {},
432+
"source": [
433+
"## Bayesian Networks\n",
434+
"\n",
435+
"A Bayesian network is a representation of the joint probability distribution encoding a collection of conditional independence statements.\n",
436+
"\n",
437+
"A Bayes Network is implemented as the class **BayesNet**. It consisits of a collection of nodes implemented by the class **BayesNode**. The implementation in the above mentioned classes focuses only on boolean variables. Each node is associated with a variable and it contains a **conditional probabilty table (cpt)**. The **cpt** represents the probability distribution of the variable conditioned on its parents **P(X | parents)**.\n",
438+
"\n",
439+
"Let us dive into the **BayesNode** implementation."
440+
]
441+
},
442+
{
443+
"cell_type": "code",
444+
"execution_count": null,
445+
"metadata": {
446+
"collapsed": false
447+
},
448+
"outputs": [],
449+
"source": [
450+
"%psource BayesNode"
451+
]
452+
},
453+
{
454+
"cell_type": "markdown",
455+
"metadata": {},
456+
"source": [
457+
"The constructor takes in the name of **variable**, **parents** and **cpt**. Here **variable** is a the name of the variable like 'Earthquake'. **parents** should a list or space separate string with variable names of parents. The conditional probability table is a dict {(v1, v2, ...): p, ...}, the distribution P(X=true | parent1=v1, parent2=v2, ...) = p. Here the keys are combination of boolean values that the parents take. The length and order of the values in keys should be same as the supplied **parent** list/string. In all cases the probability of X being false is left implicit, since it follows from P(X=true).\n",
458+
"\n",
459+
"The example below where we implement the network shown in **Figure 14.3** of the book will make this more clear.\n",
460+
"\n",
461+
"<img src=\"files/images/bayesnet.png\">\n",
462+
"\n",
463+
"The alarm node can be made as follows: "
464+
]
465+
},
466+
{
467+
"cell_type": "code",
468+
"execution_count": null,
469+
"metadata": {
470+
"collapsed": true
471+
},
472+
"outputs": [],
473+
"source": [
474+
"alarm_node = BayesNode('Alarm', ['Burglary', 'Earthquake'], \n",
475+
" {(True, True): 0.95,(True, False): 0.94, (False, True): 0.29, (False, False): 0.001})"
476+
]
477+
},
478+
{
479+
"cell_type": "markdown",
480+
"metadata": {},
481+
"source": [
482+
"It is possible to avoid using a tuple when there is only a single parent. So an alternative format for the **cpt** is"
483+
]
484+
},
485+
{
486+
"cell_type": "code",
487+
"execution_count": null,
488+
"metadata": {
489+
"collapsed": true
490+
},
491+
"outputs": [],
492+
"source": [
493+
"john_node = BayesNode('JohnCalls', ['Alarm'], {True: 0.90, False: 0.05})\n",
494+
"mary_node = BayesNode('MaryCalls', 'Alarm', {(True, ): 0.70, (False, ): 0.01}) # Using string for parents.\n",
495+
"# Equvivalant to john_node definition. "
496+
]
497+
},
498+
{
499+
"cell_type": "markdown",
500+
"metadata": {},
501+
"source": [
502+
"The general format used for the alarm node always holds. For nodes with no parents we can also use. "
503+
]
504+
},
505+
{
506+
"cell_type": "code",
507+
"execution_count": null,
508+
"metadata": {
509+
"collapsed": true
510+
},
511+
"outputs": [],
512+
"source": [
513+
"burglary_node = BayesNode('Burglary', '', 0.001)\n",
514+
"earthquake_node = BayesNode('Earthquake', '', 0.002)"
515+
]
516+
},
517+
{
518+
"cell_type": "markdown",
519+
"metadata": {},
520+
"source": [
521+
"It is possible to use the node for lookup function using the **p** method. The method takes in two arguments **value** and **event**. Event must be a dict of the type {variable:values, ..} The value corresponds to the value of the variable we are interested in (False or True).The method returns the conditional probability **P(X=value | parents=parent_values)**, where parent_values are the values of parents in event. (event must assign each parent a value.)"
522+
]
523+
},
524+
{
525+
"cell_type": "code",
526+
"execution_count": null,
527+
"metadata": {
528+
"collapsed": false
529+
},
530+
"outputs": [],
531+
"source": [
532+
"john_node.p(False, {'Alarm': True, 'Burglary': True}) # P(JohnCalls=False | Alarm=True)"
533+
]
534+
},
535+
{
536+
"cell_type": "markdown",
537+
"metadata": {},
538+
"source": [
539+
"With all the information about nodes present it is possible to construct a Bayes Network using **BayesNet**. The **BayesNet** class does not take in nodes as input but instead takes a list of **node_specs**. An entry in **node_specs** is a tuple of the parameters we use to construct a **BayesNode** namely **(X, parents, cpt)**. **node_specs** must be ordered with parents before children."
540+
]
541+
},
542+
{
543+
"cell_type": "code",
544+
"execution_count": null,
545+
"metadata": {
546+
"collapsed": true
547+
},
548+
"outputs": [],
549+
"source": [
550+
"%psource BayesNet"
551+
]
552+
},
553+
{
554+
"cell_type": "markdown",
555+
"metadata": {},
556+
"source": [
557+
"The constructor of **BayesNet** takes each item in **node_specs** and adds a **BayesNode** to its **nodes** object variable by calling the **add** method. **add** in turn adds node to the net. Its parents must already be in the net, and its variable must not. Thus add allows us to grow a **BayesNet** given its parents are already present.\n",
558+
"\n",
559+
"**burglary** global is an instance of **BayesNet** corresponding to the above example.\n",
560+
"\n",
561+
" T, F = True, False\n",
562+
"\n",
563+
" burglary = BayesNet([\n",
564+
" ('Burglary', '', 0.001),\n",
565+
" ('Earthquake', '', 0.002),\n",
566+
" ('Alarm', 'Burglary Earthquake',\n",
567+
" {(T, T): 0.95, (T, F): 0.94, (F, T): 0.29, (F, F): 0.001}),\n",
568+
" ('JohnCalls', 'Alarm', {T: 0.90, F: 0.05}),\n",
569+
" ('MaryCalls', 'Alarm', {T: 0.70, F: 0.01})\n",
570+
" ])"
571+
]
572+
},
573+
{
574+
"cell_type": "code",
575+
"execution_count": null,
576+
"metadata": {
577+
"collapsed": false
578+
},
579+
"outputs": [],
580+
"source": [
581+
"burglary"
582+
]
583+
},
584+
{
585+
"cell_type": "markdown",
586+
"metadata": {},
587+
"source": [
588+
"**BayesNet** method **variable_node** allows to reach **BayesNode** instances inside a Bayes Net. It is possible to modify the **cpt** of the nodes directly using this method."
589+
]
590+
},
591+
{
592+
"cell_type": "code",
593+
"execution_count": null,
594+
"metadata": {
595+
"collapsed": false
596+
},
597+
"outputs": [],
598+
"source": [
599+
"type(burglary.variable_node('Alarm'))"
600+
]
601+
},
602+
{
603+
"cell_type": "code",
604+
"execution_count": null,
605+
"metadata": {
606+
"collapsed": false
607+
},
608+
"outputs": [],
609+
"source": [
610+
"burglary.variable_node('Alarm').cpt"
611+
]
428612
}
429613
],
430614
"metadata": {

0 commit comments

Comments
 (0)