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.30ms A 2026-12-01T22:34:44.071979 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:44.085283 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:44.098233 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:44.136868 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:44.111711 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:44.124219 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.627415 , 0.372585] , [0.627401 , 0.372599]]

B:Range([0,1])
<A:0> : [[0.726317 , 0.273683] , [0.726313 , 0.273687]]
<A:1> : [[0.950841 , 0.0491589]]

C:Range([0,1])
<B:0|D:0> : [[0.974059 , 0.0259404]]
<B:1|D:0> : [[0.802868 , 0.197132] , [0.802867 , 0.197133]]
<B:0|D:1> : [[0.434715 , 0.565285] , [0.434469 , 0.565531]]
<B:1|D:1> : [[0.411545 , 0.588455] , [0.411186 , 0.588814]]

D:Range([0,1])
<> : [[0.728641 , 0.271359] , [0.728637 , 0.271363]]

E:Range([0,1])
<D:0> : [[0.895505 , 0.104495]]
<D:1> : [[0.834186 , 0.165813]]

F:Range([0,1])
<E:0|B:0> : [[0.781687 , 0.218313] , [0.781685 , 0.218315]]
<E:1|B:0> : [[0.458824 , 0.541176] , [0.458657 , 0.541343]]
<E:0|B:1> : [[0.330252 , 0.669748] , [0.328828 , 0.671172]]
<E:1|B:1> : [[0.943219 , 0.0567807]]


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.30ms A 2026-12-01T22:34:44.071979 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:44.085283 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:44.098233 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:44.136868 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:44.111711 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:44.124219 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
structs Inference in   0.18ms A 2026-12-01T22:34:44.515977 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:44.526956 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:44.537626 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:44.571922 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:44.550078 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:44.561066 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1
structs Inference in   0.18ms A 2026-12-01T22:34:44.673358 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:44.685065 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:44.696616 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:44.732704 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:44.708602 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:44.720660 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.1
structs Inference in   0.17ms A 2026-12-01T22:34:44.834536 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:44.846022 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:44.857304 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:44.891328 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:44.868762 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:44.880112 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.01
structs Inference in   0.09ms A 2026-12-01T22:34:45.012310 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:45.025657 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:45.036970 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:45.070746 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:45.048387 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:45.059594 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.001
structs Inference in   0.12ms A 2026-12-01T22:34:45.169278 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-12-01T22:34:45.180278 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-12-01T22:34:45.191181 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-12-01T22:34:45.225480 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-12-01T22:34:45.202052 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-12-01T22:34:45.214712 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1e-10
In [ ]: