Sensitivity analysis for Bayesian networks using credal networks

Creative Commons License

aGrUM

interactive online version

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]:
anonymousBN A A B B A->B C C B->C F F B->F D D D->C E E D->E E->F
structs Inference in   0.37ms A 2026-10-27T15:22:15.551307 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:15.570132 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:15.593015 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:15.660053 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:15.617366 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:15.642172 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F

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)
../_images/notebooks_14-Examples_SensitivityAnalysisUsingCredalNetworks_6_0.svg
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()
anonymousBN A A B B A->B C C B->C F F B->F D D D->C E E D->E E->F
structs Inference in   0.37ms A 2026-10-27T15:22:15.551307 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:15.570132 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:15.593015 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:15.660053 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:15.617366 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:15.642172 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
structs Inference in   0.25ms A 2026-10-27T15:22:16.243850 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:16.260287 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:16.282404 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:16.343219 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:16.300580 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:16.323428 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1
structs Inference in   0.12ms A 2026-10-27T15:22:16.517290 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:16.541619 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:16.560604 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:16.630038 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:16.594791 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:16.612035 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.1
structs Inference in   0.26ms A 2026-10-27T15:22:16.799929 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:16.823187 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:16.853169 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:16.913452 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:16.877091 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:16.894326 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.01
structs Inference in   1.25ms A 2026-10-27T15:22:17.187529 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:17.213417 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:17.234604 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:17.299733 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:17.255194 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:17.278502 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.001
structs Inference in   0.86ms A 2026-10-27T15:22:17.454850 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-27T15:22:17.475133 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-27T15:22:17.499841 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-27T15:22:17.563431 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-27T15:22:17.521412 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-27T15:22:17.544274 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1e-10
In [ ]: