Skip to main content

balanced-moderation

Balanced moderation analysis with causal machine learning.

balanced_moderation estimates how the effect of a treatment differs between two groups (for example women and men) while holding the distribution of other characteristics fixed. It implements the balanced group average treatment effect (BGATE) of Bearth & Lechner (2024), estimated with double/debiased machine learning (DML), and calibrates propensity scores with the cross-fitted procedure of Ballinari & Bearth (2024).

Why BGATE?

Suppose a training programme raises wages more for men than for women. Is that because of gender, or because men and women in the sample differ in education, experience or occupation? The usual group average treatment effects (GATEs) cannot tell these apart: their difference mixes the moderation effect with differences in the covariate distributions of the two groups.

The BGATE fixes the distribution of chosen balancing variables W to be the same in both groups:

BGATE(z) = E_W[ E[ Y(1) - Y(0) | Z = z, W ] ]

so the difference BGATE(1) - BGATE(0) is the moderation effect of Z holding the distribution of W fixed. Without balancing variables you get the ordinary GATE difference, so you can compare both.

Installation

pip install balanced-moderation

Requires Python 3.10 or newer. Dependencies: numpy, pandas, scikit-learn, scipy and venn-abers.

Quickstart

from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from balanced_moderation import BGATE, make_moderation_data

df = make_moderation_data(n=4000, design="z_affects_x", random_state=42)
controls = [f"x{j}" for j in range(10)]

model = BGATE(
    outcome_model=RandomForestRegressor(n_estimators=300, min_samples_leaf=5, random_state=0),
    propensity_model=RandomForestClassifier(n_estimators=300, min_samples_leaf=5, random_state=0),
    random_state=42,
)

result = model.fit(
    df,
    y="y",              # outcome
    d="d",              # binary treatment
    z="z",              # binary moderator
    x=controls,         # control variables (confounders)
    balance=["x5"],     # balancing variables W (a subset of x)
)
print(result.summary())
========================================================================
Balanced GATE (BGATE) - balanced over: x5
Moderator: z    Observations: 4000
========================================================================
            estimate  std_err  t_stat  p_value  ci_95_lower  ci_95_upper
BGATE(z=1)    1.2713   0.0815 15.5959   0.0000       1.1115       1.4310
BGATE(z=0)    1.1781   0.0529 22.2881   0.0000       1.0745       1.2817
Difference    0.0931   0.0970  0.9607   0.3367      -0.0969       0.2832
========================================================================

In this simulated design the moderator z also shifts the distribution of x5. The true GATE difference is 0.414, but once x5 is balanced, the true BGATE difference is only 0.276: part of the GATE difference is due to the different x5 distributions, not to z itself. (Both 95% confidence intervals from this one simulated sample contain the true values.)

Leave out balance= to estimate the GATE difference instead. A complete example is in examples/quickstart.py.

Working with the results

result.effect the moderation effect, BGATE(z=1) - BGATE(z=0)
result.std_err, result.pvalue its standard error and two-sided p-value
result.conf_int(alpha=0.05) its confidence interval
result.to_frame() all three estimates as a pandas DataFrame
result.summary() a printable table
result.scores the observation-level scores (their means are the estimates)

Settings

Parameter Default Meaning
outcome_model random forest any scikit-learn regressor, for E[Y | D, X]
propensity_model random forest any scikit-learn classifier with predict_proba, for P(D=1 | X)
effect_model = outcome_model BGATE only: regressor for E[τ | W, Z=z]
moderator_model = propensity_model BGATE only: classifier for P(Z=1 | W)
n_folds 2 cross-fitting folds
n_folds_second = n_folds BGATE only: folds of the second cross-fitting step
calibration "venn_abers" propensity score calibration: "venn_abers", "platt", "beta", "isotonic" or None
normalize_weights True BGATE only: normalise the second-step weights to mean one
propensity_clip 1e-10 propensity scores are clipped to [clip, 1 − clip]
random_state None seed for the fold assignment

Any scikit-learn compatible model works, including pipelines and GridSearchCV objects for hyperparameter tuning. For fully reproducible results, also set random_state in your models.

Propensity score calibration

Flexible classifiers such as random forests are often over-confident: they predict treatment probabilities close to, or exactly, 0 or 1. The inverse probability weights in DML then explode. By default, balanced_moderation calibrates both propensity scores, P(D=1 | X) and P(Z=1 | W), following Ballinari & Bearth (2024): within each cross-fitting fold, the held-out predictions are split again, and a calibration map fitted on one part is applied to the other.

The default is Venn-Abers calibration, which keeps calibrated scores away from 0 and 1 even when the classifier predicts exactly 0 or 1. Platt scaling is a good alternative. Beta calibration works on log-probabilities and can produce extreme scores when a classifier outputs exact zeros or ones (as random forests often do). Ballinari & Bearth recommend choosing the calibration method by its Brier score and advise against isotonic regression. Use calibration=None to switch calibration off.

If estimated propensity scores are still below 0.01 or above 0.99, balanced_moderation issues a warning.

How it works

Step 1. With K-fold cross-fitting, estimate E[Y | D=1, X, Z], E[Y | D=0, X, Z] and P(D=1 | X, Z), and compute the doubly robust (AIPW) score τᵢ of the treatment effect for every observation.

Step 2. Within each step-1 fold, cross-fit again: regress τ on the balancing variables W separately for Z=1 and Z=0, estimate P(Z=1 | W), and combine them into doubly robust scores for BGATE(1) and BGATE(0).

Estimates are the means of these scores, and standard errors come from their variance.

Sample size and balancing variables. The second step only uses the observations of one first-step fold, split again by the moderator. With K folds in each step, each second-step model is trained on roughly (1/K)·(1 − 1/K) of the sample, before splitting by Z: a quarter of the sample with the default of 2 × 2 folds. Balancing on one or a few variables works well in moderate samples (Bearth & Lechner, 2024, find good coverage from N ≈ 1,250 with one balancing variable). Balancing on many variables at once makes the second-step regressions harder and needs larger samples; more folds make this worse. See Bearth & Lechner (2024) for the theory, including conditions for √N-consistency and asymptotic normality.

Current limitations: the treatment D and the moderator Z must be binary.

Replication

With calibration=None, the package reproduces the DML estimator used in the simulation study of Bearth & Lechner (2024) exactly, for the same data and random seed. The original replication code is at NoraBearth/Causal-Machine-Learning-for-Moderation-Effects, and make_moderation_data generates the paper's simulation designs.

Citation

If you use this package, please cite:

@article{bearth2024moderation,
  title   = {Causal Machine Learning for Moderation Effects},
  author  = {Bearth, Nora and Lechner, Michael},
  journal = {arXiv preprint arXiv:2401.08290},
  year    = {2024}
}

and, if you use propensity score calibration:

@article{ballinari2024calibration,
  title   = {Improving the Finite Sample Estimation of Average Treatment Effects
             using Double/Debiased Machine Learning with Propensity Score Calibration},
  author  = {Ballinari, Daniele and Bearth, Nora},
  journal = {arXiv preprint arXiv:2409.04874},
  year    = {2024}
}

License

MIT, see LICENSE.

Metadata

Release files for balanced-moderation 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for balanced-moderation 0.1.0
File Size Uploaded Provenance Actions
balanced_moderation-0.1.0.tar.gz 18.9 kB With provenance Details for balanced_moderation-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for balanced-moderation 0.1.0
File Interpreter ABI Platform Actions
balanced_moderation-0.1.0-py3-none-any.whl Python 3 none any Details for balanced_moderation-0.1.0-py3-none-any.whl

Total release size: 37.7 kB

Release files / balanced_moderation-0.1.0.tar.gz

Download URL balanced_moderation-0.1.0.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a598b2a47d5f95b09a4b3e44e904116939e72f112277f508098617af0d5ecd98
BLAKE2b-256 checksum
How to use checksums
824041764c402feb54f15a36f277472293173dc229ccbbc077d90eddda71623b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / balanced_moderation-0.1.0-py3-none-any.whl

Download URL balanced_moderation-0.1.0-py3-none-any.whl
Size 18.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f668cad1eddfc24a5bf624af816fad6e7f879f4b2cbca0e015af48d901523891
BLAKE2b-256 checksum
How to use checksums
b0a48f7d4a7e10c563ed9c670335b4c578d83f9db1962c39217313c8ef378f9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for balanced-moderation 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for balanced_moderation-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page