Comparison of 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:
P (
pyagrum.BayesNet) a Bayesian networkQ (
pyagrum.BayesNet) another Bayesian network to compare with the first one
- ExactBNdistance(ebnd) -> ExactBNdistance
- Parameters:
ebnd (
pyagrum.ExactBNdistance) the exact BNdistance to copy
- 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:
klPQandklQP, 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:
klPQis then \(+\infty\), anderrorPQcounts these configurations (klQPanderrorQPfor \(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\):
(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,
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
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
bhattacharyais+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:
P (
pyagrum.BayesNet) – a Bayesian networkQ (
pyagrum.BayesNet) – another Bayesian network to compare with the first one
- GibbsBNdistance(gbnd) -> GibbsBNdistance
- Parameters:
gbnd (
pyagrum.GibbsBNdistance) – the Gibbs BNdistance to copy
- 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.
Stuart Geman and Donald Geman. Stochastic relaxation, Gibbs distributions, and the Bayesian restoration of images. IEEE Transactions on Pattern Analysis and Machine Intelligence, PAMI-6(6):721–741, 1984. URL: https://doi.org/10.1109/TPAMI.1984.4767596, doi:10.1109/TPAMI.1984.4767596.
Methods
Computes the divergences and distances between the two Bayesian networks. |
|
Continue the approximation scheme. |
|
Disable epsilon as a stopping criterion. |
|
Disable max iterations as a stopping criterion. |
|
Disable max time as a stopping criterion. |
|
Disable a min epsilon rate as a stopping criterion. |
|
Enable epsilon as a stopping criterion. |
|
Enable max iterations as a stopping criterion. |
|
Enable max time as a stopping criterion. |
|
Enable a min epsilon rate as a stopping criterion. |
|
The error at the start of each period of the last run: one value every |
|
Initiate the approximation scheme. |
|
Always 0: the burn-in is done before the first iteration of the scheme. |
|
Stop the approximation scheme. |
|
Update the approximation scheme. |
|
- 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
bhattacharyais+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:
P (
pyagrum.BayesNet) – a Bayesian networkQ (
pyagrum.BayesNet) – another Bayesian network to compare with the first one
- MCBNDistance(mcbnd) -> MCBNDistance
- Parameters:
mcbnd (
pyagrum.MCBNDistance) – the Monte Carlo BNdistance to copy
- 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):
(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.
Max Henrion. Propagating uncertainty in Bayesian networks by probabilistic logic sampling. In John F. Lemmer and Laveen N. Kanal, editors, Uncertainty in Artificial Intelligence 2 (UAI 1986), volume 5 of Machine Intelligence and Pattern Recognition, pages 149–163. North-Holland, 1988. URL: https://doi.org/10.1016/B978-0-444-70396-5.50019-4, doi:10.1016/B978-0-444-70396-5.50019-4.
Methods
Computes the divergences and distances between the two Bayesian networks. |
|
Continue the approximation scheme. |
|
Disable epsilon as a stopping criterion. |
|
Disable max iterations as a stopping criterion. |
|
Disable max time as a stopping criterion. |
|
Disable a min epsilon rate as a stopping criterion. |
|
Enable epsilon as a stopping criterion. |
|
Enable max iterations as a stopping criterion. |
|
Enable max time as a stopping criterion. |
|
Enable a min epsilon rate as a stopping criterion. |
|
The error at the start of each period of the last run: one value every |
|
Initiate the approximation scheme. |
|
Always 0: the burn-in is done before the first iteration of the scheme. |
|
Stop the approximation scheme. |
|
Update the approximation scheme. |
|
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.GraphicalBNComparatorfor 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 bypyagrum.lib.bn_vs_bn.GraphicalBNComparator.