[Repo Assist] test(estimator): add regression test for categorical confounders in conditional effects (#401) - #1799
Conversation
…onditional effects Add test_categorical_common_cause_consistent_encoding to tests/causal_estimators/test_conditional_effects.py. When _estimate_conditional_effects groups data by a numeric effect modifier, some strata may not contain all categorical levels of a confounder. The Encoders class must reuse the encoder fitted on the full dataset so that feature matrix dimensions remain consistent with the fitted regression model. Without the Encoders fix (pre-sklearn OneHotEncoder, when pd.get_dummies was used per-subset), the shapes would mismatch and raise: ValueError: shapes (N, K_subset) and (K_full,) not aligned Regression test for #401. The test uses a three-level categorical confounder where level C is rare (4% of rows) so it will be absent from many effect-modifier strata, exercising the consistent-encoding path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Pull request overview
Adds a regression test to ensure conditional effect estimation remains stable when categorical confounder levels are missing from some effect-modifier strata (issue #401), and updates the module docstring to reflect the expanded regression coverage.
Changes:
- Expanded the module docstring to document the categorical-confounder encoding regression context.
- Added
test_categorical_common_cause_consistent_encodingto validate consistent one-hot feature dimensions across strata and lack of input DataFrame mutation.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| The categorical confounder has three levels (A, B, C), level C is rare | ||
| (4 % of rows) so it will be absent from many effect-modifier strata. | ||
| """ | ||
| rng = np.random.default_rng(0) | ||
| n = 2000 | ||
| # C is deliberately rare so it will be absent from some strata | ||
| cat_cause = rng.choice(["A", "B", "C"], size=n, p=[0.48, 0.48, 0.04]) | ||
| treatment = rng.integers(0, 2, size=n) | ||
| effect_modifier = rng.standard_normal(n) |
There was a problem hiding this comment.
Made this deterministic in 33f4b6c: rows with level C are confined to the top of the effect-modifier range so they fall into a single quantile bin, and added a pre-check assertion that at least one stratum lacks level C before calling estimate_effect.
| # The caller's DataFrame must not be mutated (no __categorical__ columns) | ||
| assert list(df.columns) == ["W0", "v0", "y", "X0"] |
There was a problem hiding this comment.
Updated in 33f4b6c to assert that no column starts with CausalEstimator.TEMP_CAT_COLUMN_PREFIX and that the original column set is unchanged, instead of the exact ordered list.
Co-authored-by: emrekiciman <5982160+emrekiciman@users.noreply.github.com>
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Adds
test_categorical_common_cause_consistent_encodingtotests/causal_estimators/test_conditional_effects.pyas a regression test for #401.Background
Issue #401 reported that
backdoor.linear_regressionwith categorical common causes (confounders) and numeric effect modifiers raised a shape mismatch:✅
black --checkpasses✅
isort --checkpasses✅
flake8— no new errors introduced