Bayesian Hierarchical Staggered Rollout Estimator
Generate a self-contained PyMC script to estimate causal treatment effects under staggered adoption with unit-level heterogeneity.
Apply this template when observational panel data involves staggered policy interventions and varying group treatment effects. It generates a full Bayesian MCMC estimation and posterior predictive diagnostic script.
Role: Senior Causal Inference Statistician and Bayesian Modeler
Context
- Treatment Assignment: {{treatment_assignment_variable}}
- Unit Identification: {{panel_unit_identifier}}
- Temporal Dimension: {{temporal_index_variable}}
- Prior Philosophy: {{prior_specification_level}}
- Sampling Budget: {{mcmc_draws_target}}
- Likelihood Family: {{outcome_distribution_family}}
Task
Author an end-to-end Bayesian hierarchical panel regression script using PyMC and ArviZ that estimates dynamic Average Treatment Effects on the Treated (ATT) under staggered adoption, accounting for two-way fixed effects and group-time heterogeneity without two-way fixed effects bias.
Method
- Ingest panel data, indexing {{panel_unit_identifier}} and {{temporal_index_variable}} into dense categorical tensor coordinates for PyMC.
- Engineer relative event-time indices ($k = t - g$) tracking relative duration since {{treatment_assignment_variable}} activation.
- Formulate the hierarchical model structure: baseline unit intercepts, temporal drift components, and event-time treatment parameters.
- Assign prior distributions conforming strictly to {{prior_specification_level}}, utilizing non-centered parameterizations for group variances.
- Specify the observation likelihood matching {{outcome_distribution_family}} to model heteroskedasticity and outlier contamination.
- Execute No-U-Turn Sampler (NUTS) with target parameters from {{mcmc_draws_target}}, capturing energy diagnostics and divergences.
- Compute posterior predictive distributions and calculate synthetic counterfactual trajectory plots for treated units.
- Extract dynamic event-study ATT estimates with 94% highest density intervals (HDI) and calculate the cumulative causal impact.
Constraints
- Code MUST compile under PyMC v5+ using standard ArviZ and Aesara/PyTensor syntax.
- MUST include explicit divergence tracking, $\hat{R}$ (Gelman-Rubin) convergence assertions, and effective sample size (ESS) checks.
- MUST NOT use naive pooled OLS estimators or standard difference-in-differences with homogeneous dynamic effect assumptions.
- Model coordinates and dimensions MUST be defined using PyMC
coordsanddimsschema rather than hardcoded array indices.
Output format
Deliver the technical artifact divided into three exact parts:
- Model Architecture & DAG: A 200-word mathematical and structural explanation of the hierarchical parameterization.
- Complete PyMC Python Script: Executable script containing synthetic data generation for testing, PyMC model definition, sampling call, and convergence evaluation.
- Convergence & Inference Protocol: Structured markdown summary listing diagnostic thresholds (divergences = 0, $\hat{R} < 1.01$) and posterior interpretation guidelines.
Self-review
- Confirm that the event-time indexing handles never-treated units without producing missing coordinate keys.
- Verify that non-centered parameterization is correctly implemented on unit-level random intercepts.
- Ensure the likelihood specification perfectly matches {{outcome_distribution_family}} with appropriate dispersion priors.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.