HierarchicalInterruptedTimeSeries#
- class causalpy.experiments.hierarchical_interrupted_time_series.HierarchicalInterruptedTimeSeries[source]#
Hierarchical ITS for multi-unit panels with unit-specific launch times.
Unlike
InterruptedTimeSeries(single unit, single treatment time), this experiment accepts a long-format panel where every unit has its own launch time. Per-unit intercepts, covariate slopes and launch “lift” are partially pooled through a hierarchical PyMC model, which lets the model borrow strength across units and produces a population-level predictive distribution useful for forecasting the effect of a new unit.Three effect parameterizations are available:
effect_type="instant"— a single post-launch lift per unit,lift[unit] ~ Normal(mu_lift, sigma_lift).effect_type="event_study"— dynamic per-bin effects over post-launch event time (pre-launch is the implicit reference).effect_type="placebo"— the event-study form extended with pre-launch “leads” used as placebos to test the no-anticipation assumption.
- Parameters:
data (
DataFrame) – Long-format panel. Must containunit_col,time_col(numeric),treatment_time_col(numeric, same units astime_col) and any columns referenced byformula.formula (
str) – Patsy formula for the covariate design, e.g."sales ~ 0 + emails + price". An intercept should not be included — the hierarchicalalphaterm plays that role and an intercept column will be dropped with a warning.unit_col (
str) – Column identifying units (e.g. product id).time_col (
str) – Numeric time index column (e.g.week_idx). Datetime columns are not supported directly; convert to an integer index first.treatment_time_col (
str) – Numeric column with each unit’s launch time (same units astime_col).effect_type (
Literal['instant','event_study','placebo']) – The effect parameterization.bin_edges (
Sequence[float] |None) – Post-launch bin edges (in units oftime_col). Required foreffect_type="event_study"and"placebo".placebo_edges (
Sequence[float] |None) – Pre-launch (negative) bin edges foreffect_type="placebo". Rows withtaubelow the smallest edge are the implicit reference.seasonality (
dict|None) – Shared Fourier seasonality spec, e.g.{"period": 52, "K": 2}. IfNone(default), no seasonality term is included.ar_residuals (
bool) – IfTrue, add hierarchical AR(1) residuals per unit viapytensor.scan. Requires a balanced panel (all units observed at the same time steps). The AR coefficient is partially pooled:rho[unit] ~ tanh(Normal(mu_rho, sigma_rho)).model (
PyMCModel|None) – A custom model instance. IfNone, a default is constructed.
Methods
Fit model, compute observed/counterfactual predictions and impact.
Return a compact summary of the population-level effect.
HierarchicalInterruptedTimeSeries.fit(*args, ...)Generate a self-contained HTML report for this experiment.
Recover the data of an experiment along with the prediction and causal impact information.
HierarchicalInterruptedTimeSeries.get_plot_data_bayesian(...)Return plot data for Bayesian models.
Return plot data for OLS models.
Plot the model.
Plot observed vs counterfactual and causal impact for a single unit.
HierarchicalInterruptedTimeSeries.predictive_for_new_unit([...])Draw from the population predictive distribution of a new unit's effect.
Print population-level coefficient summaries for the hierarchical model.
Set optional maketables rendering options for this experiment.
Print a short summary of the fitted hierarchical model.
Attributes
expt_typeidataReturn the InferenceData object of the model.
supports_bayessupports_olslabelsdata- __init__(data, formula, unit_col, time_col, treatment_time_col, effect_type='instant', bin_edges=None, placebo_edges=None, seasonality=None, ar_residuals=False, model=None, **kwargs)[source]#
- classmethod __new__(*args, **kwargs)#