Credal Networks
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()
| Library | Version |
|---|---|
| OS | posix [darwin] |
| Python | 3.13.2 (main, Feb 4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)] |
| IPython | 9.0.2 |
| Matplotlib | 3.10.1 |
| Numpy | 2.2.4 |
| pyDot | 3.0.4 |
| pyAgrum | 2.0.0 |
Mon Mar 24 10:22:15 2025 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"])
|
|
|
| |
|---|---|---|---|
| 0.2763 | 0.5225 | 0.2012 | |
| 0.7169 | 0.0099 | 0.2732 | |
|
|
|
| |
|---|---|---|---|
| 0.2723 | 0.5185 | 0.1973 | |
| 0.7130 | 0.0059 | 0.2692 | |
|
|
|
| |
|---|---|---|---|
| 0.2802 | 0.5264 | 0.2052 | |
| 0.7209 | 0.0139 | 0.2771 | |
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"])
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))
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)
A:Range([0,1])
<> : [[0.0508721 , 0.949128] , [0.118701 , 0.881299]]
B:Range([0,2])
<A:0> : [[0.272306 , 0.522487 , 0.205207] , [0.272306 , 0.526444 , 0.20125] , [0.276264 , 0.526444 , 0.197292] , [0.280222 , 0.522486 , 0.197292] , [0.276263 , 0.51853 , 0.205207] , [0.280222 , 0.51853 , 0.201248]]
<A:1> : [[0.712977 , 0.00989435 , 0.277129] , [0.712977 , 0.0138504 , 0.273172] , [0.716936 , 0.0138504 , 0.269214] , [0.720892 , 0.0098943 , 0.269214] , [0.716935 , 0.00593668 , 0.277129] , [0.720892 , 0.00593668 , 0.273171]]
C:Range([0,1])
<B:0|D:0> : [[0.375199 , 0.624801] , [0.624738 , 0.375262]]
<B:1|D:0> : [[0.563307 , 0.436693] , [0.812846 , 0.187154]]
<B:2|D:0> : [[0.53187 , 0.46813] , [0.781407 , 0.218593]]
<B:0|D:1> : [[0.235162 , 0.764838] , [0.4847 , 0.5153]]
<B:1|D:1> : [[0.188552 , 0.811448] , [0.43809 , 0.56191]]
<B:2|D:1> : [[0.22119 , 0.77881] , [0.470729 , 0.529271]]
D:Range([0,1])
<A:0> : [[0.474761 , 0.525239] , [0.774898 , 0.225102]]
<A:1> : [[0.36248 , 0.63752] , [0.662617 , 0.337383]]
E:Range([0,1])
<A:0> : [[0.608869 , 0.391131] , [0.832374 , 0.167626]]
<A:1> : [[0.293474 , 0.706526] , [0.516978 , 0.483022]]
F:Range([0,1])
<E:0> : [[0.326565 , 0.673435] , [0.546482 , 0.453518]]
<E:1> : [[0.615145 , 0.384855] , [0.835062 , 0.164938]]
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")
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
In [13]:
gnb.showInference(cn,targets={"A","H","L","D"},engine=ie,evs={"L":[0,1],"G":[1,0]})
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)
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)
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.203404646911963, 11.630924864746593, 12.02223403338019, 11.958468951520764, 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()
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()
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()
In [24]:
fig=figure()
ax=fig.add_subplot(111)
ax.fill_between(range(9),ie.dynamicExpMax("temp"),ie.dynamicExpMin("temp"));
plt.show()

