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:
objectPair 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 toGaussianLikelihood.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. SetFalseto omit it and compose the entire covariance fromextra_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 pointmaskto giveactive.
- covariance#
The stacked covariance.
- Type:
- params#
Free parameters of this constraint: covariance params followed by likelihood params (e.g. Student-t
nu).
- active#
Stacked indices of the active points.
- Type:
np.ndarray
- __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.
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
Sum of the observations' comparison-space log-Jacobians (active points).
Stacked independent variable on the active points.
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);
Nonekeeps this constraint’s.point_masks (sequence of array_like of bool, optional) – One point mask per observation (
Noneentries keep that observation’s current mask).
Notes
The new constraint shares the
Term/Parameterobjects 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 sameEvidence. Sharing the terms is safe because both constraints stack the same observations in the same order, so the terms’ bound supports and cachedx-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
paramsorder.
- 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
paramsorder.
- chi2(model_params, cov_params=())[source]#
Generalised chi-squared (Mahalanobis distance) over the stack.
cov_paramsis the full constraint tuple inparamsorder, including likelihood params (e.g. Student-tnu) even though the chi-squared statistic ignores them.
- predict(*model_params, raw=False)[source]#
Predictions for each observation (all points, comparison space).
With
raw=Truethe predictions are returned in physical space (the model’s own output, before each observation’stransform).
- 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)withymof lengthn_data_pts(comparison space) andSigmaa fresh(n_data_pts, n_data_pts)array.
- 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
paramsorder (matchinglog_likelihood()).active_only (bool, optional) – Restrict to the active points (default);
Falsereturns 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)whenactive_only=False. A fresh copy (safe to mutate; never aliases the internal cache).