InversePropensityWeighting#
- class causalpy.experiments.inverse_propensity_weighting.InversePropensityWeighting[source]#
A class to analyse inverse propensity weighting experiments.
- Parameters:
data (
NativeDataFrame) – Any eager dataframe Narwhals supports, such as pandas, Polars, or PyArrow. Converted to pandas internally.formula (
str) – A statistical model formula for the propensity model.outcome_variable (
str) – A string denoting the outcome variable in data to be reweighted.weighting_scheme (
str) – A string denoting which weighting scheme to use among: ‘raw’, ‘robust’, ‘doubly_robust’ or ‘overlap’. See Aronow and Miller “Foundations of Agnostic Statistics” for discussion and computation of these weighting schemes.model (
PropensityScore|None) – A PyMC model. Defaults to PropensityScore.
Notes
Lazy lifecycle
Construction validates the inputs and builds the design matrices — no sampling happens. Call
fit()to build the propensity graph and draw prior and posterior samples. The IPW diagnostics (get_ate(),plot_ate(),plot_balance_ecdf()) are posterior-only and requirefit()first.Estimate extraction
Fitting produces posterior propensity-score draws.
get_ate()post-processes one draw at a time:"raw"and"robust"contrast inverse-probability-weighted mean outcomes for the treated and control potential outcomes,"overlap"contrasts overlap-weighted means for the overlap population, and"doubly_robust"augments inverse-probability weighting with separate OLS outcome regressions before averaging over all observations.Examples
>>> import causalpy as cp >>> df = cp.load_data("nhefs") >>> seed = 42 >>> result = cp.InversePropensityWeighting( ... df, ... formula="trt ~ 1 + age + race", ... outcome_variable="outcome", ... weighting_scheme="robust", ... model=cp.pymc_models.PropensityScore( ... sample_kwargs={ ... "draws": 100, ... "target_accept": 0.95, ... "random_seed": seed, ... "progressbar": False, ... }, ... ), ... ).fit()
Methods
Construct the model graph without sampling anything.
Raise because unified effect summaries are unavailable.
InversePropensityWeighting.fit(**kwargs)Run the posterior phase and populate
result.Generate a self-contained HTML report for this experiment.
InversePropensityWeighting.get_ate(i, idata)Compute the Average Treatment Effect for a single posterior sample.
Validate the input data and model formula for correctness.
InversePropensityWeighting.make_doubly_robust_adjustment(ps)Compute doubly-robust adjusted outcomes.
Compute inverse-propensity-weighted outcomes using the overlap scheme.
Compute inverse-propensity-weighted outcomes using the raw (basic) scheme.
Compute inverse-propensity-weighted outcomes using the robust (Horvitz-Thompson) scheme.
InversePropensityWeighting.plot(*[, show, ...])Plot the results.
InversePropensityWeighting.plot_ate([idata, ...])Plot the Average Treatment Effect and propensity score distributions.
Plot the empirical CDF of a covariate before and after IPW adjustment.
Ask the model to print its posterior coefficients.
Run the optional prior phase and populate
prior_result.Set optional maketables rendering options for this experiment.
Compute a weighted percentile of the data.
Attributes
has_prior_predictiveWhether the prior phase has run (draws, and bundle where kept).
idataReturn fitted DataTree when the model backend supports it.
is_builtWhether the model graph / fit design exists (no draws implied).
is_configureddesign matrices are ready.
is_fittedWhether posterior draws and the posterior result bundle exist.
modelThe underlying model instance.
prior_resultPrior-group result bundle; raises before prior sampling.
resultPosterior-group result bundle; raises before
fit().supports_bayessupports_olssupports_pymc_forecastlabelsdata- __init__(data, formula, outcome_variable, weighting_scheme, model=None)[source]#
- Parameters:
data (DataFrameLike)
formula (str)
outcome_variable (str)
weighting_scheme (str)
model (PropensityScore | None)
- Return type:
None
- classmethod __new__(*args, **kwargs)#