Credal Networks

Creative Commons License

aGrUM

interactive online version

In [1]:
import os

%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt

In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
gnb.configuration()
LibraryVersion
OSposix [linux]
Python3.10.10 (main, Mar 5 2023, 22:26:53) [GCC 12.2.1 20230201]
IPython8.13.2
Matplotlib3.7.1
Numpy1.24.3
pyDot1.4.2
pyAgrum1.8.1
Wed May 24 14:52:21 2023 CEST

Credal Net from BN

In [3]:
bn=gum.fastBN("A->B[3]->C<-D<-A->E->F")
bn_min=gum.BayesNet(bn)
bn_max=gum.BayesNet(bn)
for n in bn.nodes():
  x=0.4*min(bn.cpt(n).min(),1-bn.cpt(n).max())
  bn_min.cpt(n).translate(-x)
  bn_max.cpt(n).translate(x)

cn=gum.CredalNet(bn_min,bn_max)
cn.intervalToCredal()

gnb.flow.row(bn,bn.cpt("B"),cn,bn_min.cpt("B"),bn_max.cpt("B"),captions=["Bayes Net","CPT","Credal Net","CPTmin","CPTmax"])

G E E F F E->F D D C C D->C A A A->E A->D B B A->B B->C
Bayes Net
B
A
0
1
2
0
0.62120.05180.3270
1
0.47000.28080.2492

CPT
G E E F F E->F D D C C D->C A A A->E A->D B B A->B B->C
Credal Net
B
A
0
1
2
0
0.60040.03110.3062
1
0.44930.26000.2284

CPTmin
B
A
0
1
2
0
0.64190.07260.3477
1
0.49080.30150.2699

CPTmax

We can use LBP on CN (L2U) only for binary credal networks (here B is not binary). We then propose the classical binarization (but warn the user that this leads to approximation in the inference)

In [4]:
cn2=gum.CredalNet(bn_min,bn_max)
cn2.intervalToCredal()
cn2.approximatedBinarization()
cn2.computeBinaryCPTMinMax()

gnb.flow.row(cn,cn2,captions=["Credal net","Binarized credal net"])
G E E F F E->F D D C C D->C A A A->E A->D B B A->B B->C
Credal net
G B-b1 B-b1 B-v0 B-v0 B-b1->B-v0 B-v1 B-v1 B-b1->B-v1 C C B-b1->C B-v2 B-v2 B-b1->B-v2 E E F F E->F D D D->C B-b0 B-b0 B-b0->B-b1 B-b0->B-v0 B-b0->B-v1 B-b0->C B-b0->B-v2 A A A->B-b1 A->E A->D A->B-b0
Binarized credal net

Here, \(B\) becomes - \(B\)-b\(i\) : the \(i\)-th bit of B - instrumental \(B\)-v\(k\) : the indicator variable for each modality \(k\) of \(B\)

In [5]:
ie_mc=gum.CNMonteCarloSampling(cn)
ie2_lbp=gum.CNLoopyPropagation(cn2)
ie2_mc=gum.CNMonteCarloSampling(cn2)
In [6]:
gnb.sideBySide(gnb.getInference(cn,engine=ie_mc),
               gnb.getInference(cn2,engine=ie2_mc),
               gnb.getInference(cn2,engine=ie2_lbp))
structs Inference in  29.05ms A 2023-05-24T14:52:22.323849 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B 2023-05-24T14:52:22.384295 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->B D 2023-05-24T14:52:22.492406 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->D E 2023-05-24T14:52:22.546250 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->E C 2023-05-24T14:52:22.439848 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B->C D->C F 2023-05-24T14:52:22.600973 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ E->F
structs Inference in  30.65ms A 2023-05-24T14:52:22.793924 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0 2023-05-24T14:52:22.847487 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->B-b0 B-b1 2023-05-24T14:52:22.900844 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->B-b1 D 2023-05-24T14:52:23.007600 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->D E 2023-05-24T14:52:23.060831 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->E B-b0->B-b1 C 2023-05-24T14:52:22.953238 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->C B-v0 2023-05-24T14:52:23.234351 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v0 B-v1 2023-05-24T14:52:23.288293 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v1 B-v2 2023-05-24T14:52:23.342974 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v2 B-b1->C B-b1->B-v0 B-b1->B-v1 B-b1->B-v2 D->C F 2023-05-24T14:52:23.115542 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ E->F
structs Inference in   0.27ms A 2023-05-24T14:52:23.601372 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0 2023-05-24T14:52:23.653305 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->B-b0 B-b1 2023-05-24T14:52:23.705893 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->B-b1 D 2023-05-24T14:52:23.877139 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->D E 2023-05-24T14:52:23.927719 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ A->E B-b0->B-b1 C 2023-05-24T14:52:23.826209 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->C B-v0 2023-05-24T14:52:24.029066 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v0 B-v1 2023-05-24T14:52:24.081280 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v1 B-v2 2023-05-24T14:52:24.132431 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ B-b0->B-v2 B-b1->C B-b1->B-v0 B-b1->B-v1 B-b1->B-v2 D->C F 2023-05-24T14:52:23.978586 image/svg+xml Matplotlib v3.7.1, https://matplotlib.org/ E->F
In [7]:
gnb.sideBySide(ie_mc.CN(),ie_mc.marginalMin("F"),ie_mc.marginalMax("F"),
               ie_mc.CN(),ie2_lbp.marginalMin("F"),ie2_lbp.marginalMax("F"),
              ncols=3)
print(cn)
G E E F F E->F D D C C D->C A A A->E A->D B B A->B B->C
F
0
1
0.15140.6467
F
0
1
0.35330.8486
G E E F F E->F D D C C D->C A A A->E A->D B B A->B B->C
F
0
1
0.15140.6467
F
0
1
0.35330.8486

A:Range([0,1])
<> : [[0.510773 , 0.489227] , [0.790333 , 0.209667]]

B:Range([0,2])
<A:0> : [[0.600444 , 0.0518454 , 0.347711] , [0.600444 , 0.0725846 , 0.326971] , [0.621182 , 0.0725846 , 0.306233] , [0.641921 , 0.0518455 , 0.306233] , [0.621182 , 0.031107 , 0.347711] , [0.641921 , 0.031107 , 0.326972]]
<A:1> : [[0.449309 , 0.280784 , 0.269907] , [0.449309 , 0.301523 , 0.249168] , [0.470046 , 0.301523 , 0.228431] , [0.490786 , 0.280783 , 0.228431] , [0.470046 , 0.260047 , 0.269907] , [0.490786 , 0.260047 , 0.249167]]

C:Range([0,1])
<B:0|D:0> : [[0.43335 , 0.56665] , [0.579348 , 0.420652]]
<B:1|D:0> : [[0.744503 , 0.255497] , [0.890501 , 0.109499]]
<B:2|D:0> : [[0.687504 , 0.312496] , [0.833503 , 0.166497]]
<B:0|D:1> : [[0.453807 , 0.546193] , [0.599804 , 0.400196]]
<B:1|D:1> : [[0.603533 , 0.396467] , [0.749531 , 0.250469]]
<B:2|D:1> : [[0.202389 , 0.797611] , [0.348387 , 0.651613]]

D:Range([0,1])
<A:0> : [[0.720952 , 0.279048] , [0.792616 , 0.207384]]
<A:1> : [[0.0537482 , 0.946252] , [0.125413 , 0.874587]]

E:Range([0,1])
<A:0> : [[0.0782698 , 0.92173] , [0.18263 , 0.81737]]
<A:1> : [[0.648741 , 0.351259] , [0.7531 , 0.2469]]

F:Range([0,1])
<E:0> : [[0.647705 , 0.352295] , [0.686324 , 0.313676]]
<E:1> : [[0.0289655 , 0.971034] , [0.0675862 , 0.932414]]


Credal Net from bif files

In [8]:
cn=gum.CredalNet("res/cn/2Umin.bif","res/cn/2Umax.bif")
cn.intervalToCredal()
In [9]:
gnb.showCN(cn,"2")
../_images/notebooks_24-Models_credalNetworks_14_0.svg
In [10]:
ie=gum.CNMonteCarloSampling(cn)
ie.insertEvidenceFile("res/cn/L2U.evi")
In [11]:
ie.setRepetitiveInd(False)
ie.setMaxTime(1)
ie.setMaxIter(1000)

ie.makeInference()
In [12]:
cn
Out[12]:
G H H L L H->L E E E->H D D G G D->G F F D->F A A A->E C C C->F F->H B B B->E
In [13]:
gnb.showInference(cn,targets={"A","H","L","D"},engine=ie,evs={"L":[0,1],"G":[1,0]})
../_images/notebooks_24-Models_credalNetworks_18_0.svg

Comparing inference in credal networks

In [14]:
import pyAgrum as gum

def showDiffInference(model,mc,lbp):
    for i in model.current_bn().nodes():
        a,b=mc.marginalMin(i)[:]
        c,d=mc.marginalMax(i)[:]

        e,f=lbp.marginalMin(i)[:]
        g,h=lbp.marginalMax(i)[:]

        plt.scatter([a,b,c,d],[e,f,g,h])


cn=gum.CredalNet("res/cn/2Umin.bif","res/cn/2Umax.bif")
cn.intervalToCredal()

The two inference give quite the same result

In [15]:
ie_mc=gum.CNMonteCarloSampling(cn)
ie_mc.makeInference()

cn.computeBinaryCPTMinMax()
ie_lbp=gum.CNLoopyPropagation(cn)
ie_lbp.makeInference()

showDiffInference(cn,ie_mc,ie_lbp)
../_images/notebooks_24-Models_credalNetworks_22_0.svg

but not when evidence are inserted

In [16]:
ie_mc=gum.CNMonteCarloSampling(cn)
ie_mc.insertEvidenceFile("res/cn/L2U.evi")
ie_mc.makeInference()

ie_lbp=gum.CNLoopyPropagation(cn)
ie_lbp.insertEvidenceFile("res/cn/L2U.evi")
ie_lbp.makeInference()

showDiffInference(cn,ie_mc,ie_lbp)

../_images/notebooks_24-Models_credalNetworks_24_0.svg

Dynamical Credal Net

In [17]:
cn=gum.CredalNet("res/cn/bn_c_8.bif","res/cn/den_c_8.bif")
cn.bnToCredal(0.8,False)
In [18]:
ie=gum.CNMonteCarloSampling(cn)
ie.insertModalsFile("res/cn/modalities.modal")

ie.setRepetitiveInd(True)
ie.setMaxTime(5)
ie.setMaxIter(1000)

ie.makeInference()
In [19]:
print(ie.dynamicExpMax("temp"))
(13.796595328550666, 11.864395265918771, 12.015629955123197, 12.007631081945778, 11.965797424106215, 11.964926735931694, 11.965025343870192, 11.965014533608835, 11.965015735850175)
In [20]:
fig=figure()
ax=fig.add_subplot(111)
ax.fill_between(range(9),ie.dynamicExpMax("temp"),ie.dynamicExpMin("temp"))
plt.show()
../_images/notebooks_24-Models_credalNetworks_29_0.svg
In [21]:
ie=gum.CNMonteCarloSampling(cn)
ie.insertModalsFile("res/cn/modalities.modal")

ie.setRepetitiveInd(False)
ie.setMaxTime(5)
ie.setMaxIter(1000)

ie.makeInference()
print(ie.messageApproximationScheme())
stopped with epsilon=0
In [22]:
fig=figure()
ax=fig.add_subplot(111)
ax.fill_between(range(9),ie.dynamicExpMax("temp"),ie.dynamicExpMin("temp"))
plt.show()
../_images/notebooks_24-Models_credalNetworks_31_0.svg
In [23]:
ie=gum.CNMonteCarloSampling(cn)
ie.insertModalsFile("res/cn/modalities.modal")

ie.setRepetitiveInd(False)
ie.setMaxTime(5)
ie.setMaxIter(5000)

gnb.animApproximationScheme(ie)
ie.makeInference()
../_images/notebooks_24-Models_credalNetworks_32_0.svg
In [24]:
fig=figure()
ax=fig.add_subplot(111)
ax.fill_between(range(9),ie.dynamicExpMax("temp"),ie.dynamicExpMin("temp"));
plt.show()
../_images/notebooks_24-Models_credalNetworks_33_0.svg