Skip to content

Commit b63860d

Browse files
authored
Merge pull request #504 from pymc-labs/its-causal-impact-details
Add details about causal impact in the interrupted time series docs
2 parents 0449370 + 60bab33 commit b63860d

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

docs/source/notebooks/its_pymc.ipynb

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,42 @@
55
"cell_type": "markdown",
66
"metadata": {},
77
"source": [
8-
"# Example Interrupted Time Series (ITS) with `pymc` models\n",
8+
"# Bayesian Interrupted Time Series\n",
99
"\n",
10-
"This notebook shows an example of using interrupted time series, where we do not have untreated control units of a similar nature to the treated unit and we just have a single time series of observations and the predictor variables are simply time and month."
10+
"Interrupted Time Series (ITS) analysis is a powerful approach for estimating the causal impact of an intervention or treatment when you have a single time series of observations. The key idea is to compare what actually happened after the intervention to what would have happened in the absence of the intervention (the \"counterfactual\"). To do this, we train a statistical model on the pre-intervention data (when no treatment has occurred) and then use this model to forecast the expected outcomes into the post-intervention period as-if treatment had not occurred. The difference between the observed outcomes and these model-based counterfactual predictions provides an estimate of the causal effect of the intervention, along with a measure of uncertainty if using a Bayesian approach."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"## What do we mean by _causal impact_ in Interrupted Time Series?\n",
18+
"\n",
19+
"In the context of interrupted time series analysis, the term **causal impact** refers to the estimated effect of an intervention or event on an outcome of interest. We can break this down into two components which tell us different aspects of the intervention's effect:\n",
20+
"\n",
21+
"- The **Instantaneous Bayesian Causal Effect** at each time point is the difference between the observed outcome and the model's posterior predictive distribution for the counterfactual (i.e., what would have happened without the intervention). This is not just a single number, but a full probability distribution that reflects our uncertainty.\n",
22+
"- The **Cumulative Bayesian Causal Impact** is the sum of these instantaneous effects over the post-intervention period, again represented as a distribution.\n",
23+
"\n",
24+
"Let $y_t$ be the observed outcome at time $t$ (after the intervention), and $\\tilde{y}_t$ be the model's counterfactual prediction for the same time point. Then:\n",
25+
"- **Instantaneous effect:** $\\Delta_t = y_t - \\tilde{y}_t$\n",
26+
"- **Cumulative effect (up to time $T$):** $C_T = \\sum_{t=1}^T \\Delta_t$\n",
27+
"\n",
28+
"In Bayesian analysis, both $\\tilde{y}_t$ and $\\Delta_t$ are distributions, not just point estimates.\n",
29+
"\n",
30+
"### Why does this matter for decision making?\n",
31+
"These metrics allow you to answer questions like:\n",
32+
"- \"How much did the intervention change the outcome, compared to what would have happened otherwise?\"\n",
33+
"- \"What is the total effect of the intervention over time, and how certain are we about it?\"\n",
34+
"\n",
35+
"By providing both instantaneous and cumulative effects, along with their uncertainty, you can make more informed business decisions and better understand the impact of your interventions."
36+
]
37+
},
38+
{
39+
"attachments": {},
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"## Interrupted Time Series example"
1144
]
1245
},
1346
{
@@ -34,14 +67,6 @@
3467
"seed = 42"
3568
]
3669
},
37-
{
38-
"attachments": {},
39-
"cell_type": "markdown",
40-
"metadata": {},
41-
"source": [
42-
"## Interrupted Time Series (ITS) Example"
43-
]
44-
},
4570
{
4671
"attachments": {},
4772
"cell_type": "markdown",
@@ -170,7 +195,11 @@
170195
{
171196
"cell_type": "code",
172197
"execution_count": 4,
173-
"metadata": {},
198+
"metadata": {
199+
"tags": [
200+
"hide-output"
201+
]
202+
},
174203
"outputs": [
175204
{
176205
"name": "stderr",
@@ -304,11 +333,7 @@
304333
"cell_type": "markdown",
305334
"metadata": {},
306335
"source": [
307-
"As well as the model coefficients, we might be interested in the average causal impact and average cumulative causal impact.\n",
308-
"\n",
309-
":::{note}\n",
310-
"Better output for the summary statistics are in progress!\n",
311-
":::"
336+
"As well as the model coefficients, we might be interested in the average causal impact and average cumulative causal impact."
312337
]
313338
},
314339
{
@@ -508,7 +533,7 @@
508533
"name": "python",
509534
"nbconvert_exporter": "python",
510535
"pygments_lexer": "ipython3",
511-
"version": "3.11.9"
536+
"version": "3.13.2"
512537
},
513538
"orig_nbformat": 4,
514539
"vscode": {

0 commit comments

Comments
 (0)