mgsl¶
mgsl is an implementation of the Metric Gaussian Stochastic Localisation (MGSL) algorithm,
written in jax. Broadly, MGSL can be understood as a variational inference (VI) method that produces
independent posterior samples. The approximation to the posterior includes all correlations, and
does not assume any particular variational family. As such, MGSL is particularly well suited
to massive-scale inference problems where the posterior is non-Gaussian, but still unimodal.
The algorithm operates by running localisation "paths". Each of these paths is independent, and produces a single posterior sample.
In a bit more detail, each path follows the observation process
\(\mathbf{Y}_\tau = \tau \mathbf{X} + \mathbf{W}_\tau\) where \(\mathbf{X} \sim p(\mathbf{x} \mid \mathbf{d})\)
is a currently unknown posterior sample, \(\mathbf{W}_\tau\) is a standard Brownian motion process.
The observation time \(\tau\) grows until \(\mathbf{Y}_\tau / \tau\) converges to \(\mathbf{X}\).
Given \(\mathbf{Y}_\tau = \mathbf{y}\), the
unknown follows the localising distribution
\(\pi_\tau(\mathbf{x} \mid \mathbf{y}) \propto p(\mathbf{x} \mid \mathbf{d})\,
\mathcal N(\mathbf{y} \mid \tau \mathbf{x}, \tau \mathbf{I})\). At every step mgsl approximates \(\pi_\tau\)
as a Gaussian, with parameters determined by a matrix-free MGVI fit, and then increments \(\mathbf{y}\) as if
\(\mathbf{X}\) were drawn from that Gaussian.
The model must be setup in a particular way to use mgsl. Namely, the parameters must be a single
vector with independent standard normal priors on all entries. Typically this is done by using transformations
to obtain the priors of interest. The likelihood must also provide its Fisher metric in terms of matrix-vector
products. mgsl can work with any jax model, and also works out-of-the-box with nifty.re models through a provided adaptor.
Installation¶
or the latest version from GitHub:
pip install "git+https://github.com/TomHilder/mgsl.git"
# or
uv add "mgsl @ git+https://github.com/TomHilder/mgsl.git"
(or of course by ssh instead).
jax is a runtime dependency. nifty.re is required if you wish to use the adaptor.
Quick start¶
A simple linear model with Gaussian noise:
from dataclasses import replace
import jax
import jax.numpy as jnp
import mgsl
jax.config.update("jax_enable_x64", True) # recommended: tighter solver tolerances
n_latent, n_data, sigma = 3, 20, 0.1
A = jax.random.normal(jax.random.key(0), (n_data, n_latent))
data = A @ jnp.ones(n_latent) + sigma * jax.random.normal(jax.random.key(1), (n_data,))
def forward(x): # latent x ~ N(0, I) -> prediction of the data
return A @ x
lik = mgsl.gaussian(forward, data, noise_std=sigma)
options = mgsl.MGVIOptions(
n_samples_mgvi=1, # residual draws per local mgvi fit (doubled in practice via mirror samples)
n_iter_mgvi=1, # mgvi rounds per localisation step: (draw residuals, Newton) per step
newton=mgsl.NewtonOptions(maxiter=5),
)
# Initialise from a longer MGVI fit
fit = mgsl.mgvi_fit(jax.random.key(3), lik, jnp.zeros(n_latent), replace(options, n_iter_mgvi=10))
schedule = mgsl.Schedule(n_paths=256, tau_start=1.0, tau_final=1e5, n_tau=32)
result = mgsl.sample(jax.random.key(4), lik, fit.mean, schedule, options)
result.samples # (256, 3) NumPy array: posterior samples
result.diagnostics.ess_fraction # (256, 31): local ESS fraction per path and step
result.failures.totals() # solver failure counts (always recorded)
There are no defaults for the schedule or for the number of samples and rounds of the local
fits: they depend on the problem. This is an active research project and we currently do not
have a confident enough understanding of the algorithm to make many recommendations here.
The user guide goes into some detail in terms of diagnostics
however, and also covers nifty.re models, resuming runs, batches and devices, and power-user
options.