Skip to content

Commit 5e3d0b0

Browse files
committed
#34 HDI on causal impact + plot posterior in notebook
1 parent 63d8466 commit 5e3d0b0

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

causalpy/pymc_experiments.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,17 @@ def __init__(
257257

258258
# calculate causal impact
259259
self.causal_impact = (
260-
self.y_pred_treatment["posterior_predictive"]
261-
.mu.isel({"obs_ind": 1})
262-
.mean()
263-
.data
264-
- self.y_pred_counterfactual["posterior_predictive"].mu.mean().data
260+
self.y_pred_treatment["posterior_predictive"].mu.isel({"obs_ind": 1})
261+
- self.y_pred_counterfactual["posterior_predictive"].mu.squeeze()
265262
)
263+
# self.causal_impact = (
264+
# self.y_pred_treatment["posterior_predictive"]
265+
# .mu.isel({"obs_ind": 1})
266+
# .stack(samples=["chain", "draw"])
267+
# - self.y_pred_counterfactual["posterior_predictive"]
268+
# .mu.stack(samples=["chain", "draw"])
269+
# .squeeze()
270+
# )
266271

267272
def plot(self):
268273
"""Plot the results"""
@@ -348,17 +353,25 @@ def plot(self):
348353
xlim=[-0.15, 1.25],
349354
xticks=[0, 1],
350355
xticklabels=["pre", "post"],
351-
title=f"Causal impact = {self.causal_impact:.2f}",
356+
title=self._causal_impact_summary_stat(),
352357
)
353358
ax.legend(fontsize=LEGEND_FONT_SIZE)
354359
return (fig, ax)
355360

361+
def _causal_impact_summary_stat(self):
362+
percentiles = self.causal_impact.quantile([0.03, 1 - 0.03]).values
363+
ci = r"$CI_{94\%}$" + f"[{percentiles[0]:.2f}, {percentiles[1]:.2f}]"
364+
causal_impact = f"{self.causal_impact.mean():.2f}, "
365+
return f"Causal impact = {causal_impact + ci}"
366+
356367
def summary(self):
357368
"""Print text output summarising the results"""
358369

359370
print(f"{self.expt_type:=^80}")
360371
print(f"Formula: {self.formula}")
372+
print("\nResults:")
361373
# TODO: extra experiment specific outputs here
374+
print(self._causal_impact_summary_stat())
362375
self.print_coefficients()
363376

364377

docs/notebooks/did_pymc.ipynb

Lines changed: 28 additions & 4 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)