☰  klForBns
In :
import os

%matplotlib inline

from pylab import *
import matplotlib.pyplot as plt


# Initialisation¶

• importing pyAgrum
• importing pyAgrum.lib tools
In :
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb


## Create a first BN : bn¶

In :
bn=gum.loadBN(os.path.join("res","asia.bif"))
# randomly re-generate parameters for every Conditional Probability Table
bn.generateCPTs()
bn

Out:

## Create a second BN : bn2¶

In :
bn2=gum.loadBN(os.path.join("res","asia.bif"))
bn2.generateCPTs()
bn2

Out:

## bn vs bn2 : different parameters¶

In :
gnb.sideBySide(bn.cpt(3),bn2.cpt(3),
captions=["a CPT in bn","same CPT in bn2"])

positive_XraY
tuberculos_or_cancer
0
1
0
0.76550.2345
1
0.71980.2802
positive_XraY
tuberculos_or_cancer
0
1
0
0.80450.1955
1
0.25610.7439
a CPT in bn
same CPT in bn2

## Exact and (Gibbs) approximated KL-divergence¶

In order to compute KL-divergence, we just need to be sure that the 2 distributions are defined on the same domain (same variables, etc.)

### Exact KL

In :
g1=gum.ExactBNdistance(bn,bn2)
print(g1.compute())

{'klPQ': 3.5054362470252514, 'errorPQ': 0, 'klQP': 3.0433140109468484, 'errorQP': 0, 'hellinger': 0.925591730602506, 'bhattacharya': 0.5592459016415671, 'jensen-shannon': 0.517962952176204}


If the models are not on the same domain :

In :
bn_different_domain=gum.loadBN(os.path.join("res","alarm.dsl"))

# g=gum.BruteForceKL(bn,bn_different_domain) # a KL-divergence between asia and alarm ... :(
#
# would cause
#---------------------------------------------------------------------------
#OperationNotAllowed                       Traceback (most recent call last)
#
#OperationNotAllowed: this operation is not allowed : KL : the 2 BNs are not compatible (not the same vars : visit_to_Asia?)


### Gibbs-approximated KL

In :
g=gum.GibbsBNdistance(bn,bn2)
g.setVerbosity(True)
g.setMaxTime(120)
g.setBurnIn(5000)
g.setEpsilon(1e-7)
g.setPeriodSize(500)

In :
print(g.compute())
print("Computed in {0} s".format(g.currentTime()))

{'klPQ': 3.5005108023885554, 'errorPQ': 0, 'klQP': 3.1090808091554334, 'errorQP': 0, 'hellinger': 0.9302736665500002, 'bhattacharya': 0.5543641295817159, 'jensen-shannon': 0.5232423442995896}
Computed in 6.830701488 s

In :
print("--")

print(g.messageApproximationScheme())
print("--")

print("Temps de calcul : {0}".format(g.currentTime()))
print("Nombre d'itérations : {0}".format(g.nbrIterations()))

p=plot(g.history(), 'g')

--
stopped with epsilon=1e-07
--
Temps de calcul : 6.830701488
Nombre d'itérations : 627500


### Animation of Gibbs KL¶

Since it may be difficult to know what happens during approximation algorithm, pyAgrum allows to follow the iteration using animated matplotlib figure

In :
g=gum.GibbsBNdistance(bn,bn2)
g.setMaxTime(60)
g.setBurnIn(500)
g.setEpsilon(1e-7)
g.setPeriodSize(5000)

In :
gnb.animApproximationScheme(g) # logarithmique scale for Y
g.compute()

Out:
{'klPQ': 3.5135724486450943,
'errorPQ': 0,
'klQP': 3.042951771828066,
'errorQP': 0,
'hellinger': 0.9249284924518736,
'bhattacharya': 0.5639121794970662,
'jensen-shannon': 0.5167141059085778}