Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Exponential Family Gaussians

This notebook demonstrates the gaussx exponential family module -- working with Gaussians in natural parameter form.

What you’ll learn:

  1. The exponential family form of the Gaussian: natural parameters η1\eta_1, η2\eta_2
  2. Converting between mean/covariance and natural parameters
  3. Log-partition function, sufficient statistics, Fisher information
  4. KL divergence via Bregman divergence in natural parameter space
  5. Why natural parameters are useful (conjugate updates, message passing)

1. Background

Any member of the exponential family can be written as:

q(x∣η)=h(x)exp⁡ ⁣(η⊤T(x)−A(η))q(x \mid \eta) = h(x) \exp\!\bigl(\eta^\top T(x) - A(\eta)\bigr)

For a multivariate Gaussian with mean μ and precision Λ=Σ−1\Lambda = \Sigma^{-1}, the natural parameters are:

η1=Λμ,η2=−12Λ\eta_1 = \Lambda \mu, \qquad \eta_2 = -\tfrac{1}{2}\Lambda

The sufficient statistics are T(x)=[x,  xx⊤]T(x) = [x,\; x x^\top], and the log-partition function is:

A(η)=−14 η1⊤η2−1η1−12log⁡∣−2η2∣+N2log⁡(2π)A(\eta) = -\tfrac{1}{4}\,\eta_1^\top \eta_2^{-1} \eta_1 - \tfrac{1}{2}\log\lvert -2\eta_2 \rvert + \tfrac{N}{2}\log(2\pi)

This encodes the normalization constant. Everything in gaussx’s _expfam module is built on these identities.

The Gaussian is the maximum-entropy distribution for a given mean and covariance (Jaynes, 1957), which makes its exponential family form central to several inference frameworks:

  • Expectation propagation (Minka, 2001) — message passing operates directly in natural parameter space, where site updates are additive.
  • Natural gradient methods (Amari, 1998) — the Fisher information metric gives the steepest descent direction in distribution space, and natural parameters make the Fisher matrix readily available.
  • Variational inference (Wainwright & Jordan, 2008) — conjugate- computation variational inference exploits natural parameter additivity to perform closed-form coordinate updates.

2. Setup

We will work with a simple 3D Gaussian throughout.

mu = [ 1.  -0.5  2. ]
Sigma =
 [[2.  0.5 0.3]
 [0.5 1.  0.1]
 [0.3 0.1 1.5]]
Lambda =
 [[ 0.58546169 -0.28290766 -0.09823183]
 [-0.28290766  1.14341847 -0.01964637]
 [-0.09823183 -0.01964637  0.68762279]]

3. Construction

There are three ways to build a GaussianExpFam.

eta1 (from_mean_cov) = [ 0.53045187 -0.89390963  1.28683694]
eta1 (from_mean_prec) = [ 0.53045187 -0.89390963  1.28683694]
eta1 (manual)         = [ 0.53045187 -0.89390963  1.28683694]

eta2 (from_mean_cov) =
 [[-0.29273084  0.14145383  0.04911591]
 [ 0.14145383 -0.57170923  0.00982318]
 [ 0.04911591  0.00982318 -0.34381139]]
eta2 (from_mean_prec) =
 [[-0.29273084  0.14145383  0.04911591]
 [ 0.14145383 -0.57170923  0.00982318]
 [ 0.04911591  0.00982318 -0.34381139]]
eta2 (manual)         =
 [[-0.29273084  0.14145383  0.04911591]
 [ 0.14145383 -0.57170923  0.00982318]
 [ 0.04911591  0.00982318 -0.34381139]]
All three constructors produce identical natural parameters.

4. Conversions

Round-trip: natural →\to expectation →\to natural.

Original mu   = [ 1.  -0.5  2. ]
Recovered mu  = [ 1.  -0.5  2. ]
Match: True

Original Sigma =
 [[2.  0.5 0.3]
 [0.5 1.  0.1]
 [0.3 0.1 1.5]]
Recovered Sigma =
 [[2.  0.5 0.3]
 [0.5 1.  0.1]
 [0.3 0.1 1.5]]
Match: True
eta1 (to_natural) = [ 0.53045187 -0.89390963  1.28683694]
eta1 (object)     = [ 0.53045187 -0.89390963  1.28683694]
Match: True

eta2 (to_natural) =
 [[-0.29273084  0.14145383  0.04911591]
 [ 0.14145383 -0.57170923  0.00982318]
 [ 0.04911591  0.00982318 -0.34381139]]
eta2 (object)     =
 [[-0.29273084  0.14145383  0.04911591]
 [ 0.14145383 -0.57170923  0.00982318]
 [ 0.04911591  0.00982318 -0.34381139]]
Match: True

5. Log-partition function

The log-partition A(η)A(\eta) encodes the normalization constant. We verify the gaussx implementation against a manual computation.

A(eta) [gaussx]: 4.9994211997
A(eta) [manual]: 4.9994211997
Match: True

6. Sufficient statistics

For the Gaussian, T(x)=[x,  xx⊤]T(x) = [x,\; x x^\top].

x = [ 0.5 -1.   0.3]
T_1(x) = x = [ 0.5 -1.   0.3]
T_2(x) = x x^T =
 [[ 0.25 -0.5   0.15]
 [-0.5   1.   -0.3 ]
 [ 0.15 -0.3   0.09]]
Matches outer product: True
Batch input shape: (3, 3)
T_1 shape: (3, 3)
T_2 shape: (3, 3, 3)
  Sample 0: outer product match = True
  Sample 1: outer product match = True
  Sample 2: outer product match = True

7. Fisher information

For a Gaussian, the Fisher information in the natural parameterization equals the precision matrix Λ=Σ−1\Lambda = \Sigma^{-1}.

More generally, for any exponential family the Fisher information is the Hessian of the log-partition function:

Fij=∂2A(η)∂ηi ∂ηjF_{ij} = \frac{\partial^2 A(\eta)}{\partial \eta_i \,\partial \eta_j}

This connects the geometry of the natural parameter space to the curvature of A(η)A(\eta), which is always convex (Barndorff-Nielsen, 1978).

Fisher info =
 [[ 0.58546169 -0.28290766 -0.09823183]
 [-0.28290766  1.14341847 -0.01964637]
 [-0.09823183 -0.01964637  0.68762279]]
Precision   =
 [[ 0.58546169 -0.28290766 -0.09823183]
 [-0.28290766  1.14341847 -0.01964637]
 [-0.09823183 -0.01964637  0.68762279]]
Match: True

8. KL divergence

gaussx.kl_divergence(q, p) computes KL(q∥p)\text{KL}(q \| p) via the Bregman divergence in natural parameter space:

KL(q∥p)=A(ηp)−A(ηq)−(ηp−ηq)⊤∇A(ηq)\text{KL}(q \| p) = A(\eta_p) - A(\eta_q) - (\eta_p - \eta_q)^\top \nabla A(\eta_q)

The Bregman divergence interpretation means KL can be computed using only the log-partition function and its gradient — no explicit matrix inversions needed beyond what is in the natural-to-expectation conversion. This is computationally advantageous when the precision has structure (e.g. Kronecker, block-diagonal, or sparse).

We verify against the standard formula:

KL(q∥p)=12[tr(Σp−1Σq)+(μp−μq)⊤Σp−1(μp−μq)−N+log⁡∣Σp∣∣Σq∣]\text{KL}(q \| p) = \tfrac{1}{2}\bigl[ \text{tr}(\Sigma_p^{-1}\Sigma_q) + (\mu_p - \mu_q)^\top \Sigma_p^{-1}(\mu_p - \mu_q) - N + \log\tfrac{|\Sigma_p|}{|\Sigma_q|} \bigr]
KL(q || p) [gaussx]:   7.2985079681
KL(q || p) [standard]: 7.2985079681
Match: True

9. Why natural parameters?

In natural parameter space, conjugate updates are just addition. When we combine a Gaussian prior with a Gaussian likelihood site, the posterior natural parameters are the sum of the prior and site natural parameters:

ηpost=ηprior+ηsite\eta_{\text{post}} = \eta_{\text{prior}} + \eta_{\text{site}}

This makes message passing and variational inference extremely simple: no matrix inversions are needed for the update itself.

Prior mean = [ 1.  -0.5  2. ]
Site eta1 = [12. -3. 21.]
Posterior mean = [ 1.19475983 -0.31540861  2.09569557]
Posterior cov  =
 [[0.09454148 0.00240175 0.00087336]
 [0.00240175 0.08980037 0.00018715]
 [0.00087336 0.00018715 0.09357455]]
Expected posterior mean = [ 1.19475983 -0.31540861  2.09569557]
Mean match: True

Expected posterior cov =
 [[0.09454148 0.00240175 0.00087336]
 [0.00240175 0.08980037 0.00018715]
 [0.00087336 0.00018715 0.09357455]]
Cov match: True

10. Summary

Conceptgaussx function
Build from mean/covGaussianExpFam.from_mean_cov(mu, Sigma)
Build from mean/precGaussianExpFam.from_mean_prec(mu, Lambda)
Natural →\to expectationgaussx.to_expectation(q)
Expectation →\to naturalgaussx.to_natural(mu, Sigma)
Log-partition A(η)A(\eta)gaussx.log_partition(q)
Sufficient statisticsgaussx.sufficient_stats(x)
Fisher informationgaussx.fisher_info(q)
KL divergencegaussx.kl_divergence(q, p)

The key takeaway: natural parameters turn Bayesian updates into simple addition, making them the natural choice for message passing, variational inference, and conjugate update algorithms.

References

  • Amari, S. (1998). Natural gradient works efficiently in learning. Neural Computation, 10(2), 251-276.
  • Barndorff-Nielsen, O. (1978). Information and Exponential Families in Statistical Theory. Wiley.
  • Jaynes, E. T. (1957). Information theory and statistical mechanics. Physical Review, 106(4), 620-630.
  • Minka, T. P. (2001). A Family of Algorithms for Approximate Bayesian Inference. PhD thesis, MIT.
  • Wainwright, M. J. & Jordan, M. I. (2008). Graphical models, exponential families, and variational inference. Foundations and Trends in Machine Learning, 1(1-2), 1-305.