Credal Networks
In [1]:
%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
In [2]:
import pyagrum as gum
import pyagrum.lib.notebook as gnb
gnb.configuration()
| Library | Version |
|---|---|
| OS | posix [linux] |
| Python | 3.13.5 (main, Jun 21 2025, 09:35:00) [GCC 15.1.1 20250425] |
| IPython | 9.4.0 |
| Matplotlib | 3.10.3 |
| Numpy | 2.3.1 |
| pyDot | 4.0.1 |
| pyAgrum | 2.2.0 |
Thu Jul 24 00:09:50 2025 CEST
Credal Net from BN
In [3]:
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()
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.0687 | 0.2867 | 0.6446 | |
| 0.3778 | 0.3801 | 0.2421 | |
|
|
|
| |
|---|---|---|---|
| 0.0412 | 0.2592 | 0.6171 | |
| 0.3503 | 0.3526 | 0.2146 | |
|
|
|
| |
|---|---|---|---|
| 0.0962 | 0.3142 | 0.6721 | |
| 0.4053 | 0.4076 | 0.2696 | |
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"])
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)
)
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:Range([0,1])
<> : [[0.268972 , 0.731028] , [0.627601 , 0.372399]]
B:Range([0,2])
<A:0> : [[0.0412288 , 0.286709 , 0.672062] , [0.0412288 , 0.314195 , 0.644577] , [0.0687142 , 0.314195 , 0.617091] , [0.0962025 , 0.286706 , 0.617091] , [0.0687168 , 0.259221 , 0.672062] , [0.0962025 , 0.259221 , 0.644576]]
<A:1> : [[0.350282 , 0.380118 , 0.2696] , [0.350282 , 0.407603 , 0.242115] , [0.377769 , 0.407603 , 0.214628] , [0.405255 , 0.380117 , 0.214628] , [0.377768 , 0.352632 , 0.2696] , [0.405255 , 0.352632 , 0.242114]]
C:Range([0,1])
<B:0|D:0> : [[0.753178 , 0.246822] , [0.780616 , 0.219384]]
<B:1|D:0> : [[0.294783 , 0.705217] , [0.322218 , 0.677782]]
<B:2|D:0> : [[0.371397 , 0.628603] , [0.398834 , 0.601166]]
<B:0|D:1> : [[0.951987 , 0.0480132] , [0.979422 , 0.0205781]]
<B:1|D:1> : [[0.535647 , 0.464353] , [0.563084 , 0.436916]]
<B:2|D:1> : [[0.400275 , 0.599725] , [0.427711 , 0.572289]]
D:Range([0,1])
<A:0> : [[0.349589 , 0.650411] , [0.664896 , 0.335104]]
<A:1> : [[0.236478 , 0.763522] , [0.551783 , 0.448217]]
E:Range([0,1])
<A:0> : [[0.109148 , 0.890852] , [0.254678 , 0.745322]]
<A:1> : [[0.29223 , 0.70777] , [0.437759 , 0.562241]]
F:Range([0,1])
<E:0> : [[0.597266 , 0.402734] , [0.706077 , 0.293923]]
<E:1> : [[0.809581 , 0.190419] , [0.918392 , 0.0816078]]
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")
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
In [13]:
gnb.showInference(cn, targets={"A", "H", "L", "D"}, engine=ie, evs={"L": [0, 1], "G": [1, 0]})
Comparing inference in credal networks
In [14]:
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()
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)
but not when evidence are inserted
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)
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.203404648293022, 11.817699847864338, 12.173214728164902, 12.019944850932905, 12.00214897282622, 12.008870898650432, 12.007624551734526, 12.007682925808101, 12.007727248106775)
In [20]:
fig = figure()
ax = fig.add_subplot(111)
ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))
plt.show()
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 = figure()
ax = fig.add_subplot(111)
ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))
plt.show()
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()
In [24]:
fig = figure()
ax = fig.add_subplot(111)
ax.fill_between(range(9), ie.dynamicExpMax("temp"), ie.dynamicExpMin("temp"))
plt.show()

