RegressionDiscontinuity#

class causalpy.experiments.regression_discontinuity.RegressionDiscontinuity[source]#

A class to analyse sharp regression discontinuity 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.

  • treatment_threshold (float) – A scalar threshold value at which the treatment is applied.

  • model (PyMCModel | RegressorMixin | None) – A PyMC or sklearn model. Defaults to LinearRegression.

  • bandwidth (float) – Data outside of the bandwidth (relative to the discontinuity) is not used to fit the model.

  • running_variable_name (str) – The name of the predictor variable that the treatment threshold is based upon.

  • epsilon (float) – A small scalar value which determines how far above and below the treatment threshold to evaluate the causal impact.

  • donut_hole (float) – Observations within this distance from the treatment threshold are excluded from model fitting. Used as a robustness check when observations closest to the threshold may be problematic (e.g., due to manipulation or heaping). Must be non-negative and less than bandwidth if bandwidth is finite.

Notes

Lazy lifecycle

Construction only validates input and builds design matrices — nothing is sampled. Call fit() to run posterior inference (it returns self, so construction and fitting chain in one expression), and optionally sample_prior_predictive() first for prior predictive checks (plot(group="prior"), effect_summary(group="prior")). Results live on exp.result / exp.prior_result.

Estimate extraction

After fitting the regression on the selected bandwidth, the class predicts the conditional expectation immediately below the threshold with treated=0 and immediately above it with treated=1. discontinuity_at_threshold is the upper prediction minus the lower prediction, evaluated at threshold ± epsilon. This is a local prediction contrast, not a population-standardized effect.

Examples

>>> import causalpy as cp
>>> df = cp.load_data("rd")
>>> seed = 42
>>> result = cp.RegressionDiscontinuity(
...     df,
...     formula="y ~ 1 + x + treated + x:treated",
...     model=cp.pymc_models.LinearRegression(
...         sample_kwargs={
...             "draws": 100,
...             "target_accept": 0.95,
...             "random_seed": seed,
...             "progressbar": False,
...         },
...     ),
...     treatment_threshold=0.5,
... ).fit()

Methods

RegressionDiscontinuity.build()

Construct the model graph without sampling anything.

RegressionDiscontinuity.effect_summary(*[, ...])

Generate a decision-ready summary of causal effects for Regression Discontinuity.

RegressionDiscontinuity.fit(**kwargs)

Run the posterior phase and populate result.

RegressionDiscontinuity.generate_report(*[, ...])

Generate a self-contained HTML report for this experiment.

RegressionDiscontinuity.input_validation()

Validate the input data and model formula for correctness.

RegressionDiscontinuity.plot(*[, group, ...])

Plot the regression discontinuity results.

RegressionDiscontinuity.print_coefficients([...])

Ask the model to print its posterior coefficients.

RegressionDiscontinuity.sample_prior_predictive(...)

Run the optional prior phase and populate prior_result.

RegressionDiscontinuity.set_maketables_options(*)

Set optional maketables rendering options for this experiment.

RegressionDiscontinuity.summary([round_to])

Print summary of main results and model coefficients.

Attributes

has_prior_predictive

Whether the prior phase has run (draws, and bundle where kept).

idata

Return fitted DataTree when the model backend supports it.

is_built

Whether the model graph / fit design exists (no draws implied).

is_configured

design matrices are ready.

is_fitted

Whether posterior draws and the posterior result bundle exist.

model

The underlying model instance.

prior_result

Prior-group result bundle; raises before prior sampling.

result

Posterior-group result bundle; raises before fit().

supports_bayes

supports_ols

supports_pymc_forecast

labels

data

__init__(data, formula, treatment_threshold, model=None, running_variable_name='x', epsilon=0.001, bandwidth=inf, donut_hole=0.0)[source]#
Parameters:
Return type:

None

classmethod __new__(*args, **kwargs)#