Asthma

Creative Commons License

aGrUM

interactive online version

In [1]:
import matplotlib.pyplot as plt

# import the computation tools of aGrUM
import pyAgrum as gum

# import the graphical display functions
import pyAgrum.lib.notebook as gnb

The model

In [2]:
# load the "asthma" Bayesian network
bn=gum.loadBN('res/asthma.bif')
In [3]:
# display the Bayesian network
gnb.showBN(bn,nodeColor={n:0.9 for n in bn.names()},cmapNode=plt.get_cmap('Blues'))
../_images/notebooks_11-Examples_Asthma_5_0.svg
In [4]:
# display the conditional probability table of asthme given pollution
gnb.showPotential(bn.cpt(bn.idFromName('asthma')),digits=4)
asthma
pollution
crisis
no_crisis
1
0.10000.9000
2
0.21130.7887
3
0.19720.8028
4
0.28240.7176
5
0.47460.5254
6
0.52300.4770
7
0.63410.3659
8
0.82570.1743
9
0.85370.1463
10
0.90910.0909

Some inference

In [5]:
# display the probability distribution of Variable "trafic"
gnb.showPosterior (bn, {}, "traffic" )
../_images/notebooks_11-Examples_Asthma_8_0.svg
In [6]:
# display the probability distribution of Variable "pollution"
gnb.showPosterior ( bn, {}, "pollution")
../_images/notebooks_11-Examples_Asthma_9_0.svg
In [7]:
# display the distribution somewhat differently
gum.getPosterior ( bn, target="pollution")
Out[7]:
pollution
1
2
3
4
5
6
7
8
9
10
0.00000.03550.42330.27880.14750.06290.01640.02190.00820.0055
In [8]:
# more interesting: display the posterior distribution of "asthme"
# given that we observed that time is 8:00 and weather is cloudy
gnb.showPosterior (bn, evs={'hour' : 8, 'weather' : 'cloudy'}, target="asthma" )
../_images/notebooks_11-Examples_Asthma_11_0.svg
In [9]:
# show the complete model on morning (from 8 to 12)
gnb.showInference(bn,evs={'hour' : [0 if i< 8 or i> 12 else 1 for i in range(24)]})
../_images/notebooks_11-Examples_Asthma_12_0.svg
In [10]:
# show the posterior distributions of all the variables given that
# we observed that heure=8 and meteo=nuageux.
# the tables in beige represent the observations
gnb.flow.row(gnb.getInference(bn,size="9",evs={'hour' : 8, 'weather' : 'cloudy'}),
               gnb.getInference(bn,size="9",evs={'hour': 7, 'accident' : 'yes'}),
              captions=["When time is 8:00 and weatcher is cloudy","When time is 7:00 and there hase been an accident"])
structs Inference in   1.80ms hour 2024-06-07T16:26:41.685702 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ traffic 2024-06-07T16:26:41.812131 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ hour->traffic weather 2024-06-07T16:26:41.718218 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ accident 2024-06-07T16:26:41.761692 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ weather->accident accident->traffic pollution 2024-06-07T16:26:41.858193 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ traffic->pollution asthma 2024-06-07T16:26:41.887319 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ pollution->asthma
When time is 8:00 and weatcher is cloudy
structs Inference in   1.22ms hour 2024-06-07T16:26:42.152452 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ traffic 2024-06-07T16:26:42.220449 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ hour->traffic weather 2024-06-07T16:26:42.174135 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ accident 2024-06-07T16:26:42.193015 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ weather->accident accident->traffic pollution 2024-06-07T16:26:42.255275 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ traffic->pollution asthma 2024-06-07T16:26:42.281713 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ pollution->asthma
When time is 7:00 and there hase been an accident