Full-Rank BLR¶
The exact end of the spectrum: a dense precision matrix
\(\Lambda \in \mathbb{R}^{d \times d}\) over flat parameter vectors. With an
exact-Hessian callable the fixed point is the exact Gaussian posterior of a
conjugate model, which makes this variant the reference implementation the
cheaper families are tested against. Solves go through gaussx.solve
(requires the optional gaussx extra).
Transforms¶
blr_full_rank_for_loss(learning_rate: float = 0.01, prior_precision: float = 0.0001, prior_mean: jnp.ndarray | None = None, hessian_estimator: str | Callable = 'ggn', damping: float = 1e-06, solver: Solver | None = None) -> optax.GradientTransformation
¶
Full-rank BLR accepting standard loss gradients.
Wraps blr_full_rank: negates incoming
gradients (\(g_{\text{loglik}} = -g_{\text{loss}}\)) and, for callable
Hessian estimators, negates the returned matrix so the inner BLR
transform receives log-likelihood Hessians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Step size rho in (0, 1]. |
0.01
|
prior_precision
|
float
|
Scalar multiplied by I to form Lambda_0. |
0.0001
|
prior_mean
|
ndarray | None
|
Prior mean vector (d,), or None for zeros. |
None
|
hessian_estimator
|
str | Callable
|
|
'ggn'
|
damping
|
float
|
Additive damping epsilon * I. |
1e-06
|
solver
|
Solver | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Source code in src/optax_bayes/_src/wrappers.py
blr_full_rank(learning_rate: float = 0.01, prior_precision: float = 0.0001, prior_mean: jnp.ndarray | None = None, hessian_estimator: str | Callable = 'ggn', damping: float = 1e-06, solver: Solver | None = None) -> optax.GradientTransformation
¶
Full-rank Gaussian BLR as an optax transform.
Implements the Bayesian Learning Rule (Khan & Rue, 2023) with a
full-rank Gaussian variational family
\(q(\theta) = \mathcal{N}(m, \Lambda^{-1})\) over flat parameter
vectors \(\theta \in \mathbb{R}^d\). Optimizer state stores the
precision matrix \(\Lambda\) ((d, d)) and the natural mean
\(\eta = \Lambda m\) ((d,)), updated as
with \(g_t\) the log-likelihood gradient and \(H_t \preceq 0\) the
Hessian estimate. With the exact Hessian, the fixed point is the
exact Gaussian posterior of a conjugate model. The state initialises
its mean at the params passed to init; prior_mean and
prior_precision anchor every update.
Uses gaussx.solve for the precision solve at each step (requires
the optional gaussx extra).
This API expects log-likelihood gradients. For standard loss
minimisation, use
blr_full_rank_for_loss instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Step size rho in (0, 1]. |
0.01
|
prior_precision
|
float
|
Scalar multiplied by I to form Lambda_0. |
0.0001
|
prior_mean
|
ndarray | None
|
Prior mean vector (d,), or None for zeros. |
None
|
hessian_estimator
|
str | Callable
|
|
'ggn'
|
damping
|
float
|
Additive damping epsilon * I after each precision update. |
1e-06
|
solver
|
Solver | None
|
A |
None
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the optional |
Source code in src/optax_bayes/_src/full_rank.py
Newton's method¶
Classic damped Newton falls out of full-rank BLR at \(\rho = 1\) with a near-flat prior and an exact Hessian.
newton_for_loss(loss_hessian_fn: Callable, damping: float = 1e-06, solver: Solver | None = None) -> optax.GradientTransformation
¶
Newton's method for loss minimisation.
Wraps newton: accepts standard loss
gradients and a loss Hessian function, negating both internally to
match the BLR log-likelihood convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_hessian_fn
|
Callable
|
Callable |
required |
damping
|
float
|
Additive damping epsilon * I for numerical safety. |
1e-06
|
solver
|
Solver | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Source code in src/optax_bayes/_src/presets.py
newton(hessian_fn: Callable, damping: float = 1e-06, solver: Solver | None = None) -> optax.GradientTransformation
¶
Newton's method as full-rank BLR.
Equivalent to BLR with \(\rho = 1\), a near-flat prior, and an exact Hessian, recovering the classic update
Expects log-likelihood gradients. For standard loss
minimisation, use newton_for_loss
instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hessian_fn
|
Callable
|
Callable |
required |
damping
|
float
|
Additive damping epsilon * I for numerical safety. |
1e-06
|
solver
|
Solver | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
GradientTransformation
|
An |
Source code in src/optax_bayes/_src/presets.py
Posterior extraction & sampling¶
get_posterior_full_rank(state: BLRFullRankState, solver: Solver | None = None) -> tuple[jnp.ndarray, jnp.ndarray]
¶
Extract the approximate posterior from full-rank BLR state.
Returns the mean and covariance of
\(q(\theta) = \mathcal{N}(\Lambda^{-1}\eta,\ \Lambda^{-1})\),
using gaussx.solve for the mean and gaussx.inv for the
covariance (requires the optional gaussx extra).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
BLRFullRankState
|
A |
required |
solver
|
Solver | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple |
ndarray
|
covariance is (d, d). |
Source code in src/optax_bayes/_src/posterior.py
sample_posterior_full_rank(state: BLRFullRankState, key: jax.Array) -> jnp.ndarray
¶
Draw one reparameterised sample from the full-rank posterior.
Samples \(\theta \sim \mathcal{N}(m, \Lambda^{-1})\) via the Cholesky factor \(L L^\top = \Lambda\) of the precision:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
BLRFullRankState
|
A |
required |
key
|
Array
|
PRNG key. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Sampled parameter vector, shape (d,). |
Source code in src/optax_bayes/_src/sampling.py
State¶
BLRFullRankState
¶
Bases: NamedTuple
Natural-parameter state for \(q(\theta) = \mathcal{N}(m, \Lambda^{-1})\), \(\theta \in \mathbb{R}^d\).
Attributes:
| Name | Type | Description |
|---|---|---|
precision |
Array
|
Precision matrix \(\Lambda\), shape |
nat_mean |
Array
|
Natural mean \(\eta = \Lambda m\), shape |
count |
Array
|
Scalar step counter. |