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.14.9 (main, Oct 21 2026, 06:02:15) [Clang 21.0.0 (clang-2100.3.34.2)]
IPython9.17.1
Matplotlib3.11.2
Numpy2.5.4
pyDot4.0.1
pyAgrum3.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]:
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.42070.36980.2096
1
0.39420.16150.4443

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.35610.30520.1450
1
0.32960.09690.3797

CPTmin
B
A
0
1
2
0
0.48530.43440.2742
1
0.45880.22610.5089

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  15.37ms A 2026-10-25T02:12:37.765949 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:37.789486 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B D 2026-10-25T02:12:37.837261 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-25T02:12:37.855660 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E C 2026-10-25T02:12:37.816595 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C D->C F 2026-10-25T02:12:37.874945 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in  21.26ms A 2026-10-25T02:12:38.065404 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-10-25T02:12:38.081470 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-10-25T02:12:38.100950 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-10-25T02:12:38.152807 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-25T02:12:38.174056 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-10-25T02:12:38.121565 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-10-25T02:12:38.231927 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-10-25T02:12:38.252837 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-10-25T02:12:38.273929 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-10-25T02:12:38.201086 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in   0.76ms A 2026-10-25T02:12:38.528407 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-10-25T02:12:38.546523 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-10-25T02:12:38.568724 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-10-25T02:12:38.607237 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-25T02:12:38.631276 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-10-25T02:12:38.588619 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-10-25T02:12:38.670975 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-10-25T02:12:38.691986 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-10-25T02:12:38.715687 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-10-25T02:12:38.649257 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.22520.5302
F
0
1
0.46980.7748
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.22520.5302
F
0
1
0.46980.7748

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")
../_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.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()
../_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 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()
../_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
../_images/notebooks_24-Models_credalNetworks_33_1.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 [ ]: