Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions causalpy/experiments/interrupted_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class InterruptedTimeSeries(BaseExperiment):
... }
... ),
... )
Notes
-----
For Bayesian models, the causal impact is calculated using the posterior expectation
(``mu``) rather than the posterior predictive (``y_hat``). This means the impact and
its uncertainty represent the systematic causal effect, excluding observation-level
noise. The uncertainty bands in the plots reflect parameter uncertainty and
counterfactual prediction uncertainty, but not individual observation variability.
"""

expt_type = "Interrupted Time Series"
Expand Down
8 changes: 8 additions & 0 deletions causalpy/experiments/synthetic_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class SyntheticControl(BaseExperiment):
... }
... ),
... )
Notes
-----
For Bayesian models, the causal impact is calculated using the posterior expectation
(``mu``) rather than the posterior predictive (``y_hat``). This means the impact and
its uncertainty represent the systematic causal effect, excluding observation-level
noise. The uncertainty bands in the plots reflect parameter uncertainty and
counterfactual prediction uncertainty, but not individual observation variability.
"""

supports_ols = True
Expand Down
39 changes: 38 additions & 1 deletion causalpy/pymc_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,44 @@ def score(self, X: xr.DataArray, y: xr.DataArray) -> pd.Series:
def calculate_impact(
self, y_true: xr.DataArray, y_pred: az.InferenceData
) -> xr.DataArray:
impact = y_true - y_pred["posterior_predictive"]["y_hat"]
"""
Calculate the causal impact as the difference between observed and predicted values.

The impact is calculated using the posterior expectation (`mu`) rather than the
posterior predictive (`y_hat`). This means the causal impact represents the
difference from the expected value of the model, excluding observation noise.
This approach provides a cleaner measure of the causal effect by focusing on
the systematic difference rather than including sampling variability from the
observation noise term.

Parameters
----------
y_true : xr.DataArray
The observed outcome values with dimensions ["obs_ind", "treated_units"].
y_pred : az.InferenceData
The posterior predictive samples containing the "mu" variable, which
represents the expected value (mean) of the outcome.

Returns
-------
xr.DataArray
The causal impact with dimensions ending in "obs_ind". The impact includes
posterior uncertainty from the model parameters but excludes observation noise.

Notes
-----
By using `mu` (the posterior expectation) rather than `y_hat` (the posterior
predictive with observation noise), the uncertainty in the impact reflects:
- Parameter uncertainty in the fitted model
- Uncertainty in the counterfactual prediction

But excludes:
- Observation-level noise (sigma)

This makes the impact plots focus on the systematic causal effect rather than
individual observation variability.
"""
impact = y_true - y_pred["posterior_predictive"]["mu"]
return impact.transpose(..., "obs_ind")

def calculate_cumulative_impact(self, impact):
Expand Down
6 changes: 3 additions & 3 deletions docs/source/_static/interrogate_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 25 additions & 15 deletions docs/source/notebooks/geolift1.ipynb

Large diffs are not rendered by default.

58 changes: 27 additions & 31 deletions docs/source/notebooks/its_covid.ipynb

Large diffs are not rendered by default.

476 changes: 296 additions & 180 deletions docs/source/notebooks/its_pymc.ipynb

Large diffs are not rendered by default.

274 changes: 142 additions & 132 deletions docs/source/notebooks/multi_cell_geolift.ipynb

Large diffs are not rendered by default.

78 changes: 44 additions & 34 deletions docs/source/notebooks/sc_pymc.ipynb

Large diffs are not rendered by default.

288 changes: 148 additions & 140 deletions docs/source/notebooks/sc_pymc_brexit.ipynb

Large diffs are not rendered by default.