Sensitivity analysis for Bayesian networks using credal networks
There are several sensitivity analysis frameworks for Bayesian networks. A fairly efficient method is certainly to use credal networks to do this analysis.
Creating a Bayesian network
In [1]:
import pyagrum.credal_net as gum
import pyagrum.lib.notebook as gnb
In [2]:
bn = gum.fastBN("A->B->C<-D->E->F<-B")
gnb.flow.row(bn, gnb.getInference(bn))
Out[2]:
Building a credal network from a BN
It is easy to build a credal network from a Bayesian network by indicating the ‘noise’ on each parameter.
In [3]:
cr = gum.CredalNet(bn, bn)
gnb.show(cr)
In [4]:
cr.bnToCredal(1e-10, False, False)
In [5]:
cr.computeBinaryCPTMinMax()
In [6]:
print(cr)
A:Range([0,1])
<> : [[0.85829 , 0.14171]]
B:Range([0,1])
<A:0> : [[0.584418 , 0.415582] , [0.584393 , 0.415607]]
<A:1> : [[0.689437 , 0.310563] , [0.689431 , 0.310569]]
C:Range([0,1])
<B:0|D:0> : [[0.327479 , 0.672521] , [0.325983 , 0.674017]]
<B:1|D:0> : [[0.251309 , 0.748691] , [0.245061 , 0.754939]]
<B:0|D:1> : [[0.595901 , 0.404099] , [0.595879 , 0.404121]]
<B:1|D:1> : [[0.222965 , 0.777035] , [0.211442 , 0.788558]]
D:Range([0,1])
<> : [[0.189279 , 0.810721] , [0.101921 , 0.898079]]
E:Range([0,1])
<D:0> : [[0.298901 , 0.701099] , [0.296399 , 0.703601]]
<D:1> : [[0.233778 , 0.766222] , [0.224742 , 0.775258]]
F:Range([0,1])
<E:0|B:0> : [[0.40629 , 0.59371] , [0.405899 , 0.594101]]
<E:1|B:0> : [[0.245385 , 0.754615] , [0.238326 , 0.761674]]
<E:0|B:1> : [[0.851654 , 0.148345]]
<E:1|B:1> : [[0.182017 , 0.817983] , [0.132245 , 0.867755]]
Testing difference hypothesis about the global precision on the parameters
We can therefore easily conduct a sensitivity analysis based on an assumption of error on all the parameters of the network.
In [7]:
def showNoisy(bn, beta):
cr = gum.CredalNet(bn, bn)
cr.bnToCredal(beta, False, False)
cr.computeBinaryCPTMinMax()
ielbp = gum.CNLoopyPropagation(cr)
return gnb.getInference(cr, engine=ielbp)
In [8]:
for eps in [1, 1e-1, 1e-2, 1e-3, 1e-10]:
gnb.flow.add(showNoisy(bn, eps), caption=f"noise={eps}")
gnb.flow.display()
In [ ]:

