Causal Inference
Dependencies
This skill requires the bayesian-workflow skill for all PyMC modeling steps (priors, sampling,
diagnostics, calibration, reporting).
Detect it:
ls ~/.claude/skills/bayesian-workflow/SKILL.md 2>/dev/null || ls .claude/skills/bayesian-workflow/SKILL.md 2>/dev/null
If not found, install it:
git clone https://github.com/Learning-Bayesian-Statistics/baygent-skills.git /tmp/baygent-skills
cp -r /tmp/baygent-skills/bayesian-workflow ~/.claude/skills/
For all PyMC modeling steps (priors, sampling, diagnostics, calibration, reporting), follow the
bayesian-workflow skill.
Workflow overview
Every causal analysis follows this sequence. Steps 1-4 are the thinking phase (no code). Steps 5-8
are the doing phase. Think before you do.
- Formulate the causal question — Propose precise estimand (ATE, ATT, LATE, etc.). ⚠️ ASK USER TO CONFIRM.
- Draw the DAG — Propose causal graph with nodes, edges, and explicit non-edges. ⚠️ ASK USER TO CONFIRM. See references/dags-and-identification.md
- Identify — Determine identification strategy (backdoor, front-door, IV, RDD, DiD). ⚠️ ASK USER TO CONFIRM untestable assumptions. See references/dags-and-identification.md
- Choose design — Match problem to method using table below. ⚠️ ASK USER TO CONFIRM. See references/quasi-experiments.md or references/structural-models.md
- Estimate — Build and fit the model. Delegate all PyMC mechanics to bayesian-workflow skill.
- Refute — MANDATORY. Run design-specific robustness checks. See references/refutation.md
- Interpret — Effect size + decision-relevant HDIs + probability of direction.
- Report — Generate causal analysis report. See references/reporting.md
Design selection guide
| Design |
Use when |
Key assumption |
Tool |
| DiD |
Treatment at known time, control group available |
Parallel trends |
CausalPy |
| Staggered DiD |
Treatment rolls out at different times |
Parallel trends per cohort |
CausalPy |
| Synthetic Control |
Single treated unit, donor pool available |
Weighted donors approximate counterfactual |
CausalPy |
| ITS |
Time series, intervention at known time, no control |
No confounding event at treatment time |
CausalPy |
| RDD |
Treatment by threshold on running variable |
No manipulation at threshold |
CausalPy |
| IV |
Endogenous treatment, valid instrument |
Exclusion restriction, relevance |
CausalPy |
| IPSW |
Observational data, treatment modeled |
No unmeasured confounders, positivity |
CausalPy |
| Structural (do/observe) |
Full causal theory, model mechanisms |
Correct DAG specification |
PyMC |
| Counterfactual |
"What would Y have been if X differed?" |
Correct structural model |
PyMC |
Critical rules
- No estimation without a confirmed DAG. A causal graph is not optional decoration — it makes
assumptions explicit and determines the adjustment set. If the user resists, explain why the DAG
is non-negotiable before proceeding.
- No causal claims without refutation. Every design has failure modes. Run at minimum one
design-specific robustness check (placebo test, sensitivity analysis, falsification test) before
reporting results. See references/refutation.md.
- State assumptions before results. Lead with what must be true for the estimate to be causal.
Bury the estimate after the assumptions, not before. This is not optional politeness — it prevents
misuse of results.
- Adapt HDIs to the decision context. The bayesian-workflow skill's 94% HDI is a sensible
default; adapt it with explicit explanation when the decision stakes warrant it (e.g., 89% for
exploratory, 97% for high-stakes policy). Report multiple intervals when the decision threshold
matters.
- Downgrade causal language when warranted. If identification assumptions are unverifiable or
refutation raises flags, soften claims: "consistent with a causal effect" not "causes", "estimated
effect" not "true effect". Flag uncertainty loudly in the report.
- Ask the user when domain knowledge is needed. You cannot know whether an instrument is valid,
whether parallel trends holds, or whether a confounder exists without domain expertise. Ask
before assuming.
- Delegate PyMC mechanics to bayesian-workflow. This skill handles causal structure and design.
The bayesian-workflow skill handles priors, sampling, diagnostics, calibration, and reporting
format. Don't duplicate those rules here.
Common gotchas
These are battle-tested lessons that save hours of debugging:
- CausalPy formula syntax uses
C() for categoricals. Passing a string column directly without
C() will silently produce wrong dummy coding. Always wrap categorical treatment and group
variables: "y ~ C(treatment) + C(group)".
- DoWhy requires explicit
U nodes for unobserved confounders. Omitting them from the graph
will make DoWhy treat your model as fully identified when it isn't. Add latent nodes explicitly
and mark them as unobserved.
- CausalPy's PyMC models don't auto-store log-likelihood. Same issue as bayesian-workflow:
nutpie silently drops it. Call
pm.compute_log_likelihood(idata, model=model) after sampling if
you need it for model comparison.
- Parallel trends is untestable in the post-treatment period. Pre-treatment trend tests are
necessary but not sufficient — passing them doesn't prove the assumption holds after treatment.
State this explicitly in every DiD report.
- Synthetic control requires the treated unit to lie within the convex hull of donors. If the
treated unit is an outlier (highest GDP, largest city), no weighted combination of donors can
approximate its counterfactual. Check this before running — if violated, the design is invalid.
- DiD group variable must be dummy-coded (0/1). CausalPy rejects string labels like "treatment"/"control". Use integers: 1 = treatment, 0 = control. Data also requires a
unit column.
- SyntheticControl expects wide-format data. Index = time, columns = unit names, values = outcome. If your data is long format, pivot first:
df.pivot(index="date", columns="unit", values="outcome").
When things go wrong
| Symptom |
Likely cause |
Fix |
| Refutation fails |
Assumption violated |
Diagnose which assumption, try alternative design or sensitivity bounds |
| DiD effect at placebo time |
Parallel trends violated |
Try synthetic control or add group-specific time trends |
| RDD: bunching at threshold |
Manipulation of running variable |
Design is invalid for this threshold — report and stop |
| SC: poor pre-treatment fit |
Donors don't span treated unit |
Add donors, expand donor pool, or reconsider design |
| DoWhy says "not identifiable" |
Insufficient adjustment set |
Revise DAG, add measured variables, or change design |
| CausalPy formula error |
Wrong formula syntax |
Use C() for categoricals, check variable names match dataframe columns |
1---
2name: causal-inference-2
3description: >
4 Production-grade Bayesian causal inference with PyMC, CausalPy, and DoWhy. Enforces DAG-first
5 thinking, mandatory user checkpoints for assumptions, design-specific refutation, and defensible
6 reporting with causal language guardrails. Trigger on: causal inference, causal effect estimation,
7 treatment effects, counterfactuals, difference-in-differences (DiD), synthetic control, regression
8 discontinuity (RDD), interrupted time series (ITS), instrumental variables (IV), propensity scores,
9 DAGs, causal graphs, confounders, backdoor criterion, do-calculus, interventional distributions,
10 pm.do(), pm.observe(), CausalPy, DoWhy, mediation analysis, refutation, sensitivity analysis,
11 parallel trends, placebo tests, or any question of the form "does X cause Y" or "what is the
12 effect of X on Y."
13license: MIT
14metadata:
15 source: https://skillmd.com/skills/brycewang-stanford/causal-inference-2
16 author: "[Alexandre Andorra](https://alexandorra.github.io/)"
17 version: "1.0"
18---
19
20# Causal Inference
21
22## Dependencies
23
24This skill requires the **bayesian-workflow** skill for all PyMC modeling steps (priors, sampling,
25diagnostics, calibration, reporting).
26
27Detect it:
28
29```bash
30ls ~/.claude/skills/bayesian-workflow/SKILL.md 2>/dev/null || ls .claude/skills/bayesian-workflow/SKILL.md 2>/dev/null
31```
32
33If not found, install it:
34
35```bash
36git clone https://github.com/Learning-Bayesian-Statistics/baygent-skills.git /tmp/baygent-skills
37cp -r /tmp/baygent-skills/bayesian-workflow ~/.claude/skills/
38```
39
40For all PyMC modeling steps (priors, sampling, diagnostics, calibration, reporting), follow the
41bayesian-workflow skill.
42
43## Workflow overview
44
45Every causal analysis follows this sequence. Steps 1-4 are the thinking phase (no code). Steps 5-8
46are the doing phase. Think before you do.
47
481. **Formulate the causal question** — Propose precise estimand (ATE, ATT, LATE, etc.). ⚠️ ASK USER TO CONFIRM.
492. **Draw the DAG** — Propose causal graph with nodes, edges, and explicit non-edges. ⚠️ ASK USER TO CONFIRM. See [references/dags-and-identification.md](references/dags-and-identification.md)
503. **Identify** — Determine identification strategy (backdoor, front-door, IV, RDD, DiD). ⚠️ ASK USER TO CONFIRM untestable assumptions. See [references/dags-and-identification.md](references/dags-and-identification.md)
514. **Choose design** — Match problem to method using table below. ⚠️ ASK USER TO CONFIRM. See [references/quasi-experiments.md](references/quasi-experiments.md) or [references/structural-models.md](references/structural-models.md)
525. **Estimate** — Build and fit the model. Delegate all PyMC mechanics to bayesian-workflow skill.
536. **Refute** — MANDATORY. Run design-specific robustness checks. See [references/refutation.md](references/refutation.md)
547. **Interpret** — Effect size + decision-relevant HDIs + probability of direction.
558. **Report** — Generate causal analysis report. See [references/reporting.md](references/reporting.md)
56
57## Design selection guide
58
59| Design | Use when | Key assumption | Tool |
60|---|---|---|---|
61| DiD | Treatment at known time, control group available | Parallel trends | CausalPy |
62| Staggered DiD | Treatment rolls out at different times | Parallel trends per cohort | CausalPy |
63| Synthetic Control | Single treated unit, donor pool available | Weighted donors approximate counterfactual | CausalPy |
64| ITS | Time series, intervention at known time, no control | No confounding event at treatment time | CausalPy |
65| RDD | Treatment by threshold on running variable | No manipulation at threshold | CausalPy |
66| IV | Endogenous treatment, valid instrument | Exclusion restriction, relevance | CausalPy |
67| IPSW | Observational data, treatment modeled | No unmeasured confounders, positivity | CausalPy |
68| Structural (do/observe) | Full causal theory, model mechanisms | Correct DAG specification | PyMC |
69| Counterfactual | "What would Y have been if X differed?" | Correct structural model | PyMC |
70
71## Critical rules
72
73- **No estimation without a confirmed DAG.** A causal graph is not optional decoration — it makes
74 assumptions explicit and determines the adjustment set. If the user resists, explain why the DAG
75 is non-negotiable before proceeding.
76- **No causal claims without refutation.** Every design has failure modes. Run at minimum one
77 design-specific robustness check (placebo test, sensitivity analysis, falsification test) before
78 reporting results. See [references/refutation.md](references/refutation.md).
79- **State assumptions before results.** Lead with what must be true for the estimate to be causal.
80 Bury the estimate after the assumptions, not before. This is not optional politeness — it prevents
81 misuse of results.
82- **Adapt HDIs to the decision context.** The bayesian-workflow skill's 94% HDI is a sensible
83 default; adapt it with explicit explanation when the decision stakes warrant it (e.g., 89% for
84 exploratory, 97% for high-stakes policy). Report multiple intervals when the decision threshold
85 matters.
86- **Downgrade causal language when warranted.** If identification assumptions are unverifiable or
87 refutation raises flags, soften claims: "consistent with a causal effect" not "causes", "estimated
88 effect" not "true effect". Flag uncertainty loudly in the report.
89- **Ask the user when domain knowledge is needed.** You cannot know whether an instrument is valid,
90 whether parallel trends holds, or whether a confounder exists without domain expertise. Ask
91 before assuming.
92- **Delegate PyMC mechanics to bayesian-workflow.** This skill handles causal structure and design.
93 The bayesian-workflow skill handles priors, sampling, diagnostics, calibration, and reporting
94 format. Don't duplicate those rules here.
95
96## Common gotchas
97
98These are battle-tested lessons that save hours of debugging:
99
100- **CausalPy formula syntax uses `C()` for categoricals.** Passing a string column directly without
101 `C()` will silently produce wrong dummy coding. Always wrap categorical treatment and group
102 variables: `"y ~ C(treatment) + C(group)"`.
103- **DoWhy requires explicit `U` nodes for unobserved confounders.** Omitting them from the graph
104 will make DoWhy treat your model as fully identified when it isn't. Add latent nodes explicitly
105 and mark them as unobserved.
106- **CausalPy's PyMC models don't auto-store log-likelihood.** Same issue as bayesian-workflow:
107 nutpie silently drops it. Call `pm.compute_log_likelihood(idata, model=model)` after sampling if
108 you need it for model comparison.
109- **Parallel trends is untestable in the post-treatment period.** Pre-treatment trend tests are
110 necessary but not sufficient — passing them doesn't prove the assumption holds after treatment.
111 State this explicitly in every DiD report.
112- **Synthetic control requires the treated unit to lie within the convex hull of donors.** If the
113 treated unit is an outlier (highest GDP, largest city), no weighted combination of donors can
114 approximate its counterfactual. Check this before running — if violated, the design is invalid.
115- **DiD group variable must be dummy-coded (0/1).** CausalPy rejects string labels like "treatment"/"control". Use integers: 1 = treatment, 0 = control. Data also requires a `unit` column.
116- **SyntheticControl expects wide-format data.** Index = time, columns = unit names, values = outcome. If your data is long format, pivot first: `df.pivot(index="date", columns="unit", values="outcome")`.
117
118## When things go wrong
119
120| Symptom | Likely cause | Fix |
121|---|---|---|
122| Refutation fails | Assumption violated | Diagnose which assumption, try alternative design or sensitivity bounds |
123| DiD effect at placebo time | Parallel trends violated | Try synthetic control or add group-specific time trends |
124| RDD: bunching at threshold | Manipulation of running variable | Design is invalid for this threshold — report and stop |
125| SC: poor pre-treatment fit | Donors don't span treated unit | Add donors, expand donor pool, or reconsider design |
126| DoWhy says "not identifiable" | Insufficient adjustment set | Revise DAG, add measured variables, or change design |
127| CausalPy formula error | Wrong formula syntax | Use `C()` for categoricals, check variable names match dataframe columns |