InterruptedTimeSeries.analyze_persistence#
- InterruptedTimeSeries.analyze_persistence(hdi_prob=0.95, direction='increase')[source]#
Analyze effect persistence between intervention and post-intervention periods.
Computes mean effects, persistence ratio, and total (cumulative) impacts for both periods. The persistence ratio is the post-intervention mean effect divided by the intervention mean effect (as a decimal, e.g., 0.30 means 30% persistence, 1.5 means 150%). Note: The ratio can exceed 1.0 if the post-intervention effect is larger than the intervention effect.
Automatically prints a summary of the results.
- Parameters:
hdi_prob (float, default=0.95) – Probability for HDI interval (Bayesian models only)
direction ({"increase", "decrease", "two-sided"}, default="increase") – Direction for tail probability calculation (Bayesian models only)
- Returns:
Dictionary containing: - “mean_effect_during”: Mean effect during intervention period - “mean_effect_post”: Mean effect during post-intervention period - “persistence_ratio”: Post-intervention mean effect divided by intervention mean (decimal, can exceed 1.0) - “total_effect_during”: Total (cumulative) effect during intervention period - “total_effect_post”: Total (cumulative) effect during post-intervention period
- Return type:
- Raises:
ValueError – If treatment_end_time is not provided (two-period design)
Examples
>>> import causalpy as cp >>> import pandas as pd >>> df = ( ... cp.load_data("its") ... .assign(date=lambda x: pd.to_datetime(x["date"])) ... .set_index("date") ... ) >>> result = cp.InterruptedTimeSeries( ... df, ... treatment_time=pd.Timestamp("2017-01-01"), ... treatment_end_time=pd.Timestamp("2017-06-01"), ... formula="y ~ 1 + t + C(month)", ... model=cp.pymc_models.LinearRegression( ... sample_kwargs={"random_seed": 42, "progressbar": False} ... ), ... ) >>> persistence = result.analyze_persistence() ... # Note: Results are automatically printed to console >>> persistence["persistence_ratio"] -1.224