rxmc.constraint.Constraint#

class rxmc.constraint.Constraint(observations: list[Observation], physical_model: PhysicalModel, likelihood=None, extra_terms=(), include_statistical_term: bool = True, mask=None)[source]#

Bases: object

Pair observations with a physical model and a stacked covariance.

Parameters:
  • observations (list of Observation) – The observed data that the model will attempt to reproduce. Together they form one stacked vector y = [y1; y2; ...].

  • physical_model (PhysicalModel) – Model that predicts the observed data.

  • likelihood (object, optional) – Likelihood functional of (d2, logdet, n, *like_params). Defaults to GaussianLikelihood.

  • extra_terms (sequence of Term, optional) – Additional covariance contributions beyond the statistical diagonals — local systematics or cross-block couplings.

  • include_statistical_term (bool, optional) – When True (default) each observation’s statistical diagonal (obs.statistical_term) is added automatically. Set False to omit it and compose the entire covariance from extra_terms — e.g. to let an unknown-noise term (noise_term()) replace the reported statistics rather than add to them.

  • mask (sequence of bool or of int, optional) – Which observations are active (all by default): a boolean per observation, or the indices of the active ones. An integer array of length len(observations) holding only 0/1 is ambiguous and rejected; pass a bool array or explicit indices. Combined with each observation’s own point mask to give active.

covariance#

The stacked covariance.

Type:

ConstraintCovariance

params#

Free parameters of this constraint: covariance params followed by likelihood params (e.g. Student-t nu).

Type:

tuple of Parameter

n_params#

len(params).

Type:

int

active#

Stacked indices of the active points.

Type:

np.ndarray

n_data_pts#

Number of active points (the n of the likelihood).

Type:

int

n_data_pts_total#

Length of the full stack.

Type:

int

__init__(observations: list[Observation], physical_model: PhysicalModel, likelihood=None, extra_terms=(), include_statistical_term: bool = True, mask=None)[source]#

Methods

__init__(observations, physical_model[, ...])

chi2(model_params[, cov_params])

Generalised chi-squared (Mahalanobis distance) over the stack.

complement()

The held-out counterpart: every currently inactive point becomes active and every active point inactive.

covariance_matrix(model_params[, ...])

Assemble the stacked covariance matrix Σ at a parameter point.

empirical_coverage(ylow, yhigh[, xlim])

Fraction of active data points within a predictive interval.

log_likelihood(model_params[, cov_params])

Log likelihood of the stacked observations given the model.

marginal_log_likelihood(ym, *cov_params)

Log likelihood from pre-computed predictions (Gibbs hook).

masked([mask, point_masks])

A new constraint over the same observations/model/terms with new masks.

num_pts_within_interval(ylow, yhigh[, xlim])

Count data points that fall within a predictive interval.

predict(*model_params[, raw])

Predictions for each observation (all points, comparison space).

predict_and_covariance(model_params[, ...])

Stacked prediction and covariance on the active points, one model call.

Attributes

log_jacobian

Sum of the observations' comparison-space log-Jacobians (active points).

x

Stacked independent variable on the active points.

y

Stacked observed data on the active points (comparison space).

masked(mask=None, point_masks=None)[source]#

A new constraint over the same observations/model/terms with new masks.

Parameters:
  • mask (sequence of bool or of int, optional) – Observation-level mask (see the constructor); None keeps this constraint’s.

  • point_masks (sequence of array_like of bool, optional) – One point mask per observation (None entries keep that observation’s current mask).

Notes

The new constraint shares the Term/Parameter objects with this one, so its parameter vector is identical — it is a view for evaluating the same likelihood on a different subset (e.g. held-out scoring), not an independent constraint to place in the same Evidence. Sharing the terms is safe because both constraints stack the same observations in the same order, so the terms’ bound supports and cached x-dependent values stay valid.

complement()[source]#

The held-out counterpart: every currently inactive point becomes active and every active point inactive.

An observation excluded wholesale at the constraint level is therefore restored in full; an active observation whose point mask keeps every point is dropped wholesale. See masked() for the sharing caveat.

log_likelihood(model_params, cov_params=())[source]#

Log likelihood of the stacked observations given the model.

Parameters:
  • model_params (tuple) – Physical-model parameters.

  • cov_params (tuple, optional) – Constraint parameters: covariance params followed by likelihood params, in params order.

marginal_log_likelihood(ym: list, *cov_params)[source]#

Log likelihood from pre-computed predictions (Gibbs hook).

Parameters:
  • ym (list of np.ndarray) – One prediction array per observation (no physical-model re-eval).

  • *cov_params (float) – Constraint parameters, in params order.

chi2(model_params, cov_params=())[source]#

Generalised chi-squared (Mahalanobis distance) over the stack.

cov_params is the full constraint tuple in params order, including likelihood params (e.g. Student-t nu) even though the chi-squared statistic ignores them.

predict(*model_params, raw=False)[source]#

Predictions for each observation (all points, comparison space).

With raw=True the predictions are returned in physical space (the model’s own output, before each observation’s transform).

predict_and_covariance(model_params, cov_params=())[source]#

Stacked prediction and covariance on the active points, one model call.

Returns:

(np.ndarray, np.ndarray) – (ym, Sigma) with ym of length n_data_pts (comparison space) and Sigma a fresh (n_data_pts, n_data_pts) array.

property y: ndarray#

Stacked observed data on the active points (comparison space).

property x: ndarray#

Stacked independent variable on the active points.

property log_jacobian: float#

Sum of the observations’ comparison-space log-Jacobians (active points).

covariance_matrix(model_params, cov_params=(), active_only=True)[source]#

Assemble the stacked covariance matrix Σ at a parameter point.

Convenience accessor (e.g. for visualising the off-diagonal block structure of correlated observations).

Parameters:
  • model_params (tuple) – Physical-model parameters (needed for prediction-scaled terms).

  • cov_params (tuple, optional) – Constraint parameters: covariance params followed by likelihood params, in params order (matching log_likelihood()).

  • active_only (bool, optional) – Restrict to the active points (default); False returns the full stacked matrix.

Returns:

np.ndarray – Shape (n_data_pts, n_data_pts) (active points) or (n_data_pts_total, n_data_pts_total) when active_only=False. A fresh copy (safe to mutate; never aliases the internal cache).

num_pts_within_interval(ylow: list[ndarray], yhigh: list[ndarray], xlim=None)[source]#

Count data points that fall within a predictive interval.

empirical_coverage(ylow: list[ndarray], yhigh: list[ndarray], xlim=None)[source]#

Fraction of active data points within a predictive interval.

nan when the constraint has no active points.