Credal Networks

Creative Commons License

aGrUM

interactive online version

In [1]:
import matplotlib.pyplot as plt

import pyagrum.credal_net as gum
import pyagrum.lib.notebook as gnb

gnb.configuration()
LibraryVersion
OSposix [darwin]
Python3.15.2 (main, Oct 25 2026, 16:57:51) [Clang 21.0.0 (clang-2100.3.34.2)]
IPython9.19.1
Matplotlib3.11.2
Numpy2.5.5
pyDot4.0.1
pyAgrum3.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]:
A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F

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]:
anonymousBN A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F
Bayes Net
B
A
0
1
2
0
0.17440.01110.8144
1
0.46830.30540.2263

CPT
A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F
Credal Net
B
A
0
1
2
0
0.17000.00670.8100
1
0.46380.30100.2219

CPTmin
B
A
0
1
2
0
0.17890.01560.8189
1
0.47270.30990.2308

CPTmax

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]:
A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F
Credal net
A A B-b0 B-b0 A->B-b0 B-b1 B-b1 A->B-b1 D D A->D E E A->E B-b0->B-b1 C C B-b0->C B-v0 B-v0 B-b0->B-v0 B-v1 B-v1 B-b0->B-v1 B-v2 B-v2 B-b0->B-v2 B-b1->C B-b1->B-v0 B-b1->B-v1 B-b1->B-v2 D->C F F E->F
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  49.41ms A 2026-12-01T22:35:29.825929 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:35:29.838775 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B D 2026-12-01T22:35:29.863594 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-12-01T22:35:29.874752 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E C 2026-12-01T22:35:29.852496 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C D->C F 2026-12-01T22:35:29.885648 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in 112.54ms A 2026-12-01T22:35:30.099708 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-12-01T22:35:30.111142 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-12-01T22:35:30.122437 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-12-01T22:35:30.146023 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-12-01T22:35:30.157569 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-12-01T22:35:30.133568 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-12-01T22:35:30.180218 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-12-01T22:35:30.191660 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-12-01T22:35:30.202749 image/svg+xml Matplotlib v3.11.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 2026-12-01T22:35:30.168820 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in   0.17ms A 2026-12-01T22:35:30.339525 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-12-01T22:35:30.350958 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-12-01T22:35:30.362158 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-12-01T22:35:30.384194 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-12-01T22:35:30.395934 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-12-01T22:35:30.373241 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-12-01T22:35:30.417702 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-12-01T22:35:30.429218 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-12-01T22:35:30.441112 image/svg+xml Matplotlib v3.11.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 2026-12-01T22:35:30.406802 image/svg+xml Matplotlib v3.11.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)
A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F
F
0
1
0.57520.3475
F
0
1
0.65250.4248
A A B B A->B D D A->D E E A->E C C B->C D->C F F E->F
F
0
1
0.57520.3475
F
0
1
0.65250.4248

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")
../_images/notebooks_24-Models_credalNetworks_15_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]:
A A E E A->E B B B->E C C F F C->F D D D->F G G D->G H H E->H F->H L L H->L
In [13]:
gnb.showInference(cn, targets={"A", "H", "L", "D"}, engine=ie, evs={"L": [0, 1], "G": [1, 0]})
../_images/notebooks_24-Models_credalNetworks_19_0.svg

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)
../_images/notebooks_24-Models_credalNetworks_23_0.svg

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)
../_images/notebooks_24-Models_credalNetworks_25_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.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()
../_images/notebooks_24-Models_credalNetworks_30_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 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()
../_images/notebooks_24-Models_credalNetworks_32_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_33_0.svg
In [24]:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))
plt.show()
../_images/notebooks_24-Models_credalNetworks_34_0.svg
In [ ]: