Skip to content

Cov refactor - #42

Open
beykyle wants to merge 4 commits into
mainfrom
cov_refactor
Open

Cov refactor#42
beykyle wants to merge 4 commits into
mainfrom
cov_refactor

Conversation

@beykyle

@beykyle beykyle commented Aug 11, 2026

Copy link
Copy Markdown
Owner

A major refactor separating nuisance parameters from covariance instances, and both from observed data, allowing composition of nuisance terms and true hierarchical Bayesian inference. See design doc for details.

beykyle and others added 4 commits August 10, 2026 23:21
Replace the LikelihoodModel zoo with additive covariance Terms over each
constraint's stacked observation vector: cross-block terms couple
observations (case A) and Parameter-identity sharing wires one sampled
value into several terms (case B). Likelihoods become thin functionals
(Gaussian, Student-t, chi-squared) of the precomputed Mahalanobis
statistics, with the full-tuple parameter convention validated
everywhere.

Also: constraint-scoped parameter validation (cross-constraint sharing
and duplicate names are hard errors), fail-fast singular-covariance
checks naming the offending dataset, frozen block classification with
cached dense and per-block Cholesky factors, measurement systematics
retained as inert Observation metadata with an opt-in systematic_terms
factory (norm-divided units), latent-scale models (ScaledModel,
PerObservationScaledModel), predictive utilities including GP
discrepancy propagation, and comprehensive unit + regression coverage
(178 tests), including real-solver smoke tests and a fix for the
never-working dXS/dA-from-dXS/dRuth unit conversion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Port all notebooks to the Term-based covariance API and extend coverage:
new measurement_to_calibration (EXFOR-shaped measurement to calibrated
potential, unit contract, guardrails) and robust_likelihoods (Student-t
vs Gaussian) notebooks; error-model catalog completed with noise_term
and offset_term options; likelihood_scaling/weights equivalence; shared
systematics across real cross-section datasets (case A); GP model
discrepancy on a differential cross section. Notebooks use the
stacked_supports helper and no longer write PDF artifacts on execution.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Rewrite the API reference around the covariance/predictive modules,
replace the design spec with an accurate architecture page
(docs/design.md), refresh README to the Term API with the behavior
change called out, drop the broken environment.yml install path, run
notebooks in CI with pytest-xdist under a raised timeout, test 3.11,
scope bare pytest to the unit suite, and ignore local tooling artifacts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
x4i3 downloads its database at import time when missing from
site-packages, and each nbmake notebook runs in its own kernel process:
under pytest-xdist, concurrent kernels raced the same download/unpack
and failed mid-extraction. Warm the database with a single serial import
before the parallel run, and cache the data directory across runs (one
key shared by all jobs, keyed on the dependency pins) so tests, docs,
and the wheel smoke test stop re-downloading it every run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a major architecture refactor that moves covariance/uncertainty modeling out of the old “likelihood model zoo” and into an explicit, composable stacked-covariance layer (rxmc.covariance). Constraints now own a single multivariate likelihood over their stacked observations, with correlated modes expressed as additive covariance Terms; Evidence becomes a weighted sum over independent constraints with parametric constraints auto-detected.

Changes:

  • Introduces stacked covariance primitives (Term, ConstraintCovariance, factory helpers) and refactors Constraint, Evidence, Walker, and configs to use constraint-scoped covariance/likelihood parameters.
  • Redefines Observation (and reaction observation subclasses) as “pure data + statistical term”, with reported systematics retained as inert metadata and only composed into covariance when explicitly requested.
  • Adds GP predictive utilities (rxmc.predictive), extensive new tests/regression pins, and updates docs/CI to match the new API and workflows.

Reviewed changes

Copilot reviewed 40 out of 92 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/test_sampler.py Updates sampler tests to use covariance Terms and adds Gibbs-weighting/validation tests.
test/test_regression.py Adds regression pins for the systematic-default behavior change and covariance fast paths/coupling.
test/test_reaction_observation.py Adjusts reaction observation tests for “statistical-only + explicit terms” contract; adds unit/systematics retention checks.
test/test_reaction_models.py Adds real-solver smoke tests for reaction models.
test/test_predictive.py Adds unit tests for new predictive GP/band utilities.
test/test_observation.py Refactors observation tests to exercise statistical_term/systematic_terms and stacked covariance assembly.
test/test_evidence.py Refactors evidence tests for auto-detected parametric constraints and cross-constraint parameter validation.
test/test_covariance.py Adds comprehensive unit coverage for covariance term primitives, caching, and block/dense equivalence.
test/test_constraint.py Adds extensive tests for stacked constraints, case A/B, param-count/name validation, and singular-guard behavior.
test/test_config.py Updates calibration config tests for constraint-scoped params, tempering, and parametric-index alignment.
test/helpers.py Adds shared test helpers (make_ctx, dense MVN reference loglike).
test/conftest.py Ensures helpers is importable under pytest import modes.
src/rxmc/walker.py Updates parameter consistency checks and applies evidence weighting in Gibbs conditional likelihood updates.
src/rxmc/predictive.py Adds GP posterior predictive helpers and total predictive-band propagation.
src/rxmc/physical_model.py Adds ScaledModel and PerObservationScaledModel for latent mean normalization on the model side.
src/rxmc/observation.py Reworks Observation to emit a statistical Term and opt-in systematic term composition; removes fixed-covariance subclass.
src/rxmc/observation_from_measurement.py Replaces observation setup with shared validation + normalized error kwargs helper.
src/rxmc/ias_pn_observation.py Converts IAS observation to inherit Observation; composes errors via normalized kwargs; removes dynamic inheritance.
src/rxmc/evidence.py Refactors to a single constraint list with parametric auto-detection + constraint-scoped parameter validation + weighted marginal helper.
src/rxmc/elastic_diffxs_observation.py Converts elastic observation to inherit Observation; clarifies normalization conversions and error handling.
src/rxmc/covariance.py Adds the new stacked covariance engine (terms, kernels, caching, block/dense distance evaluation).
src/rxmc/correlated_discrepancy_likelihood_model.py Removes the old sklearn-GP discrepancy likelihood model implementation.
src/rxmc/constraint.py Refactors constraint to stacked MVN with explicit covariance terms and likelihood functionals; adds Σ accessor and strong validation.
src/rxmc/config.py Updates config validation, prediction ordering, and Gibbs conditional tempering to align with new evidence/constraint semantics.
src/rxmc/__init__.py Exposes new covariance and predictive modules; removes old discrepancy module export.
README.md Updates conceptual model and quickstart for explicit covariance terms + behavior-change note.
pyproject.toml Updates isort first-party list and pytest defaults to run unit tests only by default.
examples/sampling_algos.ipynb Updates notebook examples to new term-based covariance API.
examples/calibration_config_emcee_dynesty.ipynb Updates notebook to new Constraint(likelihood=...) API and output text normalization.
examples/30s_optical_potential_calibration.ipynb Updates notebook to new Constraint(likelihood=...) API.
environment.yml Removes the conda environment file.
docs/installation.rst Removes conda/mamba installation instructions.
docs/index.rst Updates docs index wording and adds design to the toctree.
docs/generated/rxmc.predictive.total_predictive_band.rst Adds generated API stub for predictive band helper.
docs/generated/rxmc.predictive.predictive_band.rst Adds generated API stub for predictive band helper.
docs/generated/rxmc.predictive.gp_posterior_predictive.rst Adds generated API stub for GP posterior predictive helper.
docs/generated/rxmc.physical_model.ScaledModel.rst Adds generated API stub for ScaledModel.
docs/generated/rxmc.physical_model.PerObservationScaledModel.rst Adds generated API stub for PerObservationScaledModel.
docs/generated/rxmc.observation.Observation.rst Updates generated API stub to reflect removed/added Observation methods.
docs/generated/rxmc.observation.FixedCovarianceObservation.rst Removes generated API stub for deleted class.
docs/generated/rxmc.likelihood_model.*.rst Removes generated stubs for deleted likelihood-model-zoo classes; adds new functional likelihood stubs.
docs/generated/rxmc.constraint.Constraint.rst Updates generated stub to include covariance matrix accessor and remove old API.
docs/generated/rxmc.config.CalibrationConfig.rst Updates generated stub to include predict_parametric.
docs/examples.rst Adds/organizes example notebooks in docs.
docs/design.md Adds design doc describing stacked covariance architecture and scope decisions.
docs/conf.py Updates exclude patterns for built docs.
docs/api.rst Updates API docs to describe covariance terms, likelihood functionals, and predictive utilities.
.gitignore Adds ignores for uv.lock, agent config, and built docs artifacts.
.github/workflows/ci.yml Expands Python matrix, adds EXFOR DB caching/warming, parallel notebook runs, and adjusts timeouts/install steps.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/rxmc/predictive.py
Comment on lines +29 to +37
def _train_noise_matrix(train_noise_var, n) -> np.ndarray:
if train_noise_var is None:
return np.zeros((n, n))
v = np.asarray(train_noise_var, dtype=float)
if v.ndim == 0:
return float(v) * np.eye(n)
if v.ndim == 1:
return np.diag(v)
return v
Comment thread src/rxmc/covariance.py
contribution into a sub-block of the stacked covariance matrix selected by an
index array ``support``.

Two mechanisms are expressed here (see ``covariance_refactor.md``):
Comment thread src/rxmc/evidence.py
Comment on lines +92 to +94
Covariance/likelihood parameters are constraint-scoped (see
``covariance_refactor.md`` §8): the same ``Parameter`` object in two
constraints would silently be sampled as two independent values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants