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.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801]
IPython8.19.0
Matplotlib3.8.2
Numpy1.26.3
pyDot2.0.0
pyAgrum1.11.0
Fri Jan 05 18:24:26 2024 CET

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 B B C C B->C A A A->B E E A->E D D A->D F F E->F D->C
Bayes Net
B
A
0
1
2
0
0.40970.27940.3109
1
0.29520.39430.3105

CPT
G B B C C B->C A A A->B E E A->E D D A->D F F E->F D->C
Credal Net
B
A
0
1
2
0
0.29790.16770.1991
1
0.18350.28250.1988

CPTmin
B
A
0
1
2
0
0.52140.39120.4227
1
0.40700.50600.4223

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 B B C C B->C A A A->B E E A->E D D A->D F F E->F D->C
Credal net
G B-v2 B-v2 B-v1 B-v1 A A B-b0 B-b0 A->B-b0 B-b1 B-b1 A->B-b1 E E A->E D D A->D B-b0->B-v2 B-b0->B-v1 B-b0->B-b1 C C B-b0->C B-v0 B-v0 B-b0->B-v0 B-b1->B-v2 B-b1->B-v1 B-b1->C B-b1->B-v0 F F E->F D->C
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  27.86ms A 2024-01-05T18:24:27.307461 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B 2024-01-05T18:24:27.354490 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->B D 2024-01-05T18:24:27.480318 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->D E 2024-01-05T18:24:27.547554 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->E C 2024-01-05T18:24:27.438661 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B->C D->C F 2024-01-05T18:24:27.614663 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ E->F
structs Inference in  28.65ms A 2024-01-05T18:24:27.918283 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0 2024-01-05T18:24:27.958884 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->B-b0 B-b1 2024-01-05T18:24:27.998826 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->B-b1 D 2024-01-05T18:24:28.079467 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->D E 2024-01-05T18:24:28.119761 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2024-01-05T18:24:28.039246 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->C B-v0 2024-01-05T18:24:28.201181 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2024-01-05T18:24:28.241834 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2024-01-05T18:24:28.283543 image/svg+xml Matplotlib v3.8.2, 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 2024-01-05T18:24:28.159567 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ E->F
structs Inference in   0.25ms A 2024-01-05T18:24:28.581257 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0 2024-01-05T18:24:28.634734 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->B-b0 B-b1 2024-01-05T18:24:28.703656 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->B-b1 D 2024-01-05T18:24:28.802998 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->D E 2024-01-05T18:24:28.883818 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2024-01-05T18:24:28.744814 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->C B-v0 2024-01-05T18:24:29.101883 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2024-01-05T18:24:29.141907 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2024-01-05T18:24:29.181671 image/svg+xml Matplotlib v3.8.2, 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 2024-01-05T18:24:28.971512 image/svg+xml Matplotlib v3.8.2, 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 B B C C B->C A A A->B E E A->E D D A->D F F E->F D->C
F
0
1
0.54670.1982
F
0
1
0.80180.4533
G B B C C B->C A A A->B E E A->E D D A->D F F E->F D->C
F
0
1
0.54630.1977
F
0
1
0.80230.4537

A:Range([0,1])
<> : [[0.325834 , 0.674166] , [0.711073 , 0.288927]]

B:Range([0,2])
<A:0> : [[0.297909 , 0.279419 , 0.422671] , [0.297909 , 0.391187 , 0.310904] , [0.409675 , 0.391187 , 0.199138] , [0.521445 , 0.279417 , 0.199138] , [0.409678 , 0.167651 , 0.422671] , [0.521445 , 0.167651 , 0.310905]]
<A:1> : [[0.183463 , 0.394252 , 0.422285] , [0.183463 , 0.506018 , 0.31052] , [0.295232 , 0.506018 , 0.19875] , [0.406998 , 0.394252 , 0.19875] , [0.295232 , 0.282483 , 0.422285] , [0.406998 , 0.282483 , 0.310519]]

C:Range([0,1])
<B:0|D:0> : [[0.540373 , 0.459627] , [0.579779 , 0.420221]]
<B:1|D:0> : [[0.34266 , 0.65734] , [0.382066 , 0.617934]]
<B:2|D:0> : [[0.486233 , 0.513767] , [0.525641 , 0.474359]]
<B:0|D:1> : [[0.571805 , 0.428195] , [0.611212 , 0.388788]]
<B:1|D:1> : [[0.347976 , 0.652024] , [0.387381 , 0.612619]]
<B:2|D:1> : [[0.029554 , 0.970446] , [0.0689604 , 0.93104]]

D:Range([0,1])
<A:0> : [[0.305195 , 0.694805] , [0.569164 , 0.430836]]
<A:1> : [[0.197977 , 0.802023] , [0.461947 , 0.538053]]

E:Range([0,1])
<A:0> : [[0.504018 , 0.495982] , [0.508674 , 0.491326]]
<A:1> : [[0.991855 , 0.0081448] , [0.99651 , 0.0034904]]

F:Range([0,1])
<E:0> : [[0.659123 , 0.340877] , [0.853909 , 0.146091]]
<E:1> : [[0.341393 , 0.658607] , [0.536179 , 0.463821]]


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 B B E E B->E A A A->E L L F F H H F->H E->H C C C->F H->L G G D D D->F D->G
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"))
(14.20340464862347, 11.671441990090813, 12.173214728164902, 11.954176229168535, 11.966313382958862, 11.964867852223103, 11.965031829300205, 11.965013837826506, 11.965015808981818)
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