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 [darwin]
Python3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]
IPython8.22.2
Matplotlib3.8.3
Numpy1.26.4
pyDot2.0.0
pyAgrum1.12.1.9
Thu Mar 21 11:31:29 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 D D C C D->C E E F F E->F A A A->D A->E B B A->B B->C
Bayes Net
B
A
0
1
2
0
0.23570.31020.4541
1
0.33430.27430.3914

CPT
G D D C C D->C E E F F E->F A A A->D A->E B B A->B B->C
Credal Net
B
A
0
1
2
0
0.14140.21600.3598
1
0.24000.18000.2971

CPTmin
B
A
0
1
2
0
0.33000.40450.5483
1
0.42860.36860.4857

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 D D C C D->C E E F F E->F A A A->D A->E B B A->B B->C
Credal net
G D D C C D->C B-b0 B-b0 B-v1 B-v1 B-b0->B-v1 B-b1 B-b1 B-b0->B-b1 B-b0->C B-v0 B-v0 B-b0->B-v0 B-v2 B-v2 B-b0->B-v2 B-b1->B-v1 B-b1->C B-b1->B-v0 B-b1->B-v2 E E F F E->F A A A->D A->B-b0 A->B-b1 A->E
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 877.29ms A 2024-03-21T11:31:48.349972 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B 2024-03-21T11:31:48.383917 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->B D 2024-03-21T11:31:48.431821 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->D E 2024-03-21T11:31:48.458621 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->E C 2024-03-21T11:31:48.410571 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B->C D->C F 2024-03-21T11:31:48.483849 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ E->F
structs Inference in 1124.58ms A 2024-03-21T11:31:49.930934 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0 2024-03-21T11:31:49.951060 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->B-b0 B-b1 2024-03-21T11:31:49.970246 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->B-b1 D 2024-03-21T11:31:50.029288 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->D E 2024-03-21T11:31:50.049379 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->E B-b0->B-b1 C 2024-03-21T11:31:50.001892 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->C B-v0 2024-03-21T11:31:50.098424 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->B-v0 B-v1 2024-03-21T11:31:50.126827 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->B-v1 B-v2 2024-03-21T11:31:50.146127 image/svg+xml Matplotlib v3.8.3, 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-03-21T11:31:50.073864 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ E->F
structs Inference in   0.50ms A 2024-03-21T11:31:50.434139 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0 2024-03-21T11:31:50.460851 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->B-b0 B-b1 2024-03-21T11:31:50.488245 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->B-b1 D 2024-03-21T11:31:50.532492 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->D E 2024-03-21T11:31:50.557240 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ A->E B-b0->B-b1 C 2024-03-21T11:31:50.511418 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->C B-v0 2024-03-21T11:31:50.633667 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->B-v0 B-v1 2024-03-21T11:31:50.659916 image/svg+xml Matplotlib v3.8.3, https://matplotlib.org/ B-b0->B-v1 B-v2 2024-03-21T11:31:50.685140 image/svg+xml Matplotlib v3.8.3, 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-03-21T11:31:50.575615 image/svg+xml Matplotlib v3.8.3, 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 D D C C D->C E E F F E->F A A A->D A->E B B A->B B->C
F
0
1
0.58800.2181
F
0
1
0.78190.4120
G D D C C D->C E E F F E->F A A A->D A->E B B A->B B->C
F
0
1
0.58800.2181
F
0
1
0.78190.4120

A:Range([0,1])
<> : [[0.939668 , 0.0603318] , [0.974144 , 0.025856]]

B:Range([0,2])
<A:0> : [[0.141425 , 0.310241 , 0.548333] , [0.141425 , 0.404525 , 0.45405] , [0.235707 , 0.404525 , 0.359768] , [0.329992 , 0.31024 , 0.359768] , [0.235709 , 0.215957 , 0.548333] , [0.329992 , 0.215957 , 0.454051]]
<A:1> : [[0.23999 , 0.274298 , 0.485712] , [0.23999 , 0.36858 , 0.39143] , [0.334273 , 0.36858 , 0.297147] , [0.428557 , 0.274296 , 0.297147] , [0.334275 , 0.180013 , 0.485712] , [0.428557 , 0.180013 , 0.39143]]

C:Range([0,1])
<B:0|D:0> : [[0.175729 , 0.824271] , [0.402498 , 0.597502]]
<B:1|D:0> : [[0.259334 , 0.740666] , [0.486101 , 0.513899]]
<B:2|D:0> : [[0.250533 , 0.749467] , [0.477301 , 0.522699]]
<B:0|D:1> : [[0.495322 , 0.504678] , [0.72209 , 0.27791]]
<B:1|D:1> : [[0.170075 , 0.829925] , [0.396843 , 0.603157]]
<B:2|D:1> : [[0.566174 , 0.433826] , [0.792943 , 0.207057]]

D:Range([0,1])
<A:0> : [[0.202312 , 0.797688] , [0.472062 , 0.527938]]
<A:1> : [[0.468855 , 0.531145] , [0.738604 , 0.261396]]

E:Range([0,1])
<A:0> : [[0.505796 , 0.494204] , [0.776852 , 0.223148]]
<A:1> : [[0.203291 , 0.796709] , [0.474348 , 0.525652]]

F:Range([0,1])
<E:0> : [[0.527326 , 0.472674] , [0.647423 , 0.352577]]
<E:1> : [[0.789832 , 0.210168] , [0.909928 , 0.0900716]]


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 L L D D G G D->G F F D->F E E H H E->H C C C->F A A A->E F->H H->L 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"))
(14.203404648293022, 11.911090684366485, 12.048452518236164, 12.031555584857191, 12.003107180947513, 12.007979271958872, 12.007860641421736, 12.007652604938034, 12.007725006693335)
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 [25]:
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