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.15.2 (main, Oct 25 2026, 16:57:51) [Clang 21.0.0 (clang-2100.3.34.2)] |
| IPython | 9.19.1 |
| Matplotlib | 3.11.2 |
| Numpy | 2.5.5 |
| pyDot | 4.0.1 |
| pyAgrum | 3.4.0 |
Tue Dec 01 22:35:29 2026 CET
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.1744 | 0.0111 | 0.8144 | |
| 0.4683 | 0.3054 | 0.2263 | |
|
|
|
| |
|---|---|---|---|
| 0.1700 | 0.0067 | 0.8100 | |
| 0.4638 | 0.3010 | 0.2219 | |
|
|
|
| |
|---|---|---|---|
| 0.1789 | 0.0156 | 0.8189 | |
| 0.4727 | 0.3099 | 0.2308 | |
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.0834862 , 0.916514] , [0.1948 , 0.8052]]
B:Range([0,2])
<A:0> : [[0.169997 , 0.0111176 , 0.818885] , [0.169997 , 0.0155642 , 0.814439] , [0.174445 , 0.0155642 , 0.809991] , [0.178891 , 0.011118 , 0.809991] , [0.174444 , 0.00667071 , 0.818885] , [0.178891 , 0.00667071 , 0.814438]]
<A:1> : [[0.463822 , 0.305426 , 0.230753] , [0.463822 , 0.309872 , 0.226306] , [0.468268 , 0.309872 , 0.22186] , [0.472714 , 0.305426 , 0.22186] , [0.468269 , 0.300979 , 0.230753] , [0.472714 , 0.300979 , 0.226307]]
C:Range([0,1])
<B:0|D:0> : [[0.414944 , 0.585056] , [0.438469 , 0.561531]]
<B:1|D:0> : [[0.369971 , 0.630029] , [0.393496 , 0.606504]]
<B:2|D:0> : [[0.214103 , 0.785897] , [0.237628 , 0.762372]]
<B:0|D:1> : [[0.443414 , 0.556586] , [0.466937 , 0.533063]]
<B:1|D:1> : [[0.0176423 , 0.982358] , [0.0411664 , 0.958834]]
<B:2|D:1> : [[0.851217 , 0.148783] , [0.874741 , 0.125259]]
D:Range([0,1])
<A:0> : [[0.544346 , 0.455654] , [0.804719 , 0.195281]]
<A:1> : [[0.203507 , 0.796493] , [0.463882 , 0.536118]]
E:Range([0,1])
<A:0> : [[0.514398 , 0.485602] , [0.573342 , 0.426658]]
<A:1> : [[0.0442073 , 0.955793] , [0.103152 , 0.896848]]
F:Range([0,1])
<E:0> : [[0.942879 , 0.0571205] , [0.97552 , 0.0244798]]
<E:1> : [[0.541716 , 0.458284] , [0.574356 , 0.425644]]
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.20340464862347, 11.911090684366485, 12.190483075680442, 12.031555584857191, 12.004198572496797, 12.008870898650432, 12.007860641421736, 12.007688078235908, 12.007727248106775)
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 max iteration=1000
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 [ ]:

