Bayesian Graphical Models Example
Overview
The present example is derived from an example given in
Russell, Norvig chpt 14.
In the example, there are two individuals living in a house, John and Mary. Over a given period, there is a probability that the house will
be burglarized. The house has an alarm. If the house is burglarized, then there is given probability that the alarm will go off.
However, over the given period, there is also a probability that an Earthquake will occur. When an Earthquake occurs, there is a chance that
it will trip the alarm.
There is also a possibility that the alarm is tripped if neither a burglary or an Earthquake happens.
When the alarm is tripped, there is possibility that John will call 911. Likewise, there is a possibility that
Mary will call 911. There is also the possibility that either will call 911 even when the alarm hasnt been tripped.
Probabilities
We assume that we have the following probabilities.
{% Prob \, Burlgary = 0.001 %}
{% Prob \, Earthquake = 0.002 %}
The probabilities of the alarm going off are given below. Note that the probabilities are only stated for the events which can cause the
alarm.
Burglarly |
Earthquake |
Prob of Alarm |
True |
True |
0.95 |
True |
False |
0.94 |
False |
True |
0.29 |
False |
False |
0.001 |
Alarm |
Prob John Calls |
True |
0.90 |
False |
0.05 |
Alarm |
Prob Mary Calls |
True |
0.70 |
False |
0.01 |
Encoding Probabilities
The above probabilities are encoded as Javascript objects as follows.
let burglary = {
name:'burglary',
table:[{burglary:true, probability:0.001},{burglary:false, probability:0.999},]
//table:[{burglary:true, probability:0.1},{burglary:false, probability:0.90},]
};
let earthquake = {
name:'earthquake',
table:[{earthquake:true, probability:0.002},{earthquake:false, probability:0.998},]
};
let alarm = {
name:'alarm',
table:[
{alarm:true, earthquake:true, burglary:true, probability:0.95},
{alarm:true, earthquake:false, burglary:true, probability:0.94},
{alarm:true, earthquake:true, burglary:false, probability:0.29},
{alarm:true, earthquake:false, burglary:false, probability:0.001},
{alarm:false, earthquake:true, burglary:true, probability:0.05},
{alarm:false, earthquake:false, burglary:true, probability:0.06},
{alarm:false, earthquake:true, burglary:false, probability:0.71},
{alarm:false, earthquake:false, burglary:false, probability:0.999},
]
};
let john = {
name:'john',
table:[
{john:true, alarm:true, probability:0.9},
{john:false, alarm:true, probability:0.1},
{john:true, alarm:false, probability:0.05},
{john:false, alarm:false, probability:0.95},
]
};
let mary = {
name:'mary',
table:[
{mary:true, alarm:true, probability:0.7},
{mary:false, alarm:true, probability:0.3},
{mary:true, alarm:false, probability:0.1},
{mary:false, alarm:false, probability:0.9},
]
};
Calculating Probabilities
dag.conditional(list, values, conditional);
The following calculates the probability that the alarm goes off conditional on a burglary happening. Notice that the answer will
be higher than 0.94 due to the alarm going off at a higher rate when both an earthquake and a burglarly happens.
let dag = await import('/lib/statistics/graphical-models/v1.0.0/dag.mjs');
let list = [burglary, earthquake, alarm, mary, john];
let conditional = dag.conditional(list, {alarm:true},{burglary:true});
Try it!
The following calculates the probability that the alarm goes off conditional on a burglary happening and an earthquake happening.
let dag = await import('/lib/statistics/graphical-models/v1.0.0/dag.mjs');
let list = [burglary, earthquake, alarm, mary, john];
let conditional = dag.conditional(list, {alarm:true},{burglary:true, earthquake:true});
Simulating