Kullback-Leibler divergence estimation¶
Definition¶
Following the notation conventions, let \(p\) and \(q\) be Gaussian mixture densities with \(K_p\) and \(K_q\) components:
and
where \(x\in\mathbb{R}^d\), means are in \(\mathbb{R}^d\), and covariances are positive-definite \(d\times d\) matrices. Component probabilities satisfy
Then the Kullback-Leibler (KL) divergence from \(p\) to \(q\) is defined as
Throughout this library, mathematical expressions use \(D_{\mathrm{KL}}(p \| q)\); “KL divergence” is the abbreviation used in prose. All logarithms are natural, so divergence values are in nats. The order matters: the expectation is under the first distribution.
For Gaussian mixtures, this can be expressed more explicitly as
Mixture KL generally has no closed-form expression. The Gaussian-to-Gaussian
case does have an exact formula, available through ClosedForm with Gaussian
objects. Mixture comparisons can instead use sampling or deterministic
approximations. There are various approaches to estimating the KL
divergence between Gaussian mixtures, including Monte Carlo sampling,
variational approximations, and deterministic sigma-point methods
1.
Monte Carlo estimate and uncertainty¶
For \(N\) independent observations \(x_n\) drawn from \(p\), the fixed-sample estimate is
For \(N\geq2\), its estimated standard error is
With stratified draws, let \(n_k\) be the allocated count for component \(k\) of \(p\) and \(x_{k,n}\) its observations. The integration weight per observation is \(\pi_{p,k}/n_k\), giving
The log densities here are full mixture densities. When every positive-weight component has at least two observations, the estimated standard error is
where \(s_k^2\) is the unbiased sample variance of the log-density ratios in component \(k\). These errors describe numerical integration with fixed distributions, not uncertainty in fitted distribution parameters. Arbitrary integration weights without a supported sampling design have no standard error. An estimate can be negative even though exact KL is nonnegative; sampled values are not clipped.
Example¶
Use the kl_divergence function
to estimate KL between two Gaussian mixtures:
import gmm_divergence as gd
p = gd.GaussianMixture.from_components( # (1)!
components=[
gd.Gaussian.univariate(mean=0.0, variance=1.0),
gd.Gaussian.univariate(mean=1.0, variance=1.0),
]
)
q = gd.GaussianMixture.from_components(
components=[
gd.Gaussian.univariate(mean=0.5, variance=1.0),
gd.Gaussian.univariate(mean=2.5, variance=0.5),
]
)
kl_estimate = gd.kl_divergence(
p, q, estimator=gd.divergence.MonteCarlo(sampling=gd.sampling.Draw(n_samples=10_000, rng=9126))
)
assert abs(kl_estimate.value - 0.32286) < 1e-5
-
This defines \(p\) as a one-feature (\(d=1\)) Gaussian mixture with two equally weighted components. In \(\mathcal{N}(x;\mu,\Sigma)\), the last argument is variance when \(d=1\):
\[ p(x) = 0.5 \mathcal{N}(x;0.0,1.0) + 0.5 \mathcal{N}(x;1.0,1.0). \]Simple constructors
For simple one-dimensional examples,
GaussianMixture.from_componentstogether withGaussian.univariateis usually the clearest way to define Gaussian mixtures.The
GaussianMixtureconstructor remains useful when you already have weight, mean, and covariance arrays.
Other methods
The kl_divergence function also supports other estimation methods. See
the divergence API reference for details.
Sampling configuration¶
Monte Carlo sampling is configured with explicit sample specifications:
import gmm_divergence as gd
p = gd.GaussianMixture.from_components([
gd.Gaussian.univariate(mean=0.0, variance=1.0),
gd.Gaussian.univariate(mean=1.0, variance=1.0),
])
drawn = gd.divergence.MonteCarlo(sampling=gd.sampling.Draw(n_samples=10_000, rng=9126))
reused = gd.divergence.MonteCarlo(sampling=gd.sampling.Samples(p.sample(10_000, rng=9126)))
stratified = gd.divergence.MonteCarlo(sampling=gd.sampling.Stratified(n_samples=10_000, rng=9126))
gd.sampling.Draw is the default and supports Gaussian and GaussianMixture sources.
gd.sampling.Samples is useful when comparing several methods on exactly the same
reference samples. gd.sampling.Stratified allocates fixed sample counts to
positive-weight components instead of relying on random component counts; a
single Gaussian is treated as a one-component mixture.
Stratified standard errors require at least two observations from every
positive-weight component. When a component has only one observation, the KL
estimate is still returned, while result.monte_carlo.standard_error is
None. Precomputed SampleBatch records retain the integration design and
produce the same estimate and uncertainty when reused with Samples.
-
Hershey, John R., and Peder A. Olsen. "Approximating the Kullback Leibler divergence between Gaussian mixture models." 2007 IEEE International Conference on Acoustics, Speech and Signal Processing-ICASSP'07. Vol. 4. IEEE, 2007. ↩