Gauss API¶
pyrox_gauss holds the Gaussian NumPyro distributions whose covariance or precision is a gaussx operator. They moved here from gaussx with their names, constructors and numerics unchanged (#453), so moving to them is an import change. pyrox_gauss imports gaussx and NumPyro and never imports pyrox; the model packages build these distributions from the hyperparameters they sample.
Multivariate normals¶
AbstractMultivariateNormal
¶
Bases: Distribution
Common surface of MultivariateNormal and MultivariateNormalPrecision.
x ~ 𝒩(μ, Σ), Λ = Σ⁻¹
A subclass stores one native operator: the covariance Σ
(MultivariateNormal) or the precision Λ (MultivariateNormalPrecision).
The other one is the lazy gaussx.inv of it. Code written against this
base works for either parameterisation:
covariance_operator/precision_operator: lineax operators, one native and one a lazy inverse.covariance_matrix/precision_matrix/scale_tril: dense arrays with numpyro's names and batch broadcasting. They materialise D_x × D_x matrices, so they are for small problems and interop.mean,log_prob,kl.
Requires the numpyro optional extra
(pip install "gaussx[numpyro]").
Examples:
Both parameterisations expose the same accessors:
>>> import jax.numpy as jnp
>>> import lineax as lx
>>> from pyrox_gauss import (
... AbstractMultivariateNormal,
... MultivariateNormal,
... MultivariateNormalPrecision,
... )
>>> covariance = lx.MatrixLinearOperator(
... 2.0 * jnp.eye(2), lx.positive_semidefinite_tag
... )
>>> precision = lx.MatrixLinearOperator(
... 0.5 * jnp.eye(2), lx.positive_semidefinite_tag
... )
>>> p = MultivariateNormal(jnp.zeros(2), covariance)
>>> q = MultivariateNormalPrecision(jnp.zeros(2), precision)
>>> isinstance(q, AbstractMultivariateNormal)
True
>>> bool(jnp.allclose(p.precision_matrix, q.precision_matrix))
True
Equal Gaussians have zero KL, whatever the parameterisation:
Batched means broadcast the dense accessors:
>>> batched = MultivariateNormal(jnp.zeros((3, 2)), covariance)
>>> batched.covariance_matrix.shape, batched.log_prob(jnp.ones(2)).shape
((3, 2, 2), (3,))
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_base.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
covariance_operator: lx.AbstractLinearOperator
property
¶
The covariance Σ as a lineax operator.
precision_operator: lx.AbstractLinearOperator
property
¶
The precision Λ = Σ⁻¹ as a lineax operator.
log_prob(value: Float[Array, '*batch N']) -> Float[Array, '*batch']
¶
Log-density log 𝒩(x; μ, Σ), broadcast over the batch shape.
log 𝒩(x; μ, Σ) = −½ (D_x log 2π + log det Σ + rᵀ Σ⁻¹ r),
r = x − μ
Algorithm
- Form the residuals r = x − μ, broadcasting x against μ.
- Flatten the leading axes and evaluate one event at a time.
- Restore the leading shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Float[Array, '*batch N']
|
Points x, with any leading shape that broadcasts against the batch shape. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
One log-density per broadcast batch element. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_base.py
mean() -> Float[Array, '*batch N']
¶
covariance_matrix() -> Float[Array, '*batch N N']
¶
Dense Σ, broadcast over the batch shape.
precision_matrix() -> Float[Array, '*batch N N']
¶
Dense Λ = Σ⁻¹, broadcast over the batch shape.
scale_tril() -> Float[Array, '*batch N N']
¶
Dense lower Cholesky factor L of Σ = L Lᵀ (numpyro's name).
kl(other: AbstractMultivariateNormal) -> Float[Array, '*batch']
¶
KL(first ‖ second): KL[self ‖ other] in closed form.
KL[p ‖ q] = ½ (tr(Σ_q⁻¹ Σ_p) + (μ_q − μ_p)ᵀ Σ_q⁻¹ (μ_q − μ_p) − D_x
+ log det Σ_q − log det Σ_p)
with p = self and q = other. Any mix of covariance and precision
parameterisations works. A precision-parameterised other needs no
solve for tr(Σ_q⁻¹ Σ_p) or the quadratic term, because the inverse of
its lazy covariance is its precision. Batch shapes broadcast as in
numpyro's kl_divergence. Unlike
numpyro.distributions.kl_divergence, it uses the closed form
whatever the solver strategies are.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
AbstractMultivariateNormal
|
The second Gaussian q, with the same event shape. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
The KL divergence, with the broadcast batch shape. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_base.py
MultivariateNormal
¶
Bases: AbstractMultivariateNormal
Multivariate normal 𝒩(μ, Σ) whose covariance Σ is a lineax operator.
x ~ 𝒩(μ, Σ)
log 𝒩(x; μ, Σ) = −½ (D_x log 2π + log det Σ + (x − μ)ᵀ Σ⁻¹ (x − μ))
Covariance-parameterised member of AbstractMultivariateNormal, which
supplies the shared accessors (covariance_operator,
precision_operator, covariance_matrix, scale_tril, kl, …).
Unlike numpyro.distributions.MultivariateNormal, which needs dense
arrays, it accepts any lineax.AbstractLinearOperator as Σ, so the
log-density, sampling and entropy use gaussx structural dispatch for
structured covariances (Kronecker, block-diagonal, low-rank, diagonal,
…).
Requires the numpyro optional extra
(pip install "gaussx[numpyro]").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loc
|
Float[Array, '*batch N']
|
Mean μ; leading axes are the batch shape. |
required |
cov_operator
|
AbstractLinearOperator
|
Covariance Σ, shared by every batch element. |
required |
solver
|
AbstractSolverStrategy | None
|
Solver strategy for the solves and log-determinants of Σ.
Defaults to |
None
|
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Examples:
A standard normal in three dimensions:
>>> import jax
>>> import jax.numpy as jnp
>>> import lineax as lx
>>> from pyrox_gauss import MultivariateNormal
>>> covariance = lx.MatrixLinearOperator(
... jnp.eye(3), lx.positive_semidefinite_tag
... )
>>> d = MultivariateNormal(jnp.zeros(3), covariance)
>>> round(float(d.log_prob(jnp.ones(3))), 4)
-4.2568
Draws carry the sample shape in front of the event shape:
A structured covariance gives the same entropy as its dense matrix:
>>> diagonal = MultivariateNormal(
... jnp.zeros(3), lx.DiagonalLinearOperator(jnp.ones(3))
... )
>>> bool(jnp.allclose(diagonal.entropy(), d.entropy()))
True
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
covariance_operator: lx.AbstractLinearOperator
property
¶
The covariance Σ (native; the same object as cov_operator).
precision_operator: lx.AbstractLinearOperator
property
¶
The precision Λ = Σ⁻¹: the lazy gaussx.inv of cov_operator.
sample(key: jax.Array | None, sample_shape: tuple[int, ...] = ()) -> Float[Array, '*batch N']
¶
Draw x = μ + Σ^½ z with z ~ 𝒩(0, I), through gaussx.sample_mvn.
x = μ + Σ^½ z, z ~ 𝒩(0, I)
gaussx.sample_mvn dispatches on the structure of Σ (Kronecker,
low-rank, scalar multiples, …) and takes a symmetric square root at
the dense fallback, so a semi-definite Σ gives exact finite draws
instead of a NaN Cholesky.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array | None
|
PRNG key. |
required |
sample_shape
|
tuple[int, ...]
|
Leading shape of the draws. |
()
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch N']
|
Draws of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn.py
variance() -> Float[Array, '*batch N']
¶
Marginal variances diag(Σ), broadcast over the batch shape.
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn.py
entropy() -> Float[Array, '*batch']
¶
Entropy ℍ[𝒩(μ, Σ)], one entry per batch element.
ℍ[𝒩(μ, Σ)] = ½ (D_x (1 + log 2π) + log det Σ)
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
The entropy, broadcast over the batch shape. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn.py
MultivariateNormalPrecision
¶
Bases: AbstractMultivariateNormal
Multivariate normal 𝒩(μ, Λ⁻¹) whose precision Λ = Σ⁻¹ is a lineax operator.
x ~ 𝒩(μ, Λ⁻¹)
log 𝒩(x; μ, Λ⁻¹) = −½ (D_x log 2π − log det Λ + (x − μ)ᵀ Λ (x − μ))
The natural parameterisation for many inference algorithms (message
passing, variational inference in natural coordinates): the log-density
needs a matrix-vector product with Λ and no solve. Precision-parameterised
member of AbstractMultivariateNormal, which supplies the shared
accessors (covariance_operator, precision_operator,
covariance_matrix, scale_tril, kl, …).
Requires the numpyro optional extra
(pip install "gaussx[numpyro]").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loc
|
Float[Array, '*batch N']
|
Mean μ; leading axes are the batch shape. |
required |
prec_operator
|
AbstractLinearOperator
|
Precision Λ = Σ⁻¹, shared by every batch element. |
required |
solver
|
AbstractSolverStrategy | None
|
Solver strategy for the log-determinant of Λ. Defaults to
|
None
|
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Examples:
A precision of 2 I in three dimensions:
>>> import jax
>>> import jax.numpy as jnp
>>> import lineax as lx
>>> from pyrox_gauss import MultivariateNormalPrecision
>>> precision = lx.MatrixLinearOperator(
... 2.0 * jnp.eye(3), lx.positive_semidefinite_tag
... )
>>> d = MultivariateNormalPrecision(jnp.zeros(3), precision)
>>> round(float(d.log_prob(jnp.ones(3))), 4)
-4.7171
The marginal variances are diag(Λ⁻¹):
Draws carry the sample shape in front of the event shape:
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_prec.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
covariance_operator: lx.AbstractLinearOperator
property
¶
The covariance Σ = Λ⁻¹: the lazy gaussx.inv of prec_operator.
precision_operator: lx.AbstractLinearOperator
property
¶
The precision Λ (native; the same object as prec_operator).
sample(key: jax.Array | None, sample_shape: tuple[int, ...] = ()) -> Float[Array, '*batch N']
¶
Draw x = μ + L_Λ⁻ᵀ z with z ~ 𝒩(0, I), by a triangular solve.
x = μ + L_Λ⁻ᵀ z, z ~ 𝒩(0, I), Λ = L_Λ L_Λᵀ
Algorithm
- Draw z ~ 𝒩(0, I) in the parameters' dtype.
- Strip tags and scalar multiples, Λ = c Λ₀, so a structured Λ₀ keeps its structured Cholesky factor L_Λ₀.
- Solve L_Λ₀ᵀ x₀ = z and rescale, x − μ = c^(−½) x₀.
- For a dense Λ whose Cholesky fails (non-finite draws), use the symmetric inverse square root x − μ = Λ^(−½) z instead.
- Add the mean μ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array | None
|
PRNG key. |
required |
sample_shape
|
tuple[int, ...]
|
Leading shape of the draws. |
()
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch N']
|
Draws of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_prec.py
variance() -> Float[Array, '*batch N']
¶
Marginal variances diag(Λ⁻¹), broadcast over the batch shape.
The exact structured paths of gaussx.diag_inv (e.g. the O(N d³)
selected inverse of a gaussx.BlockTriDiag) apply; only an operator
with none of them densifies (gh-360).
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch N']
|
The variances diag(Λ⁻¹), one per event entry and batch element. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_prec.py
entropy() -> Float[Array, '*batch']
¶
Entropy ℍ[𝒩(μ, Λ⁻¹)], one entry per batch element.
ℍ[𝒩(μ, Λ⁻¹)] = ½ (D_x (1 + log 2π) − log det Λ)
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
The entropy, broadcast over the batch shape. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_mvn_prec.py
State-space models¶
LGSSM is a density over the observations with the states marginalised by the Kalman filter; MarkovGaussian is the density over the states themselves.
LGSSM
¶
Bases: Distribution
Linear-Gaussian state-space model as a density over observations (T, M).
x_t = F x_t−1 + η_t, η_t ~ 𝒩(0, Σ_η)
y_t = H x_t + ε_t, ε_t ~ 𝒩(0, Σ_ε)
with x₀ ~ 𝒩(μ_x,0, Σ_x,0), a state x_t of length D_x, an observation
y_t of length D_y and t = 1, …, T. log_prob is the Kalman marginal
likelihood log p(y_1:T), delegated to gaussx.kalman_filter; sample
is ancestral forward simulation. event_shape is (T, M), so
log_prob returns a scalar and no .to_event() wrapping is needed.
Requires the numpyro optional extra
(pip install "gaussx[numpyro]").
A, H, Q and R each accept a dense array or a
lineax.AbstractLinearOperator, exactly matching
gaussx.kalman_filter's contract. Structured Σ_η and Σ_ε keep their
structure through the Cholesky in sample, H through the sandwich in
variance, and F and H apply via structural matvec rather than a
dense matmul. μ_x,0 and Σ_x,0 are dense, as they are for
gaussx.kalman_filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
_Transition
|
Transition F, the same for every step. |
required |
H
|
_Emission
|
Observation operator H. |
required |
Q
|
_StateCov
|
Model-noise covariance Σ_η. |
required |
R
|
_ObsCov
|
Observation-noise covariance Σ_ε. |
required |
m0
|
Float[Array, ' N']
|
Prior mean μ_x,0 of the initial state x₀. |
required |
P0
|
Float[Array, 'N N']
|
Prior covariance Σ_x,0 of x₀. |
required |
n_steps
|
int
|
Number of steps T. |
required |
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Examples:
Ten steps of a two-dimensional state, observed in full:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> from gaussx import LGSSM
>>> model = LGSSM(
... 0.9 * jnp.eye(2),
... jnp.eye(2),
... 0.1 * jnp.eye(2),
... 0.2 * jnp.eye(2),
... jnp.zeros(2),
... jnp.eye(2),
... n_steps=10,
... )
>>> y = model.sample(jr.key(0))
>>> y.shape, model.log_prob(y).shape
((10, 2), ())
The marginal moments have the event shape:
A diagonal operator Σ_η gives the same density as its dense matrix:
>>> import lineax as lx
>>> structured = LGSSM(
... 0.9 * jnp.eye(2),
... jnp.eye(2),
... lx.DiagonalLinearOperator(0.1 * jnp.ones(2)),
... 0.2 * jnp.eye(2),
... jnp.zeros(2),
... jnp.eye(2),
... n_steps=10,
... )
>>> bool(jnp.allclose(structured.log_prob(y), model.log_prob(y)))
True
Notes
To use as a normalizing-flow base, wrap with
pyrox_flows.NumpyroBase; no bespoke adapter is required.
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
n_steps: int
property
¶
Number of steps T.
log_prob(value: Float[Array, '*batch T M']) -> Float[Array, '*batch']
¶
Kalman marginal log-likelihood log p(y_1:T) of each series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Float[Array, '*batch T M']
|
Observations y_1:T, with leading batch axes. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
One log-likelihood per series. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
sample(key: jax.Array | None, sample_shape: tuple[int, ...] = ()) -> Float[Array, '*sample T M']
¶
Ancestral draws of the observation series y_1:T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array | None
|
PRNG key. |
required |
sample_shape
|
tuple[int, ...]
|
Leading shape of the draws. |
()
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*sample T M']
|
Draws of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
mean() -> Float[Array, 'T M']
¶
Marginal mean μ_y,t = H Fᵗ μ_x,0 of each observation.
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
variance() -> Float[Array, 'T M']
¶
Marginal variances diag(Σ_y,t) of each observation.
diag(Σ_y,t) = diag(H Σ_x,t Hᵀ) + diag(Σ_ε)
The state recursion Σ_x,t = F Σ_x,t−1 Fᵀ + Σ_η runs on a dense Σ_x,t,
the evolving quantity, so no input structure survives it. The
observation step avoids forming H Σ_x,t Hᵀ: operators go through
gaussx.sandwich and gaussx.diag, arrays contract straight to
the diagonal.
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
MaskedLGSSM
¶
Bases: LGSSM
LGSSM whose log_prob marginalises unobserved channels exactly.
log p(y_obs) = log ∫ p(y_obs, y_miss) dy_miss
Takes a (T, M) boolean mask at construction. log_prob returns
the exact marginal log p(y_obs), not a bound: the conditional
p(y_miss ∣ y_obs) is closed-form Gaussian, so dropping entries costs
nothing in exactness. Masked entries of value are never read and
may be NaN.
sample is inherited unchanged and simulates the complete
(T, M) series: the mask governs which entries the density scores,
not which are generated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
_Transition
|
Transition F, the same for every step. |
required |
H
|
_Emission
|
Observation operator H. |
required |
Q
|
_StateCov
|
Model-noise covariance Σ_η. |
required |
R
|
_ObsCov
|
Observation-noise covariance Σ_ε. |
required |
m0
|
Float[Array, ' N']
|
Prior mean μ_x,0 of the initial state x₀. |
required |
P0
|
Float[Array, 'N N']
|
Prior covariance Σ_x,0 of x₀. |
required |
n_steps
|
int
|
Number of steps T. |
required |
obs_mask
|
Bool[Array, 'T M']
|
Observation mask, one row per step. |
required |
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
Score a partially observed series:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> from gaussx import LGSSM, MaskedLGSSM
>>> params = (
... 0.9 * jnp.eye(2),
... jnp.eye(2),
... 0.1 * jnp.eye(2),
... 0.2 * jnp.eye(2),
... jnp.zeros(2),
... jnp.eye(2),
... )
>>> mask = jr.bernoulli(jr.key(0), 0.7, (10, 2))
>>> masked = MaskedLGSSM(*params, n_steps=10, obs_mask=mask)
>>> masked.log_prob(masked.sample(jr.key(1))).shape
()
An all-True mask gives the LGSSM density:
>>> y = masked.sample(jr.key(2))
>>> full = MaskedLGSSM(*params, n_steps=10, obs_mask=jnp.ones((10, 2)))
>>> plain = LGSSM(*params, n_steps=10)
>>> bool(jnp.allclose(full.log_prob(y), plain.log_prob(y)))
True
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
LGSSMFactory
¶
Bases: Module
Callable obs_mask -> MaskedLGSSM, for conditional use.
obs_mask ↦ MaskedLGSSM(F, H, Σ_η, Σ_ε, μ_x,0, Σ_x,0, T, obs_mask)
An equinox.Module rather than a closure so the state-space
parameters stay visible to equinox.filter_grad when passed as
pyrox_flows.NumpyroBase(dist_factory=...).
Attributes:
| Name | Type | Description |
|---|---|---|
A |
_Transition
|
Transition F, the same for every step. |
H |
_Emission
|
Observation operator H. |
Q |
_StateCov
|
Model-noise covariance Σ_η. |
R |
_ObsCov
|
Observation-noise covariance Σ_ε. |
m0 |
Float[Array, ' N']
|
Prior mean μ_x,0 of the initial state x₀. |
P0 |
Float[Array, 'N N']
|
Prior covariance Σ_x,0 of x₀. |
n_steps |
int
|
Number of steps T. |
Examples:
Build a masked model for one mask:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> from gaussx import LGSSMFactory
>>> factory = LGSSMFactory(
... 0.9 * jnp.eye(2),
... jnp.eye(2),
... 0.1 * jnp.eye(2),
... 0.2 * jnp.eye(2),
... jnp.zeros(2),
... jnp.eye(2),
... n_steps=10,
... )
>>> model = factory(jr.bernoulli(jr.key(0), 0.7, (10, 2)))
>>> model.event_shape
(10, 2)
The model carries the factory's parameters:
Source code in packages/pyrox-gauss/src/pyrox_gauss/_lgssm.py
MarkovGaussian
¶
Bases: Distribution
Gauss-Markov chain over the states x_0:T−1 as a density over (T, d).
x₀ ~ 𝒩(μ_x,0, Σ_x,0)
x_t+1 = F_t x_t + c_t + η_t+1, η_t+1 ~ 𝒩(0, Σ_η,t+1)
for t = 0, …, T − 2 and states x_t ∈ ℝ^{D_x}. This generative
state-space form is the canonical parameterisation; the joint
covariance Σ_x of x_0:T−1 has a block-tridiagonal inverse Λ_x.
event_shape is (T, d), so log_prob of a trajectory is a
scalar and the distribution can be used directly as a NumPyro site: a
prior over a latent path, or a structured variational guide. Every view
is derived from the state-space parameters on demand:
| View | Cost | Route |
|---|---|---|
marginals, mean, variance |
O(T D_x³) | forward moment propagation |
sample, log_prob |
O(T D_x³) | chain factorisation, no joint Cholesky |
precision, to_precision_form |
O(T D_x³) | udl_from_ssm_params |
from_precision_form |
O(T D_x³) | udl_decomposition |
covariance_matrix |
O(T² D_x³) | banded solves against I; small T |
Where LGSSM is a density over the observations of a chain with
the states marginalised out, this is the density over the states
themselves. Requires the numpyro optional extra
(pip install "gaussx[numpyro]").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Float[Array, 'Tm1 d d']
|
Transition matrices F_t, with |
required |
Q
|
Float[Array, 'Tm1 d d']
|
Model-noise covariances Σ_η,t+1, with |
required |
mu0
|
Float[Array, ' d']
|
Prior mean μ_x,0 of the initial state x₀. |
required |
P0
|
Float[Array, 'd d']
|
Prior covariance Σ_x,0 of x₀. |
required |
b
|
Float[Array, 'Tm1 d'] | None
|
Optional transition offsets c_t, with |
None
|
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
A ten-step chain in two dimensions:
>>> import jax
>>> import jax.numpy as jnp
>>> import gaussx
>>> import pyrox_gauss
>>> T, d = 10, 2
>>> A = jnp.broadcast_to(0.9 * jnp.eye(d), (T - 1, d, d))
>>> Q = jnp.broadcast_to(0.1 * jnp.eye(d), (T - 1, d, d))
>>> chain = pyrox_gauss.MarkovGaussian(A, Q, jnp.zeros(d), jnp.eye(d))
>>> xs = chain.sample(jax.random.key(0), (4,))
>>> xs.shape, chain.log_prob(xs).shape
((4, 10, 2), (4,))
The precision form round-trips to the same chain:
>>> mean, precision = chain.to_precision_form()
>>> mean.shape, precision.as_matrix().shape
((20,), (20, 20))
>>> rebuilt = pyrox_gauss.MarkovGaussian.from_precision_form(mean, precision)
>>> bool(jnp.allclose(rebuilt.A, A, atol=1e-4))
True
A single state has no transitions:
>>> single = pyrox_gauss.MarkovGaussian(A[:0], Q[:0], jnp.zeros(d), jnp.eye(d))
>>> single.event_shape, single.cross_covariances().shape
((1, 2), (0, 2, 2))
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
horizon: int
property
¶
Number of states T in the chain.
state_dim: int
property
¶
State size D_x.
precision: BlockTriDiag
property
¶
Joint precision Λ_x = Σ_x⁻¹ as a BlockTriDiag.
Raw Λ_x, not the −½ Λ_x natural-parameter convention of
ssm_to_naturals.
marginals() -> tuple[Float[Array, 'T d'], Float[Array, 'T d d']]
¶
Marginal means μ_x,t and covariances Σ_x,t of every state.
μ_x,t+1 = F_t μ_x,t + c_t
Σ_x,t+1 = F_t Σ_x,t F_tᵀ + Σ_η,t+1
Algorithm
- Start from the prior moments μ_x,0 and Σ_x,0.
- Push the moments through each transition with a scan.
- Prepend the prior moments to the propagated ones.
Returns:
| Type | Description |
|---|---|
Float[Array, 'T d']
|
Tuple |
Float[Array, 'T d d']
|
t = 0, …, T − 1. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
mean() -> Float[Array, 'T d']
¶
variance() -> Float[Array, 'T d']
¶
Marginal variances diag(Σ_x,t), one row per state.
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
cross_covariances() -> Float[Array, 'Tm1 d d']
¶
Lag-one cross-covariances Σ_x,t+1,t = Cov[x_t+1, x_t] = F_t Σ_x,t.
Returns:
| Type | Description |
|---|---|
Float[Array, 'Tm1 d d']
|
One D_x × D_x block per transition, t = 0, …, T − 2. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
pairwise_marginals() -> tuple[Float[Array, 'Tm1 two_d'], Float[Array, 'Tm1 two_d two_d']]
¶
Joint Gaussian p(x_t, x_t+1) of each pair of consecutive states.
p(x_t, x_t+1) = 𝒩([μ_x,t; μ_x,t+1], [[Σ_x,t, Σ_x,t+1,tᵀ],
[Σ_x,t+1,t, Σ_x,t+1]])
Returns:
| Type | Description |
|---|---|
Float[Array, 'Tm1 two_d']
|
Tuple |
Float[Array, 'Tm1 two_d two_d']
|
transition, each of length 2 D_x, in the layout of |
tuple[Float[Array, 'Tm1 two_d'], Float[Array, 'Tm1 two_d two_d']]
|
|
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
covariance_matrix() -> Float[Array, 'Td Td']
¶
Dense joint covariance Σ_x = Λ_x⁻¹ of the flattened trajectory.
Built column by column through UDLDecomposition.solve, so it
costs O(T² D_x³) and is meant for small T (reference checks, dense
downstream consumers).
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
udl() -> UDLDecomposition
¶
The chain's precision, already factorised as Λ_x = U D Uᵀ.
Returns:
| Type | Description |
|---|---|
UDLDecomposition
|
The |
UDLDecomposition
|
t ≥ 1 and D_0 = Σ_x,0⁻¹. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
to_precision_form() -> tuple[Float[Array, ' Td'], BlockTriDiag]
¶
The chain as its flattened mean and joint precision (μ_x, Λ_x).
This is the layout spingp_posterior consumes and returns: pass
mean as its prior_mean (it assumes a zero-mean prior
otherwise) and hand its output to from_precision_form. The
information vector Λ_x μ_x, if needed, is precision.mv(mean).
Returns:
| Type | Description |
|---|---|
Float[Array, ' Td']
|
Tuple |
BlockTriDiag
|
flattened state by state, and the joint precision Λ_x. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
from_precision_form(mean: Float[Array, ' Td'] | Float[Array, 'T d'], precision: BlockTriDiag) -> MarkovGaussian
classmethod
¶
Recover the generative chain from its mean and precision.
F_t = −U_tᵀ, Σ_η,t+1 = D_t+1⁻¹, c_t = μ_x,t+1 − F_t μ_x,t
Round-trips to_precision_form exactly (to floating point).
Algorithm
- Factorise Λ_x = U D Uᵀ with one
udl_decompositionpass. - Read off F_t, Σ_η,t+1 and Σ_x,0 = D_0⁻¹ with
udl_to_ssm_params. - Recover the offsets c_t from the mean chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
Float[Array, ' Td'] | Float[Array, 'T d']
|
Marginal means μ_x,t, flattened state by state or one row per state. |
required |
precision
|
BlockTriDiag
|
Symmetric positive-definite joint precision Λ_x. |
required |
Returns:
| Type | Description |
|---|---|
MarkovGaussian
|
The equivalent |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
log_prob(value: Float[Array, '*batch T d']) -> Float[Array, '*batch']
¶
Log-density log p(x_0:T−1) of each trajectory in value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Float[Array, '*batch T d']
|
Trajectories x_0:T−1, with leading batch axes. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch']
|
One log-density per trajectory. |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
sample(key: jax.Array | None, sample_shape: tuple[int, ...] = ()) -> Float[Array, '*sample T d']
¶
Ancestral draws of the trajectory x_0:T−1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array | None
|
PRNG key. |
required |
sample_shape
|
tuple[int, ...]
|
Leading shape of the draws. |
()
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*sample T d']
|
Draws of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_markov_gaussian.py
GMRFs¶
The GMRF fields (gaussx.GaussianMRF, IntrinsicGMRF, ConstrainedGMRF, BYM2GMRF) stay in gaussx, because gaussx's Laplace and VB code and argminax use them as linear-algebra objects. GMRFDistribution hands one to numpyro.sample.
GMRFDistribution
¶
Bases: Distribution
A GMRF field as a NumPyro distribution, for numpyro.sample.
x ~ field, log p(x) = field.log_prob(x)
The field keeps every structural dispatch (sparse Cholesky, factored eigenbasis, perturbation-optimisation, kriging); this class only gives it NumPyro's support, event shape and seeding. Draws are differentiable in the field's parameters, so the distribution is reparameterised.
Requires the numpyro optional extra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
GaussianMRF | ConstrainedGMRF | IntrinsicGMRF | BYM2GMRF
|
The field, a |
required |
validate_args
|
bool | None
|
Whether to validate input arguments. |
None
|
Examples:
A proper field on four iid nodes, sampled inside a NumPyro model:
>>> import jax.numpy as jnp
>>> import lineax as lx
>>> import numpyro
>>> import gaussx as gx
>>> import pyrox_gauss as pg
>>> precision = lx.MatrixLinearOperator(
... 2.0 * jnp.eye(4), lx.positive_semidefinite_tag
... )
>>> field = gx.GaussianMRF(jnp.zeros(4), precision)
>>> def model():
... return numpyro.sample("x", pg.GMRFDistribution(field))
>>> numpyro.handlers.seed(model, 0)().shape
(4,)
Source code in packages/pyrox-gauss/src/pyrox_gauss/_gmrf.py
mean: Float[Array, ' N']
property
¶
The field's mean.
variance: Float[Array, ' N']
property
¶
The field's marginal variances.
log_prob(value: Float[Array, '*sample N']) -> Float[Array, '*sample']
¶
The field's log-density.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Float[Array, '*sample N']
|
Points x, shape |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*sample']
|
Log-densities, shape |
Source code in packages/pyrox-gauss/src/pyrox_gauss/_gmrf.py
sample(key: jax.Array | None, sample_shape: tuple[int, ...] = ()) -> Float[Array, '*sample N']
¶
The field's draws.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array | None
|
PRNG key. |
required |
sample_shape
|
tuple[int, ...]
|
Leading sample shape. |
()
|
Returns:
| Type | Description |
|---|---|
Float[Array, '*sample N']
|
Draws, shape |