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.3 (main, Jun 5 2023, 09:32:32) [GCC 13.1.1 20230429]
IPython8.14.0
Matplotlib3.7.2
Numpy1.25.1
pyDot1.4.2
pyAgrum1.8.3.9
Tue Jul 25 09:22:57 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 F F A A E E A->E B B A->B D D A->D E->F C C B->C D->C
Bayes Net
B
A
0
1
2
0
0.19170.52450.2838
1
0.29800.61510.0869

CPT
G F F A A E E A->E B B A->B D D A->D E->F C C B->C D->C
Credal Net
B
A
0
1
2
0
0.15690.48980.2490
1
0.26320.58030.0522

CPTmin
B
A
0
1
2
0
0.22650.55930.3185
1
0.33280.64990.1217

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 F F A A E E A->E B B A->B D D A->D E->F C C B->C D->C
Credal net
G F F B-b1 B-b1 B-v0 B-v0 B-b1->B-v0 C C B-b1->C B-v2 B-v2 B-b1->B-v2 B-v1 B-v1 B-b1->B-v1 A A A->B-b1 E E A->E B-b0 B-b0 A->B-b0 D D A->D E->F B-b0->B-b1 B-b0->B-v0 B-b0->C B-b0->B-v2 B-b0->B-v1 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.45ms A 2023-07-25T09:22:57.845824 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B 2023-07-25T09:22:57.899082 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->B D 2023-07-25T09:22:58.038996 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->D E 2023-07-25T09:22:58.082988 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->E C 2023-07-25T09:22:57.993855 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B->C D->C F 2023-07-25T09:22:58.129889 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ E->F
structs Inference in  29.86ms A 2023-07-25T09:22:58.329441 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0 2023-07-25T09:22:58.388891 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->B-b0 B-b1 2023-07-25T09:22:58.435600 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->B-b1 D 2023-07-25T09:22:58.526722 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->D E 2023-07-25T09:22:58.571831 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2023-07-25T09:22:58.481064 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->C B-v0 2023-07-25T09:22:58.663050 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2023-07-25T09:22:58.707652 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2023-07-25T09:22:58.752074 image/svg+xml Matplotlib v3.7.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 2023-07-25T09:22:58.617823 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ E->F
structs Inference in   0.31ms A 2023-07-25T09:22:59.090917 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0 2023-07-25T09:22:59.136528 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->B-b0 B-b1 2023-07-25T09:22:59.180935 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->B-b1 D 2023-07-25T09:22:59.274450 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->D E 2023-07-25T09:22:59.321853 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2023-07-25T09:22:59.225872 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->C B-v0 2023-07-25T09:22:59.492302 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2023-07-25T09:22:59.538407 image/svg+xml Matplotlib v3.7.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2023-07-25T09:22:59.582390 image/svg+xml Matplotlib v3.7.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 2023-07-25T09:22:59.446594 image/svg+xml Matplotlib v3.7.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 F F A A E E A->E B B A->B D D A->D E->F C C B->C D->C
F
0
1
0.31030.3566
F
0
1
0.64340.6897
G F F A A E E A->E B B A->B D D A->D E->F C C B->C D->C
F
0
1
0.28730.3566
F
0
1
0.64340.7127

A:Range([0,1])
<> : [[0.213995 , 0.786005] , [0.49932 , 0.50068]]

B:Range([0,2])
<A:0> : [[0.156917 , 0.524545 , 0.318538] , [0.156917 , 0.559314 , 0.283769] , [0.191685 , 0.559314 , 0.249001] , [0.226453 , 0.524546 , 0.249001] , [0.191684 , 0.489778 , 0.318538] , [0.226453 , 0.489778 , 0.283769]]
<A:1> : [[0.263229 , 0.615083 , 0.121688] , [0.263229 , 0.649852 , 0.0869191] , [0.297997 , 0.649852 , 0.0521512] , [0.332765 , 0.615084 , 0.0521512] , [0.297996 , 0.580316 , 0.121688] , [0.332765 , 0.580316 , 0.0869198]]

C:Range([0,1])
<B:0|D:0> : [[0.658631 , 0.341369] , [0.810215 , 0.189785]]
<B:1|D:0> : [[0.422569 , 0.577431] , [0.574153 , 0.425847]]
<B:2|D:0> : [[0.522177 , 0.477823] , [0.673762 , 0.326238]]
<B:0|D:1> : [[0.240798 , 0.759202] , [0.392383 , 0.607617]]
<B:1|D:1> : [[0.565201 , 0.434799] , [0.716787 , 0.283213]]
<B:2|D:1> : [[0.113689 , 0.886311] , [0.265273 , 0.734727]]

D:Range([0,1])
<A:0> : [[0.00377644 , 0.996224] , [0.00880853 , 0.991191]]
<A:1> : [[0.0898662 , 0.910134] , [0.0948992 , 0.905101]]

E:Range([0,1])
<A:0> : [[0.598238 , 0.401762] , [0.827817 , 0.172183]]
<A:1> : [[0.331707 , 0.668293] , [0.561286 , 0.438714]]

F:Range([0,1])
<E:0> : [[0.365006 , 0.634994] , [0.68227 , 0.31773]]
<E:1> : [[0.237949 , 0.762051] , [0.555212 , 0.444788]]


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 G G F F H H F->H L L A A E E A->E E->H B B B->E H->L C C C->F D D D->G D->F
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.864395265918771, 12.190483075680442, 12.023367090540296, 11.975306510688327, 11.943567938674553, 11.949683596400295, 11.94586710688126, 11.946601778299542)
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