|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Benchmark: WooldridgeDiD (ETWFE) Estimator (diff-diff WooldridgeDiD). |
| 4 | +
|
| 5 | +Validates OLS ETWFE ATT(g,t) against Callaway-Sant'Anna on mpdta data |
| 6 | +(Proposition 3.1 equivalence), and measures estimation timing. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python benchmark_wooldridge.py --data path/to/mpdta.csv --output path/to/results.json |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +# IMPORTANT: Parse --backend and set environment variable BEFORE importing diff_diff |
| 19 | +def _get_backend_from_args(): |
| 20 | + """Parse --backend argument without importing diff_diff.""" |
| 21 | + parser = argparse.ArgumentParser(add_help=False) |
| 22 | + parser.add_argument("--backend", default="auto", choices=["auto", "python", "rust"]) |
| 23 | + args, _ = parser.parse_known_args() |
| 24 | + return args.backend |
| 25 | + |
| 26 | +_requested_backend = _get_backend_from_args() |
| 27 | +if _requested_backend in ("python", "rust"): |
| 28 | + os.environ["DIFF_DIFF_BACKEND"] = _requested_backend |
| 29 | + |
| 30 | +# NOW import diff_diff and other dependencies (will see the env var) |
| 31 | +import pandas as pd |
| 32 | + |
| 33 | +# Add parent to path for imports |
| 34 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) |
| 35 | + |
| 36 | +from diff_diff import WooldridgeDiD, HAS_RUST_BACKEND |
| 37 | +from benchmarks.python.utils import Timer |
| 38 | + |
| 39 | + |
| 40 | +def parse_args(): |
| 41 | + parser = argparse.ArgumentParser( |
| 42 | + description="Benchmark WooldridgeDiD (ETWFE) estimator" |
| 43 | + ) |
| 44 | + parser.add_argument("--data", required=True, help="Path to input CSV data (mpdta format)") |
| 45 | + parser.add_argument("--output", required=True, help="Path to output JSON results") |
| 46 | + parser.add_argument( |
| 47 | + "--backend", default="auto", choices=["auto", "python", "rust"], |
| 48 | + help="Backend to use: auto (default), python (pure Python), rust (Rust backend)" |
| 49 | + ) |
| 50 | + return parser.parse_args() |
| 51 | + |
| 52 | + |
| 53 | +def get_actual_backend() -> str: |
| 54 | + """Return the actual backend being used based on HAS_RUST_BACKEND.""" |
| 55 | + return "rust" if HAS_RUST_BACKEND else "python" |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + args = parse_args() |
| 60 | + |
| 61 | + actual_backend = get_actual_backend() |
| 62 | + print(f"Using backend: {actual_backend}") |
| 63 | + |
| 64 | + print(f"Loading data from: {args.data}") |
| 65 | + df = pd.read_csv(args.data) |
| 66 | + |
| 67 | + # Run OLS ETWFE estimation |
| 68 | + print("Running WooldridgeDiD (OLS ETWFE) estimation...") |
| 69 | + est = WooldridgeDiD(method="ols", control_group="not_yet_treated") |
| 70 | + |
| 71 | + with Timer() as estimation_timer: |
| 72 | + results = est.fit( |
| 73 | + df, |
| 74 | + outcome="lemp", |
| 75 | + unit="countyreal", |
| 76 | + time="year", |
| 77 | + cohort="first_treat", |
| 78 | + ) |
| 79 | + |
| 80 | + estimation_time = estimation_timer.elapsed |
| 81 | + |
| 82 | + # Compute event study aggregation |
| 83 | + results.aggregate("event") |
| 84 | + total_time = estimation_timer.elapsed |
| 85 | + |
| 86 | + # Store data info |
| 87 | + n_units = len(df["countyreal"].unique()) |
| 88 | + n_periods = len(df["year"].unique()) |
| 89 | + n_obs = len(df) |
| 90 | + |
| 91 | + # Format ATT(g,t) effects |
| 92 | + gt_effects_out = [] |
| 93 | + for (g, t), cell in sorted(results.group_time_effects.items()): |
| 94 | + gt_effects_out.append({ |
| 95 | + "cohort": int(g), |
| 96 | + "time": int(t), |
| 97 | + "att": float(cell["att"]), |
| 98 | + "se": float(cell["se"]), |
| 99 | + }) |
| 100 | + |
| 101 | + # Format event study effects |
| 102 | + es_effects = [] |
| 103 | + if results.event_study_effects: |
| 104 | + for rel_t, effect_data in sorted(results.event_study_effects.items()): |
| 105 | + es_effects.append({ |
| 106 | + "event_time": int(rel_t), |
| 107 | + "att": float(effect_data["att"]), |
| 108 | + "se": float(effect_data["se"]), |
| 109 | + }) |
| 110 | + |
| 111 | + output = { |
| 112 | + "estimator": "diff_diff.WooldridgeDiD", |
| 113 | + "method": "ols", |
| 114 | + "control_group": "not_yet_treated", |
| 115 | + "backend": actual_backend, |
| 116 | + # Overall ATT |
| 117 | + "overall_att": float(results.overall_att), |
| 118 | + "overall_se": float(results.overall_se), |
| 119 | + # Group-time ATT(g,t) |
| 120 | + "group_time_effects": gt_effects_out, |
| 121 | + # Event study |
| 122 | + "event_study": es_effects, |
| 123 | + # Timing |
| 124 | + "timing": { |
| 125 | + "estimation_seconds": estimation_time, |
| 126 | + "total_seconds": total_time, |
| 127 | + }, |
| 128 | + # Metadata |
| 129 | + "metadata": { |
| 130 | + "n_units": n_units, |
| 131 | + "n_periods": n_periods, |
| 132 | + "n_obs": n_obs, |
| 133 | + "n_cohorts": len(results.groups), |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + print(f"Writing results to: {args.output}") |
| 138 | + output_path = Path(args.output) |
| 139 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 140 | + with open(output_path, "w") as f: |
| 141 | + json.dump(output, f, indent=2) |
| 142 | + |
| 143 | + print(f"Overall ATT: {results.overall_att:.6f} (SE: {results.overall_se:.6f})") |
| 144 | + print(f"Completed in {total_time:.3f} seconds") |
| 145 | + return output |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments