*/
public static double KL (FactorGraph mdl1, FactorGraph mdl2)
{
JunctionTreeInferencer inf1 = new JunctionTreeInferencer ();
inf1.computeMarginals (mdl1);
JunctionTree jt1 = inf1.lookupJunctionTree ();
JunctionTreeInferencer inf2 = new JunctionTreeInferencer ();
inf2.computeMarginals (mdl2);
JunctionTree jt2 = inf2.lookupJunctionTree ();
double entropy = jt1.entropy ();
double energy = 0;
for (Iterator it = jt2.clusterPotentials ().iterator(); it.hasNext();) {
Factor marg2 = (Factor) it.next ();
Factor marg1 = inf1.lookupMarginal (marg2.varSet ());
for (AssignmentIterator assnIt = marg2.assignmentIterator (); assnIt.hasNext();) {
energy += marg1.value (assnIt) * marg2.logValue (assnIt);
assnIt.advance();
}
}
for (Iterator it = jt2.sepsetPotentials ().iterator(); it.hasNext();) {
Factor marg2 = (Factor) it.next ();
Factor marg1 = inf1.lookupMarginal (marg2.varSet ());
for (AssignmentIterator assnIt = marg2.assignmentIterator (); assnIt.hasNext();) {
energy -= marg1.value (assnIt) * marg2.logValue (assnIt);
assnIt.advance();