Probablistic Inference with pyAgrum¶
In this notebook, we will show different basic features for probabilistic inference on Bayesian networks using pyAgrum.
First we need some external modules:
import os
%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
Basic inference and display¶
Then we import pyAgrum and the pyAgrum's notebook module, that offers very usefull methods when writting a notebook.
This first example shows how you can load a BayesNet and show it as graph. Note that pyAgrum handles serveral BayesNet file format such as DSL, BIF and UAI.
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
bn=gum.loadBN("res/alarm.dsl")
gnb.showBN(bn,size="9")
print(bn)
BN{nodes: 37, arcs: 46, domainSize: 10^16.2389, dim: 509, mem: 5Ko 896o}
From there, it is easy to get a posterior using an inference engine :
ie=gum.LazyPropagation(bn)
ie.makeInference()
print(ie.posterior(bn.idFromName("CATECHOL")))
CATECHOL | NORMAL |HIGH | ---------|---------| 0.0512 | 0.9488 |
But since we are in notebook, why not use pyAgrum notebook's methods ?
gnb.showPosterior(bn,evs={},target='CATECHOL')
You may also want to see the graph with some posteriors
# due to matplotlib, format is forced to png.
gnb.showInference(bn,evs={},targets={"VENTALV","CATECHOL","HR","MINVOLSET"},size="11")
.. and then observe the impact of evidence :
gnb.showInference(bn,
evs={"CO":1,"VENTLUNG":1},
targets={"VENTALV",
"CATECHOL",
"HR",
"MINVOLSET",
"ANAPHYLAXIS",
"STROKEVOLUME",
"ERRLOWOUTPUT",
"HBR",
"PULMEMBOLUS",
"HISTORY",
"BP",
"PRESS",
"CO"},
size="10")
You can even compute all posteriors by leaving the targets
parameter empty (which is its default value).
gnb.showInference(bn,evs={"CO":1,"VENTLUNG":1},size="14")