rxmc.covariance.ConstraintCovariance#
- class rxmc.covariance.ConstraintCovariance(terms, N, blocks=None, active=None)[source]#
Bases:
objectThe stacked covariance of a constraint, assembled from
Terms.Parameters are routed by identity: the unique
Parameterobjects across all terms (first-seen order) form the flat parameter vector; each term gathers its own parameters from that vector. Referencing the sameParameterobject in two terms makes them share one sampled value (case B).- Parameters:
terms (sequence of Term) – Additive covariance contributions. Terms with
support=Noneare 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_diagonalis 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 Nmatrix is always assembled (terms are authored in the full space); factorisation and the Mahalanobis distance are restricted toactive.Nonemeans all rows.``terms`` and ``blocks`` are treated as immutable after construction
:attr:`block_diagonal` and :attr:`is_constant` are decided once, here.
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 withblocks.cholesky(ctx, *theta)Lower Cholesky factor and log-determinant of the active stacked covariance.
matrix(ctx, *theta)Assemble the full stacked covariance matrix (all
Nrows).stacked_distance(ctx[, params])Squared Mahalanobis distance and log-determinant over the active residual.
Attributes
Observation block index arrays, or
Noneif unknown.Whether
stacked_distance()factors block by block (block_cholesky()) rather than the whole active stack (cholesky()).- property blocks#
Observation block index arrays, or
Noneif 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
Nrows).- Parameters:
ctx (StackContext) – Stacked arrays.
ctxitself may beNoneonly when every term is array-valued. Whenis_constant,ctx.ymis never read (it may beNone, seeStackContext.constant()) and the result is cached.*theta (float) – One value per unique parameter, in
paramsorder.
- 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)withLlower-triangular andlogdet = log det Sigma.
- block_cholesky(ctx, *theta)[source]#
Per-block
(L_i, logdet_i)factors, aligned withblocks.Only meaningful when
block_diagonal; cached whenis_constantso 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_ilower-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 viacholesky()when constant).- Returns:
(float, float) –
(d2, logdet).