Credal Networks
![]() | ![]() |
import matplotlib.pyplot as plt
import pyagrum.credal_net as gumimport pyagrum.lib.notebook as gnb
gnb.configuration()| Library | Version |
|---|---|
| OS | posix [darwin] |
| Python | 3.14.10 (main, Oct 25 2026, 17:12:00) [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.2 |
Credal Net from BN
Section titled “Credal Net from BN”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()cninference on Credal Net
Section titled “inference on Credal Net”gnb.flow.row( bn, bn.cpt("B"), cn, bn_min.cpt("B"), bn_max.cpt("B"), captions=["Bayes Net", "CPT", "Credal Net", "CPTmin", "CPTmax"])|
|
|
| |
|---|---|---|---|
| 0.0615 | 0.5467 | 0.3918 | |
| 0.4164 | 0.1224 | 0.4612 | |
|
|
|
| |
|---|---|---|---|
| 0.0369 | 0.5221 | 0.3672 | |
| 0.3918 | 0.0978 | 0.4366 | |
|
|
|
| |
|---|---|---|---|
| 0.0861 | 0.5713 | 0.4164 | |
| 0.4410 | 0.1470 | 0.4858 | |
Binarization
Section titled “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)
cn2 = gum.CredalNet(bn_min, bn_max)cn2.intervalToCredal()cn2.approximatedBinarization()cn2.computeBinaryCPTMinMax()
gnb.flow.row(cn, cn2, captions=["Credal net", "Binarized credal net"])Here, becomes
- -b : the -th bit of B
- instrumental -v : the indicator variable for each modality of
ie_mc = gum.CNMonteCarloSampling(cn)ie2_lbp = gum.CNLoopyPropagation(cn2)ie2_mc = gum.CNMonteCarloSampling(cn2)gnb.sideBySide( gnb.getInference(cn, engine=ie_mc), gnb.getInference(cn2, engine=ie2_mc), gnb.getInference(cn2, engine=ie2_lbp))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.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
Section titled “Credal Net from bif files”cn = gum.CredalNet("res/cn/2Umin.bif", "res/cn/2Umax.bif")cn.intervalToCredal()gnb.showCN(cn, "2")ie = gum.CNMonteCarloSampling(cn)ie.insertEvidenceFile("res/cn/L2U.evi")ie.setRepetitiveInd(False)ie.setMaxTime(1)ie.setMaxIter(1000)
ie.makeInference()cngnb.showInference(cn, targets={"A", "H", "L", "D"}, engine=ie, evs={"L": [0, 1], "G": [1, 0]})Comparing inference in credal networks
Section titled “Comparing inference in credal networks”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
Section titled “Inference with no evidence”The two inference give quite the same result
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
Section titled “The problem of evidence”When evidence are inserted, there are some divergence.
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
Section titled “Dynamical Credal Net”cn = gum.CredalNet("res/cn/bn_c_8.bif", "res/cn/den_c_8.bif")cn.bnToCredal(0.8, False)ie = gum.CNMonteCarloSampling(cn)ie.insertModalsFile("res/cn/modalities.modal")
ie.setRepetitiveInd(True)ie.setMaxTime(5)ie.setMaxIter(1000)
ie.makeInference()print(ie.dynamicExpMax("temp"))(14.203404648128334, 11.718137408680768, 12.0406461626149, 12.019944850932905, 12.00214897282622, 12.008870898650432, 12.007624551734526, 12.007682925808101, 12.007727248106775)fig = plt.figure()ax = fig.add_subplot(111)ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))plt.show()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=0fig = plt.figure()ax = fig.add_subplot(111)ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))plt.show()ie = gum.CNMonteCarloSampling(cn)ie.insertModalsFile("res/cn/modalities.modal")
ie.setRepetitiveInd(False)ie.setMaxTime(5)ie.setMaxIter(5000)
gnb.animApproximationScheme(ie)ie.makeInference()fig = plt.figure()ax = fig.add_subplot(111)ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))plt.show()
