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.

Bayesian updates from scratch — sequential conjugate inference

Notebook 0.4 — three parameterizations showed that combining two Gaussian factors is addition in natural-parameter space. This notebook follows that thread to its conclusion: an inference loop that absorbs NN Gaussian observations one at a time, equivalent to processing them all at once, in any order, with the same answer to floating-point precision.

That single idea — exponential-family inference reduces to summing natural parameters — underlies the Kalman filter, GP regression, EP, and most of approximate inference. We make it concrete by

  1. writing the linear-Gaussian Bayes rule once, in natural form, leaning on ((5));
  2. running a sequential loop over N=6N=6 noisy observations of an unknown 2D location;
  3. confirming batch = sequential = any-order to ∼10−15\sim 10^{-15};
  4. closing with the punch line: GP regression with a Gaussian likelihood is a single application of the same rule.

Prerequisites: 0.2 — MultivariateNormal API, 0.4 — three parameterizations, 0.5 — Joseph-form covariance update.

Linear-Gaussian Bayes rule, in one breath

The linear-Gaussian observation model from ((1)) gives a Gaussian posterior with three equivalent updates:

Λpost  =  Λprior  +  H⊤R−1H,Λpost μpost  =  Λprior μprior  +  H⊤R−1y.\Lambda_{\rm post} \;=\; \Lambda_{\rm prior} \;+\; H^\top R^{-1} H, \qquad \Lambda_{\rm post}\,\mu_{\rm post} \;=\; \Lambda_{\rm prior}\,\mu_{\rm prior} \;+\; H^\top R^{-1} y.

In natural parameters, recalling η1=Λμ\eta_1 = \Lambda \mu and η2=−12Λ\eta_2 = -\tfrac12 \Lambda from ((2)), the rule is just ((5)) applied to the prior and the likelihood factor:

η1post  =  η1prior+H⊤R−1y,η2post  =  η2prior−12 H⊤R−1H.\eta_1^{\rm post} \;=\; \eta_1^{\rm prior} + H^\top R^{-1} y, \qquad \eta_2^{\rm post} \;=\; \eta_2^{\rm prior} - \tfrac12\, H^\top R^{-1} H.

That’s the entire algorithm. No matrix inverses, no Kalman gains. The mean-cov form ((2)) and Joseph form ((6)) are equivalent recipes for getting back to (μ,Σ)(\mu, \Sigma) when you need to plot or sample.

A 2D location-estimation problem

We’re trying to estimate an unknown 2D location x⋆=(3, −1)x_\star = (3,\,-1) from a sequence of noisy linear measurements. Start with a vague Gaussian prior N(0, 4I)\mathcal{N}(\mathbf{0},\,4I) and convert it to natural form via gaussx.to_natural.

Prior  mu    : [0. 0.]
Prior  Sigma :
 [[4. 0.]
 [0. 4.]]

Prior  eta1  : [0. 0.]
Prior  eta2  :
 [[-0.125 -0.   ]
 [-0.    -0.125]]

A single observation: natural form vs covariance form

We observe the xx-coordinate, y1=H1 x⋆+εy_1 = H_1\, x_\star + \varepsilon with H1=[1, 0]H_1 = [1,\,0] and ε∼N(0,1)\varepsilon \sim \mathcal{N}(0, 1).

Two paths to the posterior — both implementations of the same equation.

PathRecipeCost
NaturalAdd likelihood naturals via ((2)).One matrix add per observation
Mean-cov / KalmanCompute Kalman gain KK, then μ+,Σ+\mu^+, \Sigma^+ via ((2))One innovation solve, one Joseph rebuild

We do both and confirm agreement.

Observed y1 = 3.6498920118351403

Natural posterior mu : [2.91991361 0.        ]
Mean-cov posterior mu: [2.91991361 0.        ]

max |mu_nat  - mu_cov | = 0.00e+00
max |Sig_nat - Sig_cov| = 2.22e-16

Same posterior to ∼10−15\sim 10^{-15}. The natural-form path replaced two matrix solves (innovation + Joseph rebuild) with two matrix adds — the difference compounds as we chain updates.

<Figure size 605x550 with 1 Axes>

Sequential updates: NN factors, one addition at a time

Five additional measurements, each through a different HiH_i (single coordinates and diagonal projections), each with R=0.5R = 0.5. The update rule is just ((2)) applied repeatedly:

η(n)  =  η(n−1)  +  ηnlik,ηnlik  =  (Hn⊤Rn−1yn,    −12 Hn⊤Rn−1Hn).\eta^{(n)} \;=\; \eta^{(n-1)} \;+\; \eta^{\rm lik}_n, \qquad \eta^{\rm lik}_n \;=\; \bigl(H_n^\top R_n^{-1} y_n,\;\; -\tfrac12\, H_n^\top R_n^{-1} H_n\bigr).

Each step is an array sum — no matrix inverses during the recursion. We only invert (via to_expectation) when we want to plot the running posterior.

after obs 2:  mu = [+2.920, -0.159]  sqrt(tr Sigma) = 1.116
after obs 3:  mu = [+2.622, -0.324]  sqrt(tr Sigma) = 0.933
after obs 4:  mu = [+2.988, -0.437]  sqrt(tr Sigma) = 0.765
after obs 5:  mu = [+3.130, -1.041]  sqrt(tr Sigma) = 0.668
after obs 6:  mu = [+2.816, -0.777]  sqrt(tr Sigma) = 0.592

x_true = [3.000, -1.000]
<Figure size 726x660 with 1 Axes>

Batch = sequential = any order

Vector addition is commutative and associative, so the recursion ((3)) doesn’t care in what order we apply the likelihood factors:

ηNpost  =  ηprior  +  ∑i=1Nηilik  =  ηprior  +  ∑i=1Nηπ(i)likfor any permutation π.\eta^{\rm post}_N \;=\; \eta^{\rm prior} \;+\; \sum_{i=1}^{N} \eta^{\rm lik}_i \;=\; \eta^{\rm prior} \;+\; \sum_{i=1}^{N} \eta^{\rm lik}_{\pi(i)} \quad \text{for any permutation } \pi.

This is not a coincidence of Gaussian arithmetic — it’s the defining property of exponential-family inference. Concretely:

max ||eta(seq) - eta(reversed)||           = 1.78e-15
max ||eta(seq) - eta(random [3, 2, 5, 4, 0, 1])|| = 1.78e-15
max ||mu(batch)  - mu(traj last)||          = 0.00e+00
max ||Sig(batch) - Sig(traj last)||         = 0.00e+00

All differences sit at ∼10−15\sim 10^{-15} — the recursion really is permutation-invariant up to round-off.

GP regression is this update

Gaussian-process regression with a Gaussian likelihood is precisely ((2)) applied with H=IH = I (we observe the function directly) and R=σn2IR = \sigma_n^2 I:

Λpost  =  K−1  +  σn−2I,μpost  =  Λpost−1 σn−2 y  =  K (K+σn2I)−1 y.\Lambda_{\rm post} \;=\; K^{-1} \;+\; \sigma_n^{-2} I, \qquad \mu_{\rm post} \;=\; \Lambda_{\rm post}^{-1}\,\sigma_n^{-2}\,y \;=\; K\,(K + \sigma_n^2 I)^{-1}\,y.

The standard GP posterior-mean formula on the right is ((5)) rearranged via the matrix-inversion identity. Different recipe, same equation.

Operationally, gaussx covers the whole loop:

GP log marginal likelihood : -19.557
posterior-mean RMSE vs sin : 0.1561
<Figure size 770x396 with 1 Axes>

Where this update lives in practice

SettingWhat “factor” meansWhere in the curriculum
Kalman filter (part 7)Each observation step = one Gaussian factor folded in via ((2)). The information form is the running natural-parameter sum; the standard form is its mean-cov twin.Information-form filter / smoother.
GP regression (part 3)All training observations are one big factor with H=I,R=σn2IH=I, R=\sigma_n^2 I. Posterior reduces to a single solve ((5)).Exact, sparse, and approximate GP recipes.
Ensemble Kalman / data assimilation (part 7.x)Each ensemble member or observation batch contributes a likelihood factor. Order-independence ((4)) lets you parallelise across sensors.EnKF, particle Kalman, sequential 3D-Var.
EP for non-Gaussian likelihoods (part 6)Each site approximation is a Gaussian factor in natural form. The cavity-tilted-update cycle is ((2)) with cavity = posterior − site, site = projection − cavity.Classification GPs, robust regression.
Conjugate Bayesian linear regressionHH is the design matrix, R=σ2IR = \sigma^2 I. Sequential additions handle streaming data.Recursive least squares as a degenerate Kalman filter.
Distributed inferenceEach worker computes ηwlik\eta^{\rm lik}_w on its shard; the parameter server sums them.Federated GP / Bayesian linear models.

Recap

  • Linear-Gaussian Bayes rule = one application of ((5)) with the likelihood factor read off ((1)). No matrix inverses during the recursion.
  • NN observations = NN additions: ((3)). Mean-cov / Joseph forms are equivalent recipes if you need (μ,Σ)(\mu, \Sigma) at any point.
  • Order-independence ((4)) is exact (modulo round-off) and is the reason exponential-family inference parallelises so cleanly.
  • GP regression with a Gaussian likelihood is one shot of this update ((5)), executed via gaussx.solve on the structured operator K+σn2IK + \sigma_n^2 I.

References

  • Bernardo, J. M. & Smith, A. F. M. (2000). Bayesian Theory. Wiley.
  • Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. §2.3.
  • Rasmussen, C. E. & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press. §2.2.
  • Minka, T. P. (2001). A family of algorithms for approximate Bayesian inference. PhD thesis, MIT.