import os
%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
gnb.configuration()
Library | Version |
---|---|
OS | posix [linux] |
Python | 3.10.10 (main, Mar 5 2023, 22:26:53) [GCC 12.2.1 20230201] |
IPython | 8.13.2 |
Matplotlib | 3.7.1 |
Numpy | 1.24.3 |
pyDot | 1.4.2 |
pyAgrum | 1.8.1 |
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.6212 | 0.0518 | 0.3270 | |
0.4700 | 0.2808 | 0.2492 |
|
|
| |
---|---|---|---|
0.6004 | 0.0311 | 0.3062 | |
0.4493 | 0.2600 | 0.2284 |
|
|
| |
---|---|---|---|
0.6419 | 0.0726 | 0.3477 | |
0.4908 | 0.3015 | 0.2699 |
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
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.510773 , 0.489227] , [0.790333 , 0.209667]] B:Range([0,2]) <A:0> : [[0.600444 , 0.0518454 , 0.347711] , [0.600444 , 0.0725846 , 0.326971] , [0.621182 , 0.0725846 , 0.306233] , [0.641921 , 0.0518455 , 0.306233] , [0.621182 , 0.031107 , 0.347711] , [0.641921 , 0.031107 , 0.326972]] <A:1> : [[0.449309 , 0.280784 , 0.269907] , [0.449309 , 0.301523 , 0.249168] , [0.470046 , 0.301523 , 0.228431] , [0.490786 , 0.280783 , 0.228431] , [0.470046 , 0.260047 , 0.269907] , [0.490786 , 0.260047 , 0.249167]] C:Range([0,1]) <B:0|D:0> : [[0.43335 , 0.56665] , [0.579348 , 0.420652]] <B:1|D:0> : [[0.744503 , 0.255497] , [0.890501 , 0.109499]] <B:2|D:0> : [[0.687504 , 0.312496] , [0.833503 , 0.166497]] <B:0|D:1> : [[0.453807 , 0.546193] , [0.599804 , 0.400196]] <B:1|D:1> : [[0.603533 , 0.396467] , [0.749531 , 0.250469]] <B:2|D:1> : [[0.202389 , 0.797611] , [0.348387 , 0.651613]] D:Range([0,1]) <A:0> : [[0.720952 , 0.279048] , [0.792616 , 0.207384]] <A:1> : [[0.0537482 , 0.946252] , [0.125413 , 0.874587]] E:Range([0,1]) <A:0> : [[0.0782698 , 0.92173] , [0.18263 , 0.81737]] <A:1> : [[0.648741 , 0.351259] , [0.7531 , 0.2469]] F:Range([0,1]) <E:0> : [[0.647705 , 0.352295] , [0.686324 , 0.313676]] <E:1> : [[0.0289655 , 0.971034] , [0.0675862 , 0.932414]]
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