Credal Networks
In [1]:
import matplotlib.pyplot as plt
import pyagrum.credal_net as gum
import pyagrum.lib.notebook as gnb
gnb.configuration()
| Library | Version |
|---|---|
| OS | posix [darwin] |
| Python | 3.14.9 (main, Oct 21 2026, 06:02:15) [Clang 21.0.0 (clang-2100.3.34.2)] |
| IPython | 9.17.1 |
| Matplotlib | 3.11.2 |
| Numpy | 2.5.4 |
| pyDot | 4.0.1 |
| pyAgrum | 3.3.1 |
Sun Oct 25 02:12:36 2026 CEST
Credal Net from BN
In [2]:
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()
cn
Out[2]:
inference on Credal Net
In [3]:
gnb.flow.row(
bn, bn.cpt("B"), cn, bn_min.cpt("B"), bn_max.cpt("B"), captions=["Bayes Net", "CPT", "Credal Net", "CPTmin", "CPTmax"]
)
Out[3]:
|
|
|
| |
|---|---|---|---|
| 0.4207 | 0.3698 | 0.2096 | |
| 0.3942 | 0.1615 | 0.4443 | |
|
|
|
| |
|---|---|---|---|
| 0.3561 | 0.3052 | 0.1450 | |
| 0.3296 | 0.0969 | 0.3797 | |
|
|
|
| |
|---|---|---|---|
| 0.4853 | 0.4344 | 0.2742 | |
| 0.4588 | 0.2261 | 0.5089 | |
Binarization
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"])
Out[4]:
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.2151 , 0.7849] , [0.501898 , 0.498102]]
B:Range([0,2])
<A:0> : [[0.356068 , 0.369764 , 0.274168] , [0.356068 , 0.434372 , 0.20956] , [0.420678 , 0.434372 , 0.14495] , [0.485286 , 0.369764 , 0.14495] , [0.420679 , 0.305153 , 0.274168] , [0.485286 , 0.305153 , 0.209561]]
<A:1> : [[0.329558 , 0.161524 , 0.508918] , [0.329558 , 0.226133 , 0.444308] , [0.394167 , 0.226133 , 0.379699] , [0.458777 , 0.161524 , 0.379699] , [0.394169 , 0.0969131 , 0.508918] , [0.458777 , 0.0969131 , 0.44431]]
C:Range([0,1])
<B:0|D:0> : [[0.481153 , 0.518847] , [0.489451 , 0.510549]]
<B:1|D:0> : [[0.150727 , 0.849273] , [0.159026 , 0.840974]]
<B:2|D:0> : [[0.00622568 , 0.993774] , [0.0145251 , 0.985475]]
<B:0|D:1> : [[0.598881 , 0.401119] , [0.607181 , 0.392819]]
<B:1|D:1> : [[0.968466 , 0.0315343] , [0.976767 , 0.0232333]]
<B:2|D:1> : [[0.207493 , 0.792507] , [0.215794 , 0.784206]]
D:Range([0,1])
<A:0> : [[0.570427 , 0.429573] , [0.815896 , 0.184104]]
<A:1> : [[0.344214 , 0.655786] , [0.589683 , 0.410317]]
E:Range([0,1])
<A:0> : [[0.711704 , 0.288296] , [0.876444 , 0.123556]]
<A:1> : [[0.532011 , 0.467989] , [0.696751 , 0.303249]]
F:Range([0,1])
<E:0> : [[0.269014 , 0.730986] , [0.491561 , 0.508439]]
<E:1> : [[0.166909 , 0.833091] , [0.389456 , 0.610544]]
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.credal_net 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()
Inference with no evidence
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)
The problem of evidence
When evidence are inserted, there are some divergence.
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.20340464752247, 11.57038887238521, 11.996427741652514, 11.93872235246365, 11.889556424369683, 11.895778193186716, 11.895189970510256, 11.89449213926251, 11.894449763627915)
In [20]:
fig = plt.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 = plt.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 = plt.figure()
ax = fig.add_subplot(111)
ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))
plt.show()
In [ ]:

