blackjax.mcmc.metrics#
Metric space in which the Hamiltonian dynamic is embedded.
An important particular case (and the most used in practice) of metric for the position space in the Euclidean metric. It is defined by a definite positive matrix \(M\) with fixed value so that the kinetic energy of the hamiltonian dynamic is independent of the position and only depends on the momentum \(p\) [BBLG17].
For a Newtonian hamiltonian dynamic the kinetic energy is given by:
We can also generate a relativistic dynamic [LPH+17].
Classes#
Pure-array description of a low-rank inverse mass matrix. |
Functions#
|
Convert an input metric into a |
|
Hamiltonian dynamic on euclidean manifold with normally-distributed momentum |
|
Euclidean metric with low-rank-modified mass matrix [SCC26]. |
|
Hamiltonian dynamic on Riemannian manifold with normally-distributed momentum. |
Convert an L-BFGS factored inverse-Hessian to a |
Module Contents#
- class LowRankInverseMassMatrix[source]#
Pure-array description of a low-rank inverse mass matrix.
The inverse mass matrix has the form
\[M^{-1} = \operatorname{diag}(\sigma) \bigl(I + U(\Lambda - I)U^\top\bigr) \operatorname{diag}(\sigma)\]where \(\sigma \in \mathbb{R}^d_{>0}\), \(U \in \mathbb{R}^{d \times k}\) has orthonormal columns and \(\Lambda = \operatorname{diag}(\lambda)\).
This is the array-only payload produced by
window_adaptation_low_rank(). Unlike a fully-constructedMetric(whose fields are Python closures that capture these arrays), this NamedTuple is a pure JAX pytree and can be safely transported acrossjax.vmap/jax.pmapboundaries.default_metric()expands this into aMetricat the kernel call site viagaussian_euclidean_low_rank().
- default_metric(metric: MetricTypes) Metric[source]#
Convert an input metric into a
Metricobject following sensible default rules.The metric can be specified in four different ways:
A
Metricobject that implements the full interfaceA
LowRankInverseMassMatrixNamedTuple holding(sigma, U, lam), which is expanded to a fullMetricviagaussian_euclidean_low_rank(). This is the form returned bywindow_adaptation_low_rank()and is safe to transport acrossjax.vmapboundaries.An
Arraywhich is assumed to specify the inverse mass matrix of a static metricA function that takes a coordinate position and returns the mass matrix at that location
- Returns:
A
Metricobject withsample_momentum,kinetic_energy,check_turning, andscalefields.
- gaussian_euclidean(inverse_mass_matrix: blackjax.types.Array) Metric[source]#
Hamiltonian dynamic on euclidean manifold with normally-distributed momentum [Bet13].
The gaussian euclidean metric is a euclidean metric further characterized by setting the conditional probability density \(\pi(momentum|position)\) to follow a standard gaussian distribution. A Newtonian hamiltonian dynamics is assumed.
- Parameters:
inverse_mass_matrix – One or two-dimensional array corresponding respectively to a diagonal or dense mass matrix. The inverse mass matrix is multiplied to a flattened version of the Pytree in which the chain position is stored (the current value of the random variables). The order of the variables should thus match JAX’s tree flattening order, and more specifically that of ravel_pytree. In particular, JAX sorts dictionaries by key when flattening them. The value of each variables will appear in the flattened Pytree following the order given by sort(keys).
- Returns:
momentum_generator – A function that generates a value for the momentum at random.
kinetic_energy – A function that returns the kinetic energy given the momentum.
is_turning – A function that determines whether a trajectory is turning back on itself given the values of the momentum along the trajectory.
- gaussian_euclidean_low_rank(sigma: blackjax.types.Array, U: blackjax.types.Array, lam: blackjax.types.Array) Metric[source]#
Euclidean metric with low-rank-modified mass matrix [SCC26].
The inverse mass matrix has the form
\[M^{-1} = \operatorname{diag}(\sigma) \bigl(I + U(\Lambda - I)U^\top\bigr) \operatorname{diag}(\sigma)\]where \(\sigma \in \mathbb{R}^d_{>0}\) is a diagonal scaling, \(U \in \mathbb{R}^{d \times k}\) has orthonormal columns, and \(\Lambda = \operatorname{diag}(\lambda)\) with \(\lambda > 0\). When \(\lambda = \mathbf{1}\) the metric reduces to a diagonal metric with scale \(\sigma\). All HMC operations are \(O(dk)\), making this efficient when \(k \ll d\).
- Parameters:
sigma – Shape
(d,). Positive diagonal scaling; plays the role of marginal standard deviations.U – Shape
(d, k). Matrix with orthonormal columns spanning the low-rank correction subspace.lam – Shape
(k,). Positive eigenvalues for the low-rank correction.
- Return type:
A
Metricobject whose operations all run in \(O(dk)\).
- gaussian_riemannian(mass_matrix_fn: Callable) Metric[source]#
Hamiltonian dynamic on Riemannian manifold with normally-distributed momentum.
- Parameters:
mass_matrix_fn – A callable that takes a position and returns the mass matrix at that location (positive definite, one or two-dimensional array).
- Returns:
A
Metricobject withsample_momentum,kinetic_energy,check_turning, andscalefields.
- lbfgs_inverse_hessian_to_low_rank_metric(alpha: blackjax.types.Array, beta: blackjax.types.Array, gamma: blackjax.types.Array) LowRankInverseMassMatrix[source]#
Convert an L-BFGS factored inverse-Hessian to a
LowRankInverseMassMatrix.The L-BFGS inverse Hessian is stored in the factored form
\[H^{-1} = \operatorname{diag}(\alpha) + \beta \Gamma \beta^\top\](formula II.1 / II.3 of [ZCGV22]). This adapter rewrites it as a
LowRankInverseMassMatrixwith\[M^{-1} = \operatorname{diag}(\sigma) \bigl(I + U(\Lambda - I)U^\top\bigr) \operatorname{diag}(\sigma), \quad \sigma = \sqrt{\alpha}\]via a compact \(O((2m)^3)\) inner eigendecomposition that avoids the \(O(d^3)\) full eigenproblem:
Set \(D = \operatorname{diag}(\sigma)\) and factor out to obtain \(H^{-1} = D(I + \tilde B \Gamma \tilde B^\top)D\) with \(\tilde B = D^{-1}\beta \in \mathbb{R}^{d \times 2m}\).
QR-decompose \(\tilde B = Q R\) (thin QR, \(Q\) orthonormal \(d \times r\), \(r = \min(d, 2m)\)).
The inner correction satisfies \(\tilde B \Gamma \tilde B^\top = Q (R \Gamma R^\top) Q^\top\), so its eigenvalues are those of the \(r \times r\) matrix \(R \Gamma R^\top\).
Eigendecompose \(R \Gamma R^\top = V \Lambda_c V^\top\) (eigh, \(r \times r\) only).
Return \(U = QV\) (orthonormal eigenvectors, shape \(d \times r\)) and \(\lambda = 1 + \Lambda_c\) (eigenvalues of \(I + \tilde B \Gamma \tilde B^\top\)).
When to use this. Pass the
(alpha, beta, gamma)triple produced bylbfgs_inverse_hessian_factors()to obtain a JAX-pytree-safeLowRankInverseMassMatrixthat can crossjax.vmapboundaries and feed any consumer that accepts the unified representation (HMC, MCLMC, etc.).Note
This function is a pure adapter — it does not alter Pathfinder’s internal sampling path. Wiring this adapter into Pathfinder’s own sampling path is deliberate follow-up work; for now it ships as an adapter with parity tests only.
Warning
Positive-definiteness precondition. The triple
(alpha, beta, gamma)must yield a positive-definite dense formdiag(alpha) + beta @ gamma @ beta.T; this is guaranteed when the triple comes fromlbfgs_inverse_hessian_factors()under a Wolfe-condition line search. Non-positive-definite inputs producelam <= 0silently here and surface as NaN at momentum sampling. Additionally, at float32 near-singular metrics (condition number ≳ 1e7) can resolve the smallest eigenvalue with unreliable sign; for such inputs prefer float64 factors.- Parameters:
alpha – Shape
(d,). Positive diagonal of the inverse Hessian approximation.beta – Shape
(d, 2m). Left factor of the low-rank correction. Whenm = 0(empty L-BFGS history)betahas shape(d, 0)and the adapter returns a pure diagonal metric.gamma – Shape
(2m, 2m). Symmetric inner factor of the low-rank correction.
- Returns:
With
sigma = sqrt(alpha),Uthe(d, r)orthonormal eigenvector matrix, andlam = 1 + eigenvalues(R Γ Rᵀ). Empty-history edge:Uhas shape(d, 0)andlamshape(0,), representing a pure diagonal metric with scalesigma.- Return type:
See also
LowRankInverseMassMatrixTarget representation consumed by
gaussian_euclidean_low_rank().gaussian_euclidean_low_rankFull metric protocol built from the representation.