Skip to content

CausalPFN: keep a rare treatment arm whole in the context - #154

Open
amrit110 wants to merge 3 commits into
mainfrom
fix/causalpfn-rare-arm-context
Open

amrit110 wants to merge 3 commits into
mainfrom
fix/causalpfn-rare-arm-context

Conversation

@amrit110

@amrit110 amrit110 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Stacked on #153 (it extends that PR's reference test). It will retarget to main once #153 merges.

Problem: a training table larger than the context (4,096 rows) was subsampled keeping each arm's share. Lalonde CPS has 173 treated rows out of 16,177, so its context kept about 44 treated rows, and the effects were clearly worse than CausalPFN's own estimator on the same data.

Change (context_rows in backends/causalpfn.py):

  • Each arm gets up to half the context.
  • A smaller arm is kept whole, and the other arm takes the rest.
  • Rows within an arm are still sampled across bands of the outcome.

CausalPFN's estimator also caps each arm at half its context (of the rows nearest each group of test rows). Giving the unused share to the other arm is tabctx's choice. Tables that fit the context are unchanged, and so is SurvivalPFN.

Real data (the CausalPFN authors' files and splits at vdblm/CausalPFN@b7ed2f1, CPU):

before after CausalPFN estimator paper (Table 1)
Lalonde CPS (100): PEHE 9,552 8,947 8,955 8,960
Lalonde CPS (100): ATE error 0.388 0.177 0.133 0.13
ACIC 2016 (10): PEHE 0.975 0.964 0.963 0.92 ± 0.11
ACIC 2016 (10): ATE error 0.046 0.046 0.051 0.05
IHDP (100), Lalonde PSID (100) fit the context whole, unchanged

CPS PEHE now matches the estimator and the paper. The ATE error has gone from 0.39 to 0.18, still above the estimator's 0.13.

Both arms larger than half the context (simulated, 20,000 training rows, 5 seeds, confounded heterogeneous effect). The reviewers asked about this case because 30/70 becomes 50/50:

treated PEHE before PEHE after ATE error before ATE error after
10% 0.634 0.169 0.043 0.014
30% 0.207 0.203 0.016 0.016
50% 0.192 0.181 0.012 0.007

A floor rule (keep shares, but raise a small arm to half) gives the same rows as this rule for a binary treatment, so the alternative wasn't separate.

Intervals (CPS, first 5 realizations): the 5–95% range holds the true effect for 74.7% of test rows, up from 63.6%, with median width 18.7k vs 16.9k. That is still under 90%: CPS intervals are overconfident before and after this change, and that is a separate issue.

Tests:

  • tests/unit/test_pfn_backends.py::TestCausalContextRows, six tests:

    • a table that fits is kept whole;
    • a rare arm is kept whole;
    • two large arms get half each;
    • an odd size, with an arm just over half;
    • each arm keeps the spread of its outcomes;
    • one arm only, and repeatable.

    Restoring the old sampling inside context_rows fails 2 of them.

  • tests/integration/test_causalpfn_reference.py::test_a_rare_treatment_in_a_table_larger_than_the_context, on the first 10 CPS realizations (13 MB, sha256-pinned, cached):

    • every treated row is in the context;
    • PEHE is within 2% of CausalPFN's estimator (measured 1.003, before 1.070);
    • the ATE error is at most 0.2 (measured 0.128, before 0.346).

    Against main's backend it fails: 9,444 against a limit of 9,005. It reads the CSVs with numpy, so it needs only the causalpfn extra.

  • Runs:

    • unit suite: 1,229 passed;
    • test_pfn_backends.py: 23 passed;
    • test_causalpfn_reference.py and test_causalpfn_real.py: 16 passed, about 7.5 min on CPU;
    • the integration conftest needs ray; these tests don't use its fixtures, so I ran them with --noconftest locally.

Reviews: two independent reviews, no correctness bugs. One fuzzed 4,000 random cases (sizes, ties, NaN outcomes, single arm, odd sizes). Their fixes are in the second commit:

  • wording about upstream, and stale _pfn_common docstrings;
  • docs (now say the other arm takes the rest);
  • a CHANGELOG entry under Unreleased;
  • named test constants, a helper for the upstream estimators, and a direct treated-row check.

Not in this PR: prod runs tabctx 0.21.0. It gets this only after a release and an image bump.

🤖 Generated with Claude Code

Base automatically changed from test/causalpfn-ihdp-reference to main November 22, 2026 18:02
amrit110 and others added 3 commits November 22, 2026 16:12
A table larger than the context was subsampled keeping each arm's share,
so a rare treatment (Lalonde CPS: 173 of 16,177 rows) left the model ~44
treated rows. Each arm now gets up to an equal share of the context, a
smaller arm whole, still stratified by outcome within the arm; CausalPFN's
own estimator likewise takes up to half its context from each arm.

Lalonde CPS, first 10 realizations (the authors' splits): PEHE 9,444 ->
8,851 (CausalPFN's estimator 8,828), ATE error 0.346 -> 0.128. ACIC 2016
(10): PEHE 0.975 -> 0.964 (estimator 0.963). IHDP and PSID fit the
context whole and are unchanged. The reference test gains a CPS check
that fails on the old sampling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@amrit110
amrit110 force-pushed the fix/causalpfn-rare-arm-context branch from f2efdd0 to 035ee20 Compare November 22, 2026 21:12
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.75%. Comparing base (0280896) to head (035ee20).

Files with missing lines Patch % Lines
src/tabctx/backends/causalpfn.py 93.33% 1 Missing ⚠️

❌ Your changes status has failed because you have indirect coverage changes. Learn more about Unexpected Coverage Changes and reasons for indirect coverage changes.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #154      +/-   ##
==========================================
- Coverage   87.78%   87.75%   -0.04%     
==========================================
  Files          67       67              
  Lines        6723     6737      +14     
==========================================
+ Hits         5902     5912      +10     
- Misses        821      825       +4     
Flag Coverage Δ
integration 32.13% <0.00%> (-0.07%) ⬇️
unit 87.59% <93.33%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/tabctx/backends/_pfn_common.py 86.36% <ø> (ø)
src/tabctx/backends/causalpfn.py 35.96% <93.33%> (+4.18%) ⬆️

... and 1 file with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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