Comparison of Bayesian networks

tools for comparing Bayesian networks

To compare Bayesian networks, one can compare their structure (see pyagrum.lib.bn_vs_bn.GraphicalBNComparator). However BNs can also be compared as probability distributions.

class pyagrum.ExactBNdistance(*args)

Class representing exact computation of divergence and distance between BNs

ExactBNdistance(P,Q) -> ExactBNdistance
Parameters:
ExactBNdistance(ebnd) -> ExactBNdistance
Parameters:
Raises:

pyagrum.OperationNotAllowed – If P and Q are not defined on the same variables (same names and same domains)

Scientific notes

ExactBNdistance compares two Bayesian networks \(P\) and \(Q\) defined on the same variables (same names, same domains) by enumerating all their joint configurations \(x\), whose probabilities \(p = P(x)\) and \(q = Q(x)\) are given by the chain rule of each network. The cost is the size of the joint domain: the method is limited to small networks, pyagrum.MCBNDistance and pyagrum.GibbsBNdistance estimate the same quantities by sampling. compute() returns:

  • klPQ and klQP, the Kullback-Leibler divergences [1] in both directions, in bits (logarithm in base 2):

    \[KL(P \| Q) = \sum_{x:\, p > 0} p \log_2 \frac{p}{q}, \qquad KL(Q \| P) = \sum_{x:\, q > 0} q \log_2 \frac{q}{p}.\]

    A configuration with \(p > 0\) and \(q = 0\) makes \(KL(P\|Q)\) infinite: klPQ is then \(+\infty\), and errorPQ counts these configurations (klQP and errorQP for \(KL(Q\|P)\)).

  • hellinger, the Hellinger distance [2] in the form of [3], with no factor 1/2:

    \[H(P, Q) = \sqrt{\sum_x \left(\sqrt{p} - \sqrt{q}\right)^2} = \sqrt{2 - 2\rho}, \qquad \rho = \sum_x \sqrt{p\,q},\]

    where \(\rho\) is the Bhattacharyya coefficient. \(H\) lies in \([0, \sqrt 2]\), and is \(\sqrt 2\) times the distance normalized to \([0, 1]\) by the factor 1/2 found elsewhere.

  • bhattacharya, the Bhattacharyya distance [4], with a natural logarithm (in nats, unlike the other quantities): \(B(P, Q) = -\ln \rho\), in \([0, +\infty]\), infinite when the supports of \(P\) and \(Q\) are disjoint.

  • jensen-shannon, the Jensen-Shannon divergence [5] in bits, with \(M = (P + Q)/2\):

    \[JS(P, Q) = \frac{1}{2} KL(P \| M) + \frac{1}{2} KL(Q \| M) \in [0, 1].\]

    It is symmetric and always finite (\(M > 0\) wherever \(P > 0\) or \(Q > 0\)), hence a safer choice than the Kullback-Leibler divergences when the supports differ.

Numerical forms. The computations use forms whose terms are exactly 0 when \(p = q\), so that all the values are exactly 0 for two identical networks, with \(m = (p + q)/2\):

\[KL(P \| Q) = \sum_{x:\, p, q > 0} p \left(\log_2 p - \log_2 q\right), \qquad JS(P, Q) = \frac{1}{2} \sum_x \left( p \left(\log_2 p - \log_2 m\right) + q \left(\log_2 q - \log_2 m\right) \right)\]

(in \(KL\), the configurations with \(p > 0\) and \(q = 0\) are left out of the sum, counted by errorPQ, and make klPQ infinite as above; in \(JS\), a null probability gives a null term), and, since \(\rho = 1 - H^2/2\) when \(P\) and \(Q\) sum to 1,

\[\begin{split}B(P, Q) = \begin{cases} -\ln\left(1 - H^2/2\right) & \text{if } H^2 < 1, \\ -\ln \rho & \text{otherwise.} \end{cases}\end{split}\]

The first form of \(B\) is precise for close networks, where \(\rho\) is rounded to a value near 1: it is computed with log1p (\(\ln(1 + u)\) for a small \(u\)), which keeps the digits of a small \(H^2\) that \(\ln(1 - H^2/2)\) would lose. The second one is precise for distant networks, where \(1 - H^2/2\) loses its digits. A negative rounding residue is replaced by 0.

Methods

compute()

Computes the divergences and distances between the two Bayesian networks.

class pyagrum.GibbsBNdistance(*args)

Class representing a Gibbs-Approximated computation of divergence and distance between BNs

If bhattacharya is +inf, no sample drawn from P had a positive probability in Q, and conversely: the supports are disjoint, or their overlap is too rare to be observed. (In very large networks, joint probabilities may also be rounded to 0.)

GibbsBNdistance(P,Q) -> GibbsBNdistance
Parameters:
GibbsBNdistance(gbnd) -> GibbsBNdistance
Parameters:
Raises:

pyagrum.OperationNotAllowed – If P and Q are not defined on the same variables (same names and same domains)

Scientific notes

GibbsBNdistance computes the estimators of pyagrum.MCBNDistance (formulas, errorPQ, limits and stopping criterion, with the defaults of the section gibbsKL), but from the samples of a Gibbs chain on \(P\) [6] instead of independent samples: no variable is observed, and each step draws again nbrDrawnVar variables (by default \(1 + \lfloor n/10 \rfloor\) for \(n\) variables, chosen at random), each one given its Markov blanket, as in pyagrum.GibbsSampling. The chain starts from a configuration of positive probability, and its first burnIn steps (2000 by default) are discarded.

The samples of the chain are correlated: for the same \(N\), they carry less information than independent samples, and the stopping criterion, which looks at the change brought by the last sample, may stop while the estimate still drifts slowly. When \(P\) has null probabilities, the chain may also miss parts of the support of \(P\). Since the samples of a Bayesian network without evidence can be drawn exactly and independently, pyagrum.MCBNDistance is usually preferable.

Methods

burnIn()

compute()

Computes the divergences and distances between the two Bayesian networks.

continueApproximationScheme()

Continue the approximation scheme.

currentTime()

disableEpsilon()

Disable epsilon as a stopping criterion.

disableMaxIter()

Disable max iterations as a stopping criterion.

disableMaxTime()

Disable max time as a stopping criterion.

disableMinEpsilonRate()

Disable a min epsilon rate as a stopping criterion.

enableEpsilon()

Enable epsilon as a stopping criterion.

enableMaxIter()

Enable max iterations as a stopping criterion.

enableMaxTime()

Enable max time as a stopping criterion.

enableMinEpsilonRate()

Enable a min epsilon rate as a stopping criterion.

epsilon()

history()

The error at the start of each period of the last run: one value every periodSize() iterations, recorded only when the verbosity is enabled (see setVerbosity()).

initApproximationScheme()

Initiate the approximation scheme.

isDrawnAtRandom()

isEnabledEpsilon()

isEnabledMaxIter()

isEnabledMaxTime()

isEnabledMinEpsilonRate()

maxIter()

maxTime()

messageApproximationScheme()

minEpsilonRate()

nbrDrawnVar()

nbrIterations()

periodSize()

remainingBurnIn()

Always 0: the burn-in is done before the first iteration of the scheme.

setBurnIn()

setDrawnAtRandom()

setEpsilon()

setMaxIter()

setMaxTime()

setMinEpsilonRate()

setNbrDrawnVar()

setPeriodSize()

setVerbosity()

startOfPeriod()

stateApproximationScheme()

stopApproximationScheme()

Stop the approximation scheme.

updateApproximationScheme()

Update the approximation scheme.

verbosity()

class pyagrum.MCBNDistance(*args)

Class representing a Monte-Carlo-approximated computation of divergence and distance between BNs

Unlike GibbsBNdistance, each sample is drawn independently (forward sampling in topological order), so there is no Markov chain and no burn-in to configure.

If bhattacharya is +inf, no sample drawn from P had a positive probability in Q, and conversely: the supports are disjoint, or their overlap is too rare to be observed. (In very large networks, joint probabilities may also be rounded to 0.)

MCBNDistance(P,Q) -> MCBNDistance
Parameters:
MCBNDistance(mcbnd) -> MCBNDistance
Parameters:
Raises:

pyagrum.OperationNotAllowed – If P and Q are not defined on the same variables (same names and same domains)

Scientific notes

MCBNDistance estimates the quantities of pyagrum.ExactBNdistance (definitions, logarithm bases, errorPQ) from \(N\) independent samples \(x^{(k)}\) of \(P\), drawn forward in a topological order (logic sampling [7]). With \(p_k = P(x^{(k)})\), \(q_k = Q(x^{(k)})\), \(r_k = q_k / p_k\) and \(m_k = (p_k + q_k)/2\), a sum over the configurations weighted by \(q\) becomes a mean of \(r_k\) (importance weights):

\[\widehat{KL}(P\|Q) = \frac{1}{N} \sum_k \log_2 \frac{p_k}{q_k}, \qquad \widehat{KL}(Q\|P) = \frac{1}{N} \sum_k r_k \log_2 r_k, \qquad \hat\rho = \frac{1}{N} \sum_k \sqrt{r_k},\]
\[\hat H = \sqrt{\max(0,\, 2 - 2\hat\rho)}, \qquad \hat B = -\ln \hat\rho, \qquad \widehat{JS} = \frac{1}{2N} \sum_k \left(\log_2 \frac{p_k}{m_k} + r_k \log_2 \frac{q_k}{m_k}\right) + \frac{1}{2}\left(1 - \frac{1}{N} \sum_k r_k\right).\]

(The last term of \(\widehat{JS}\) is added only when a parameter of \(P\) is null, and \(\widehat{JS}\) is kept in \([0, 1]\).)

A sample with \(q_k = 0\) is counted in errorPQ and in \(N\); it proves that \(KL(P\|Q)\) is infinite (klPQ is then \(+\infty\), as in the exact computation), and its term in \(\widehat{JS}\) is \(\log_2 (p_k/m_k) = 1\). The Hellinger distance uses \(H^2 = 2 - 2\rho\): the configurations where \(p = 0\) add nothing to \(\rho\), so that \(\hat\rho\) estimates it without bias.

Implementation limits. A configuration with \(p = 0\) is never drawn. Hence errorQP stays 0 even when \(KL(Q\|P)\) is infinite. \(\widehat{JS}\) takes the mass \(Q(p = 0)\) into account through its last term, since the mean of the \(r_k\) estimates \(Q(p > 0)\); this term is added only when a parameter of \(P\) is null (otherwise \(Q(p=0) = 0\) exactly), and \(\widehat{JS}\) is kept in \([0, 1]\). \(\widehat{KL}(Q\|P)\), this last term and \(\hat\rho\) rely on the weights \(r_k\), and are imprecise when \(Q\) gives a large probability to configurations that are rare under \(P\).

Stopping criterion. After each sample with \(q_k > 0\), the error is the change of \(\widehat{KL}(P\|Q)\) caused by this sample. The sampling stops when this error, checked every periodSize (200) samples, is below epsilon (\(10^{-5}\)), when its relative change between two checks is below minEpsilonRate (\(10^{-10}\)), or at the limit of iterations (\(10^7\)) or of time. As long as no sample has \(q_k > 0\), the error is \(1/N\), the order of magnitude of the largest mass that \(P\) may plausibly give to the support of \(Q\). bhattacharya is \(+\infty\) if and only if the sampling ends without any such sample: the supports are disjoint, or their overlap is too rare to be observed (or, in very large networks, joint probabilities are rounded to 0). It is a criterion on the stability of \(\widehat{KL}(P\|Q)\) only, not a confidence bound: the other quantities may be less precise.

Methods

compute()

Computes the divergences and distances between the two Bayesian networks.

continueApproximationScheme()

Continue the approximation scheme.

currentTime()

disableEpsilon()

Disable epsilon as a stopping criterion.

disableMaxIter()

Disable max iterations as a stopping criterion.

disableMaxTime()

Disable max time as a stopping criterion.

disableMinEpsilonRate()

Disable a min epsilon rate as a stopping criterion.

enableEpsilon()

Enable epsilon as a stopping criterion.

enableMaxIter()

Enable max iterations as a stopping criterion.

enableMaxTime()

Enable max time as a stopping criterion.

enableMinEpsilonRate()

Enable a min epsilon rate as a stopping criterion.

epsilon()

history()

The error at the start of each period of the last run: one value every periodSize() iterations, recorded only when the verbosity is enabled (see setVerbosity()).

initApproximationScheme()

Initiate the approximation scheme.

isEnabledEpsilon()

isEnabledMaxIter()

isEnabledMaxTime()

isEnabledMinEpsilonRate()

maxIter()

maxTime()

messageApproximationScheme()

minEpsilonRate()

nbrIterations()

periodSize()

remainingBurnIn()

Always 0: the burn-in is done before the first iteration of the scheme.

setEpsilon()

setMaxIter()

setMaxTime()

setMinEpsilonRate()

setPeriodSize()

setVerbosity()

startOfPeriod()

stateApproximationScheme()

stopApproximationScheme()

Stop the approximation scheme.

updateApproximationScheme()

Update the approximation scheme.

verbosity()

In general, pyagrum.MCBNDistance converges better (faster, with a smaller variance) than pyagrum.GibbsBNdistance for a given number of iterations. This is expected: computing a distance between two BNs does not involve any observation (hard evidence) to account for during sampling, so there is no need for the Markov chain machinery (mixing, burn-in) that Gibbs sampling relies on to handle evidence. Drawing independent samples directly from P is both simpler and, here, more efficient.

See also

other pyagrum.lib modules

pyagrum.lib.bn_vs_bn.GraphicalBNComparator for structural comparison of two Bayesian networks.

Structural comparison metrics

pyagrum.StructuralMetrics, the (lower-level) C++ class computing precision, recall, F-score, SHD and SID, used internally by pyagrum.lib.bn_vs_bn.GraphicalBNComparator.