pyAgrum on notebooks
☰  graphicalInference

# 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:

In [1]:
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.

In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
gnb.showBN(bn,size="9")


From there, it is easy to get a posterior using an inference engine :

In [3]:
ie=gum.LazyPropagation(bn)
ie.makeInference()
print(ie.posterior(bn.idFromName("CATECHOL")))

<CATECHOL:NORMAL> :: 0.0511754 /<CATECHOL:HIGH> :: 0.948825


But since we are in notebook, why not use pyAgrum notebook's methods ?

In [4]:
gnb.showPosterior(bn,evs={},target='CATECHOL')


You may also want to see the graph with some posteriors

In [5]:
# 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 :

In [6]:
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).

In [7]:
gnb.showInference(bn,evs={"CO":1,"VENTLUNG":1},size="16")