Skip to content

Add EPAnalysisFactor for cavity-message injection - #1248

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/cancer-ep-cavity-messages
May 1, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/cancer-ep-cavity-messages

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Adds EPAnalysisFactor, an additive AnalysisFactor subclass that exposes the EP cavity distribution to its Analysis on each fit. This unblocks hierarchical / population-level likelihoods that need to read per-variable cavity messages directly inside log_likelihood_function — for example, a "global" Analysis that compares predictions to per-dataset posterior summaries produced by upstream local fits, of the form log L = -0.5 * sum_i || (pred_i - cavity_mean_i) / cavity_sigma_i ||^2.

The change is purely additive: the cavity hook in factor_step is duck-typed via hasattr(factor, "set_cavity_dist"), so plain AnalysisFactor and every other existing factor type are unaffected.

API Changes

Added af.EPAnalysisFactor — an AnalysisFactor subclass with a set_cavity_dist(cavity_dist) hook that the EP optimiser invokes before every fit, attaching the cavity MeanField to analysis._cavity_mean_field so the user's log_likelihood_function can read per-variable cavity messages (.mean, .sigma) at evaluation time. No symbols removed, renamed, or behaviour-changed for existing factors. See full details below.

Test Plan

  • pytest test_autofit/graphical/test_ep_analysis_factor.py — 4 new unit tests pass
  • pytest test_autofit/graphical -q — full graphical suite passes (184 tests)
  • End-to-end exercised on a real EP fit (3-dataset cancer dose-response): EPAnalysisFactor.set_cavity_dist fires before every global step, populates analysis._cavity_mean_field with non-trivial messages from the local fits, and the per-variable means/sigmas tighten across EP iterations as expected.
Full API Changes (for automation & release notes)

Added

  • autofit.EPAnalysisFactor(prior_model, analysis, optimiser=None, name=None) — AnalysisFactor subclass exposing the EP cavity distribution to its Analysis via set_cavity_dist(cavity_dist). Stores the cavity MeanField on analysis._cavity_mean_field so log_likelihood_function can read per-variable cavity messages.
  • autofit.graphical.EPAnalysisFactor — same class re-exported under the graphical namespace.

Changed Behaviour

  • autofit.graphical.expectation_propagation.optimiser.factor_step — now calls factor.set_cavity_dist(factor_approx.cavity_dist) if the factor implements it. Duck-typed: no-op for plain AnalysisFactor and every other factor type that does not opt in.

Migration

None required — purely additive. Existing AnalysisFactor users see no change.

🤖 Generated with Claude Code

EPAnalysisFactor is a thin AnalysisFactor subclass that exposes the EP
cavity distribution to its Analysis on each fit, via a guarded
``set_cavity_dist`` hook called from ``factor_step``. This lets a
hierarchical / population-level Analysis read per-variable cavity
messages directly inside ``log_likelihood_function`` — useful for
"global" likelihoods that compare predictions to the per-dataset
posterior summaries produced by upstream local fits, e.g. the
EP-message-comparison form documented in the cancer-IC50 use case:

    log L = -0.5 * sum_i || (pred_i - cavity_mean_i) / cavity_sigma_i ||^2

The hook is duck-typed so plain AnalysisFactor behaviour is unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 1, 2026
@Jammy2211
Jammy2211 merged commit f6d3b4a into main May 1, 2026
3 checks passed
@Jammy2211
Jammy2211 deleted the feature/cancer-ep-cavity-messages branch May 1, 2026 15:42
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
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.

1 participant