rxmc.covariance.ConstraintCovariance#

class rxmc.covariance.ConstraintCovariance(terms, N, blocks=None, active=None)[source]#

Bases: object

The stacked covariance of a constraint, assembled from Term s.

Parameters are routed by identity: the unique Parameter objects across all terms (first-seen order) form the flat parameter vector; each term gathers its own parameters from that vector. Referencing the same Parameter object in two terms makes them share one sampled value (case B).

Parameters:
  • terms (sequence of Term) – Additive covariance contributions. Terms with support=None are bound to the whole stack here.

  • N (int) – Dimension of the stacked vector.

  • blocks (sequence of np.ndarray, optional) – One index array per observation block. When given, block_diagonal is decided against the true block boundaries. When omitted, only a covariance whose every term is strictly diagonal (couples_offdiagonal == False) is classified block-diagonal; any coupling-capable term conservatively forces the dense path — there is no guessing of block structure from support shape.

  • active (array_like of int, optional) – Indices of the active (unmasked) rows of the stack. The full N x N matrix is always assembled (terms are authored in the full space); factorisation and the Mahalanobis distance are restricted to active. None means all rows.

  • ``terms`` and ``blocks`` are treated as immutable after construction

  • :attr:`block_diagonal` and :attr:`is_constant` are decided once, here.

__init__(terms, N, blocks=None, active=None)[source]#

Methods

__init__(terms, N[, blocks, active])

active_matrix(ctx, *theta)

The covariance restricted to the active rows/columns.

block_cholesky(ctx, *theta)

Per-block (L_i, logdet_i) factors, aligned with blocks.

cholesky(ctx, *theta)

Lower Cholesky factor and log-determinant of the active stacked covariance.

matrix(ctx, *theta)

Assemble the full stacked covariance matrix (all N rows).

stacked_distance(ctx[, params])

Squared Mahalanobis distance and log-determinant over the active residual.

Attributes

blocks

Observation block index arrays, or None if unknown.

n_params

uses_block_path

Whether stacked_distance() factors block by block (block_cholesky()) rather than the whole active stack (cholesky()).

property n_params: int#
property blocks#

Observation block index arrays, or None if unknown.

property uses_block_path: bool#

Whether stacked_distance() factors block by block (block_cholesky()) rather than the whole active stack (cholesky()).

matrix(ctx, *theta) → ndarray[source]#

Assemble the full stacked covariance matrix (all N rows).

Parameters:
  • ctx (StackContext) – Stacked arrays. ctx itself may be None only when every term is array-valued. When is_constant, ctx.ym is never read (it may be None, see StackContext.constant()) and the result is cached.

  • *theta (float) – One value per unique parameter, in params order.

active_matrix(ctx, *theta) → ndarray[source]#

The covariance restricted to the active rows/columns.

cholesky(ctx, *theta)[source]#

Lower Cholesky factor and log-determinant of the active stacked covariance.

Cached when is_constant, so a fixed covariance is factored once.

Returns:

(np.ndarray, float) – (L, logdet) with L lower-triangular and logdet = log det Sigma.

block_cholesky(ctx, *theta)[source]#

Per-block (L_i, logdet_i) factors, aligned with blocks.

Only meaningful when block_diagonal; cached when is_constant so a constant block-diagonal covariance is factored once instead of on every likelihood evaluation. Blocks are restricted to the active rows; a fully-masked block yields (empty, 0.0).

Returns:

tuple of (np.ndarray, float) – One (L_i, logdet_i) pair per block, L_i lower-triangular.

stacked_distance(ctx, params=())[source]#

Squared Mahalanobis distance and log-determinant over the active residual.

Owns the dispatch between the block-diagonal fast path (factor each block separately, \(O(\sum n_i^3)\), cached per block via block_cholesky() when constant) and a single dense Cholesky over the full stack (cached via cholesky() when constant).

Returns:

(float, float) – (d2, logdet).