def kullbackLeiblerDistance(fac1, fac2, base=2): """ Calculates the Kullback-Leibler Distance between fac1 and fac2. The factors should represent probability distributions with the same variables, like P(X) and Q(X), or P(X, Y) and Q(X, Y) :param fac1: First factor in operation :param fac2: Second factor in operation :param base: Base of logarithms in the operations, defaults to 2 :returns: The Kullback-Leibler Distance """ kld = lambda f1, f2: f1 * log(f1 / f2) / log(base) return sum(Factor.factorOp(fac1, fac2, kld).values)