pyAgrum on notebooks
☰  ComparingBN

In [1]:
def dict2html(di):
return "<br/>".join([f"<b>{k:15}</b>:{v}" for k,v in di.items()])

In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
import pyAgrum.lib.bn_vs_bn as gcm


# How to compare two BNs¶

PyAgrum allows you to compare BNs in several ways. This notebook show you some of them:

• a graphical diff between the 2 BNs
• some scores form recal and precision
• distance measures (for more, see notebook 26-klForBNs for more)

## Between two different structures¶

In [3]:
bn1=gum.fastBN("A->B->C->D->E<-A->F")
bn2=gum.fastBN("A->B<-C->D->E<-A;F->E")
cmp=gcm.GraphicalBNComparator(bn1,bn2)
kl=gum.ExactBNdistance(bn1,bn2) # bruteForce is possible car the BNs are small
gnb.sideBySide(bn1,bn2,gnb.getBNDiff(bn1,bn2),dict2html(cmp.scores()),cmp.equivalentBNs(),dict2html(kl.compute()),
captions=['bn1','bn2','graphical diff','Scores','equivalent ?','distances'])

 G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E G A A B B A->B E E A->E C C C->B D D C->D D->E F F F->E G A A B B A->B E E A->E F F A->F C C C->B D D C->D D->E F->E count :{'tp': 8, 'tn': 16, 'fp': 2, 'fn': 4}recall :0.6666666666666666precision :0.8fscore :0.7272727272727272dist2opt :0.38873012632302 B has different parents in the two bns whose names are in {'C'} klPQ :2.1903126321313953errorPQ :0klQP :1.6807832970158665errorQP :0hellinger :0.751169430525294bhattacharya :0.33146365985777043jensen-shannon :0.3605123930126333 bn1 bn2 graphical diff Scores equivalent ? distances

The logic for the arcs of the graphical diff is the following. When comparaing bn1 with bn2 (in that order) :

• full black line: the arc is common for both
• full red line: the arc is common but inverted in bn2
• dotted black line: the arc is added in bn2
• dotted red line: the arc is removed in bn2

For the scores :

• precision and recall are computed considering BN1 as the reference
• $Fscore=\frac{2\cdot recall\cdot precision}{recall+precision}$ is the weighted average of Precision and Recall.
• $dist2opt=\sqrt{(1-precision)^2+(1-recall)^2}$ represents the euclidian distance to the ideal(precision=1,recall=1)

EquivalentBN return "OK" if equivalent or a reason for non equivalence

Finally, BruteForceKL compute in the same time several distances : I-projection, M-projection, Hellinger and Bhattacharya. For more complex BNs, there exists a GibbsKL to approximate those distances. Of course, the computation are much slower.

## Same structure, different parameters¶

In [4]:
bn1=gum.fastBN("A->B->C->D->E<-A->F")
bn2=gum.fastBN("A->B->C->D->E<-A->F")
cmp=gcm.GraphicalBNComparator(bn1,bn2)
kl=gum.ExactBNdistance(bn1,bn2) # bruteForce is possible car the BNs are small
gnb.sideBySide(bn1,bn2,gnb.getBNDiff(bn1,bn2),dict2html(cmp.scores()),cmp.equivalentBNs(),dict2html(kl.compute()),
captions=['bn1','bn2','graphical diff','Scores','equivalent ?','distances'])

 G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E count :{'tp': 12, 'tn': 18, 'fp': 0, 'fn': 0}recall :1.0precision :1.0fscore :1.0dist2opt :0.0 Different CPTs for A klPQ :2.207496158892435errorPQ :0klQP :2.853344351783943errorQP :0hellinger :0.8525268059240984bhattacharya :0.4516152994943757jensen-shannon :0.4599367533144224 bn1 bn2 graphical diff Scores equivalent ? distances

## identical BNs¶

In [5]:
bn1=gum.fastBN("A->B->C->D->E<-A->F")
bn2=bn1
cmp=gcm.GraphicalBNComparator(bn1,bn2)
kl=gum.ExactBNdistance(bn1,bn2) # bruteForce is possible car the BNs are small
gnb.sideBySide(bn1,bn2,gnb.getBNDiff(bn1,bn2),dict2html(cmp.scores()),cmp.equivalentBNs(),dict2html(kl.compute()),
captions=['bn1','bn2','graphical diff','Scores','equivalent ?','distances'])

 G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E G A A B B A->B E E A->E F F A->F C C B->C D D C->D D->E count :{'tp': 12, 'tn': 18, 'fp': 0, 'fn': 0}recall :1.0precision :1.0fscore :1.0dist2opt :0.0 OK klPQ :0.0errorPQ :0klQP :0.0errorQP :0hellinger :0.0bhattacharya :1.1102230246251565e-16jensen-shannon :0.0 bn1 bn2 graphical diff Scores equivalent ? distances

In the notebook 15-DirichletPrior, you can find an interresting discussion on how can change those scores and distance.

In [ ]: