Credal Networks
![]() | ![]() |
import matplotlib.pyplot as plt
import pyagrum as gumimport pyagrum.lib.notebook as gnb
gnb.configuration()| Library | Version |
|---|---|
| OS | posix [darwin] |
| Python | 3.14.0 (main, Oct 7 2025, 09:34:52) [Clang 17.0.0 (clang-1700.3.19.1)] |
| IPython | 9.6.0 |
| Matplotlib | 3.10.7 |
| Numpy | 2.3.4 |
| pyDot | 4.0.1 |
| pyAgrum | 2.3.0.9 |
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.4546 | 0.3811 | 0.1643 | |
| 0.1253 | 0.4896 | 0.3851 | |
|
|
|
| |
|---|---|---|---|
| 0.4045 | 0.3309 | 0.1142 | |
| 0.0752 | 0.4394 | 0.3350 | |
|
|
|
| |
|---|---|---|---|
| 0.5048 | 0.4312 | 0.2144 | |
| 0.1755 | 0.5397 | 0.4352 | |
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.564403 , 0.435597] , [0.813316 , 0.186684]]
B:Range([0,2])<A:0> : [[0.404503 , 0.381073 , 0.214425] , [0.404503 , 0.431208 , 0.164289] , [0.454634 , 0.431208 , 0.114158] , [0.50477 , 0.381072 , 0.114158] , [0.454634 , 0.330941 , 0.214425] , [0.50477 , 0.330941 , 0.164289]] <A:1> : [[0.0752 , 0.489576 , 0.435224] , [0.0752 , 0.53971 , 0.38509] , [0.125334 , 0.53971 , 0.334956] , [0.175468 , 0.489575 , 0.334956] , [0.125335 , 0.439441 , 0.435224] , [0.175468 , 0.439441 , 0.385091]]
C:Range([0,1])<B:0|D:0> : [[0.274878 , 0.725122] , [0.378155 , 0.621845]] <B:1|D:0> : [[0.394721 , 0.605279] , [0.497997 , 0.502003]] <B:2|D:0> : [[0.390519 , 0.609481] , [0.493797 , 0.506203]] <B:0|D:1> : [[0.373125 , 0.626875] , [0.476401 , 0.523599]] <B:1|D:1> : [[0.0774566 , 0.922543] , [0.180733 , 0.819267]] <B:2|D:1> : [[0.566948 , 0.433052] , [0.670225 , 0.329775]]
D:Range([0,1])<A:0> : [[0.169429 , 0.830571] , [0.395333 , 0.604667]] <A:1> : [[0.192405 , 0.807595] , [0.41831 , 0.58169]]
E:Range([0,1])<A:0> : [[0.487658 , 0.512342] , [0.594354 , 0.405646]] <A:1> : [[0.813281 , 0.186719] , [0.919977 , 0.0800234]]
F:Range([0,1])<E:0> : [[0.494737 , 0.505263] , [0.768817 , 0.231183]] <E:1> : [[0.20556 , 0.79444] , [0.47964 , 0.52036]]
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 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.20340464862347, 11.911090684366485, 12.0406461626149, 12.031555584857191, 12.003107180947513, 12.008870898650432, 12.007860641421736, 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()
