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.10 (main, Oct 25 2026, 17:12:00) [Clang 21.0.0 (clang-2100.3.34.2)]
IPython9.17.1
Matplotlib3.11.2
Numpy2.5.4
pyDot4.0.1
pyAgrum3.3.2
Tue Oct 27 15:22:15 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.06150.54670.3918
1
0.41640.12240.4612

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.03690.52210.3672
1
0.39180.09780.4366

CPTmin
B
A
0
1
2
0
0.08610.57130.4164
1
0.44100.14700.4858

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  14.38ms A 2026-10-27T15:22:16.389438 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:16.411980 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B D 2026-10-27T15:22:16.459730 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-27T15:22:16.481282 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E C 2026-10-27T15:22:16.437677 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C D->C F 2026-10-27T15:22:16.499689 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in  14.28ms A 2026-10-27T15:22:16.726372 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-10-27T15:22:16.749644 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-10-27T15:22:16.767521 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-10-27T15:22:16.811289 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-27T15:22:16.833879 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-10-27T15:22:16.793754 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-10-27T15:22:16.869831 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-10-27T15:22:16.886012 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-10-27T15:22:16.903396 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-27T15:22:16.849911 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ E->F
structs Inference in   0.15ms A 2026-10-27T15:22:17.145965 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0 2026-10-27T15:22:17.163859 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b0 B-b1 2026-10-27T15:22:17.182681 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B-b1 D 2026-10-27T15:22:17.217010 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->D E 2026-10-27T15:22:17.235480 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->E B-b0->B-b1 C 2026-10-27T15:22:17.199679 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->C B-v0 2026-10-27T15:22:17.280266 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v0 B-v1 2026-10-27T15:22:17.302683 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B-b0->B-v1 B-v2 2026-10-27T15:22:17.323609 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-27T15:22:17.259467 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.22930.5057
F
0
1
0.49430.7707
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.21690.5057
F
0
1
0.49430.7831

A:Range([0,1])
<> : [[0.122953 , 0.877047] , [0.286889 , 0.713111]]

B:Range([0,2])
<A:0> : [[0.0368852 , 0.546714 , 0.4164] , [0.0368852 , 0.571304 , 0.39181] , [0.0614757 , 0.571304 , 0.36722] , [0.0860656 , 0.546715 , 0.36722] , [0.0614757 , 0.522124 , 0.4164] , [0.0860656 , 0.522124 , 0.391811]]
<A:1> : [[0.391813 , 0.122426 , 0.485761] , [0.391813 , 0.147016 , 0.461171] , [0.416403 , 0.147016 , 0.436581] , [0.440994 , 0.122425 , 0.436581] , [0.416403 , 0.0978363 , 0.485761] , [0.440994 , 0.0978363 , 0.46117]]

C:Range([0,1])
<B:0|D:0> : [[0.280177 , 0.719823] , [0.405634 , 0.594366]]
<B:1|D:0> : [[0.780449 , 0.219551] , [0.905907 , 0.0940934]]
<B:2|D:0> : [[0.243839 , 0.756161] , [0.369297 , 0.630703]]
<B:0|D:1> : [[0.321696 , 0.678304] , [0.447152 , 0.552848]]
<B:1|D:1> : [[0.758062 , 0.241938] , [0.88352 , 0.11648]]
<B:2|D:1> : [[0.407432 , 0.592568] , [0.532887 , 0.467113]]

D:Range([0,1])
<A:0> : [[0.963338 , 0.0366625] , [0.984287 , 0.0157127]]
<A:1> : [[0.924447 , 0.0755532] , [0.945396 , 0.0546039]]

E:Range([0,1])
<A:0> : [[0.650974 , 0.349026] , [0.850418 , 0.149582]]
<A:1> : [[0.202053 , 0.797947] , [0.401496 , 0.598504]]

F:Range([0,1])
<E:0> : [[0.342286 , 0.657714] , [0.57359 , 0.42641]]
<E:1> : [[0.17348 , 0.82652] , [0.404786 , 0.595214]]


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.203404648128334, 11.718137408680768, 12.0406461626149, 12.019944850932905, 12.00214897282622, 12.008870898650432, 12.007624551734526, 12.007682925808101, 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 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 [ ]: