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.44ms A 2026-10-25T02:12:36.889510 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:36.915266 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:36.946337 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:37.009774 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:36.969017 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:36.987379 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.342428 , 0.657572] , [0.341278 , 0.658722]]

B:Range([0,1])
<A:0> : [[0.676997 , 0.323003] , [0.67699 , 0.32301]]
<A:1> : [[0.182966 , 0.817034] , [0.117296 , 0.882704]]

C:Range([0,1])
<B:0|D:0> : [[0.567064 , 0.432936] , [0.567032 , 0.432968]]
<B:1|D:0> : [[0.486228 , 0.513772] , [0.486119 , 0.513881]]
<B:0|D:1> : [[0.557282 , 0.442718] , [0.557245 , 0.442755]]
<B:1|D:1> : [[0.322739 , 0.677261] , [0.0405434 , 0.959457]]

D:Range([0,1])
<> : [[0.86887 , 0.13113]]

E:Range([0,1])
<D:0> : [[0.80294 , 0.19706] , [0.802939 , 0.197061]]
<D:1> : [[0.466925 , 0.533075] , [0.466777 , 0.533223]]

F:Range([0,1])
<E:0|B:0> : [[0.473768 , 0.526232] , [0.473635 , 0.526365]]
<E:1|B:0> : [[0.186694 , 0.813306] , [0.152504 , 0.847496]]
<E:0|B:1> : [[0.404645 , 0.595355] , [0.404243 , 0.595757]]
<E:1|B:1> : [[0.623479 , 0.376521] , [0.623465 , 0.376535]]


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.44ms A 2026-10-25T02:12:36.889510 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:36.915266 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:36.946337 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:37.009774 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:36.969017 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:36.987379 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
structs Inference in   0.43ms A 2026-10-25T02:12:37.632026 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:37.647815 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:37.669589 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:37.729933 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:37.691847 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:37.710110 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1
structs Inference in   1.12ms A 2026-10-25T02:12:37.901777 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:37.922678 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:37.944202 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:38.052705 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:37.964238 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:38.029386 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.1
structs Inference in   0.32ms A 2026-10-25T02:12:38.225002 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:38.242734 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:38.259676 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:38.360084 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:38.277555 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:38.298223 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.01
structs Inference in   0.69ms A 2026-10-25T02:12:38.571067 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:38.600385 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:38.620997 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:38.685594 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:38.641157 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:38.666304 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=0.001
structs Inference in   0.13ms A 2026-10-25T02:12:38.877870 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B 2026-10-25T02:12:38.899286 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ A->B C 2026-10-25T02:12:38.917611 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->C F 2026-10-25T02:12:39.008535 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ B->F D 2026-10-25T02:12:38.940240 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->C E 2026-10-25T02:12:38.990073 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ D->E E->F
noise=1e-10
In [ ]: