Goodman-Bacon decomposition

The decomposition of a two-way fixed effects estimate on a simulated panel, the same with sampling weights, and the covariate-adjusted decomposition on the Castle Doctrine panel.

bacon.py

from diff_stream import bacon_decomp
from diff_stream.simulate import simulate_panel

data = simulate_panel(2000, 8, seed=72)
bacon = bacon_decomp(
    "y ~ 1",
    data,
    panel=("id", "time"),
    cohort="first_treat",
    n_threads=2,
)
print("TWFE:", bacon.twfe_estimate)
print("Reconstructed:", bacon.decomposed_estimate)
print(bacon.summary())
print(bacon.comparisons())

Output:

TWFE: 1.1932584577189762
Reconstructed: 1.1932584577189762
shape: (3, 5)
┌──────────────────┬───────────────┬──────────┬──────────┬──────────────┐
│ comparison_type  ┆ n_comparisons ┆ weight   ┆ estimate ┆ contribution │
│ ---              ┆ ---           ┆ ---      ┆ ---      ┆ ---          │
│ str              ┆ u32           ┆ f64      ┆ f64      ┆ f64          │
╞══════════════════╪═══════════════╪══════════╪══════════╪══════════════╡
│ treated_vs_never ┆ 7             ┆ 0.268153 ┆ 1.558077 ┆ 0.417803     │
│ earlier_vs_later ┆ 21            ┆ 0.369811 ┆ 1.281448 ┆ 0.473893     │
│ later_vs_earlier ┆ 21            ┆ 0.362037 ┆ 0.832962 ┆ 0.301563     │
└──────────────────┴───────────────┴──────────┴──────────┴──────────────┘
shape: (49, 17)
┌────────────┬───────────┬───────────┬───────────┬───┬───────────┬──────────┬──────────┬───────────┐
│ treated_co ┆ control_c ┆ compariso ┆ control_a ┆ … ┆ control_w ┆ estimate ┆ weight   ┆ contribut │
│ hort       ┆ ohort     ┆ n_type    ┆ lready_tr ┆   ┆ eight_tot ┆ ---      ┆ ---      ┆ ion       │
│ ---        ┆ ---       ┆ ---       ┆ eated     ┆   ┆ al        ┆ f64      ┆ f64      ┆ ---       │
│ i64        ┆ i64       ┆ str       ┆ ---       ┆   ┆ ---       ┆          ┆          ┆ f64       │
│            ┆           ┆           ┆ bool      ┆   ┆ f64       ┆          ┆          ┆           │
╞════════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪══════════╪══════════╪═══════════╡
│ 2          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.482616 ┆ 0.020284 ┆ 0.030074  │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 3          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.512247 ┆ 0.036612 ┆ 0.055366  │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 4          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.575516 ┆ 0.050743 ┆ 0.079946  │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 5          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.530619 ┆ 0.0529   ┆ 0.08097   │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 6          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.679308 ┆ 0.046339 ┆ 0.077817  │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 7          ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 268.0     ┆ 1.484664 ┆ 0.039828 ┆ 0.059132  │
│            ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆          ┆           │
│ …          ┆ …         ┆ …         ┆ …         ┆ … ┆ …         ┆ …        ┆ …        ┆ …         │
│ 6          ┆ 7         ┆ earlier_v ┆ false     ┆ … ┆ 260.0     ┆ 1.532908 ┆ 0.014985 ┆ 0.022971  │
│            ┆           ┆ s_later   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 7          ┆ 6         ┆ later_vs_ ┆ true      ┆ … ┆ 242.0     ┆ 1.211004 ┆ 0.005994 ┆ 0.007259  │
│            ┆           ┆ earlier   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 6          ┆ 8         ┆ earlier_v ┆ false     ┆ … ┆ 240.0     ┆ 1.510373 ┆ 0.027665 ┆ 0.041784  │
│            ┆           ┆ s_later   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 8          ┆ 6         ┆ later_vs_ ┆ true      ┆ … ┆ 242.0     ┆ 1.118001 ┆ 0.005533 ┆ 0.006186  │
│            ┆           ┆ earlier   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 7          ┆ 8         ┆ earlier_v ┆ false     ┆ … ┆ 240.0     ┆ 1.408877 ┆ 0.017834 ┆ 0.025125  │
│            ┆           ┆ s_later   ┆           ┆   ┆           ┆          ┆          ┆           │
│ 8          ┆ 7         ┆ later_vs_ ┆ true      ┆ … ┆ 260.0     ┆ 1.333604 ┆ 0.002972 ┆ 0.003964  │
│            ┆           ┆ earlier   ┆           ┆   ┆           ┆          ┆          ┆           │
└────────────┴───────────┴───────────┴───────────┴───┴───────────┴──────────┴──────────┴───────────┘

bacon_weights.py

import polars as pl

from diff_stream import bacon_decomp
from diff_stream.simulate import simulate_panel

data = simulate_panel(2000, 8, seed=72).with_columns(
    (0.2 + pl.col("id") % 9).alias("sampling_weight")
)
bacon = bacon_decomp(
    "y ~ 1",
    data,
    panel=("id", "time"),
    cohort="first_treat",
    weights="sampling_weight",
    n_threads=2,
)
print("Weighted TWFE:", bacon.twfe_estimate)
print("Reconstructed:", bacon.decomposed_estimate)
print(bacon.summary())
print(
    bacon.comparisons().select(
        "treated_cohort",
        "control_cohort",
        "n_treated_units",
        "treated_weight_total",
        "estimate",
        "weight",
        "contribution",
    )
)

Output:

Weighted TWFE: 1.2112700441630821
Reconstructed: 1.211270044163082
shape: (3, 5)
┌──────────────────┬───────────────┬──────────┬──────────┬──────────────┐
│ comparison_type  ┆ n_comparisons ┆ weight   ┆ estimate ┆ contribution │
│ ---              ┆ ---           ┆ ---      ┆ ---      ┆ ---          │
│ str              ┆ u32           ┆ f64      ┆ f64      ┆ f64          │
╞══════════════════╪═══════════════╪══════════╪══════════╪══════════════╡
│ treated_vs_never ┆ 7             ┆ 0.266925 ┆ 1.582843 ┆ 0.4225       │
│ earlier_vs_later ┆ 21            ┆ 0.371993 ┆ 1.31287  ┆ 0.488379     │
│ later_vs_earlier ┆ 21            ┆ 0.361082 ┆ 0.83192  ┆ 0.300392     │
└──────────────────┴───────────────┴──────────┴──────────┴──────────────┘
shape: (49, 7)
┌───────────────┬───────────────┬──────────────┬──────────────┬──────────┬──────────┬──────────────┐
│ treated_cohor ┆ control_cohor ┆ n_treated_un ┆ treated_weig ┆ estimate ┆ weight   ┆ contribution │
│ t             ┆ t             ┆ its          ┆ ht_total     ┆ ---      ┆ ---      ┆ ---          │
│ ---           ┆ ---           ┆ ---          ┆ ---          ┆ f64      ┆ f64      ┆ f64          │
│ i64           ┆ i64           ┆ i64          ┆ f64          ┆          ┆          ┆              │
╞═══════════════╪═══════════════╪══════════════╪══════════════╪══════════╪══════════╪══════════════╡
│ 2             ┆ 0             ┆ 227          ┆ 911.4        ┆ 1.559341 ┆ 0.019236 ┆ 0.029995     │
│ 3             ┆ 0             ┆ 239          ┆ 1039.8       ┆ 1.555442 ┆ 0.037622 ┆ 0.058518     │
│ 4             ┆ 0             ┆ 265          ┆ 1095.0       ┆ 1.607016 ┆ 0.049524 ┆ 0.079585     │
│ 5             ┆ 0             ┆ 259          ┆ 1097.8       ┆ 1.546081 ┆ 0.05296  ┆ 0.081881     │
│ 6             ┆ 0             ┆ 242          ┆ 1024.4       ┆ 1.70938  ┆ 0.046331 ┆ 0.079197     │
│ 7             ┆ 0             ┆ 260          ┆ 1109.0       ┆ 1.460562 ┆ 0.040125 ┆ 0.058606     │
│ …             ┆ …             ┆ …            ┆ …            ┆ …        ┆ …        ┆ …            │
│ 6             ┆ 7             ┆ 242          ┆ 1024.4       ┆ 1.620836 ┆ 0.015366 ┆ 0.024906     │
│ 7             ┆ 6             ┆ 260          ┆ 1109.0       ┆ 1.206491 ┆ 0.006146 ┆ 0.007416     │
│ 6             ┆ 8             ┆ 242          ┆ 1024.4       ┆ 1.503149 ┆ 0.027739 ┆ 0.041696     │
│ 8             ┆ 6             ┆ 240          ┆ 1001.0       ┆ 1.089067 ┆ 0.005548 ┆ 0.006042     │
│ 7             ┆ 8             ┆ 260          ┆ 1109.0       ┆ 1.306509 ┆ 0.018018 ┆ 0.023541     │
│ 8             ┆ 7             ┆ 240          ┆ 1001.0       ┆ 1.292978 ┆ 0.003003 ┆ 0.003883     │
└───────────────┴───────────────┴──────────────┴──────────────┴──────────┴──────────┴──────────────┘

bacon_covariates.py

from pathlib import Path

import polars as pl

from diff_stream import bacon_decomp

ROOT = Path(__file__).resolve().parents[1]
data = pl.read_csv(ROOT / "tests/data/bacon_castle_covariates.csv")
result = bacon_decomp(
    "l_homicide ~ l_pop + l_income",
    data,
    panel=("state", "year"),
    cohort="first_treat",
    n_threads=2,
)
print("TWFE:", result.twfe_estimate)
print(result.summary())
print(result.within_timing())
print(result.comparisons())
print("Reconstruction error:", result.reconstruction_error)

Output:

TWFE: 0.09058532274239066
shape: (3, 5)
┌──────────────────┬───────────────┬──────────┬──────────┬──────────────┐
│ comparison_type  ┆ n_comparisons ┆ weight   ┆ estimate ┆ contribution │
│ ---              ┆ ---           ┆ ---      ┆ ---      ┆ ---          │
│ str              ┆ u32           ┆ f64      ┆ f64      ┆ f64          │
╞══════════════════╪═══════════════╪══════════╪══════════╪══════════════╡
│ treated_vs_never ┆ 5             ┆ 0.896422 ┆ 0.090022 ┆ 0.080698     │
│ both_treated     ┆ 10            ┆ 0.089973 ┆ 0.027237 ┆ 0.002451     │
│ within_timing    ┆ 1             ┆ 0.013605 ┆ 0.546626 ┆ 0.007437     │
└──────────────────┴───────────────┴──────────┴──────────┴──────────────┘
shape: (1, 4)
┌─────────────────┬──────────┬──────────┬──────────────┐
│ comparison_type ┆ weight   ┆ estimate ┆ contribution │
│ ---             ┆ ---      ┆ ---      ┆ ---          │
│ str             ┆ f64      ┆ f64      ┆ f64          │
╞═════════════════╪══════════╪══════════╪══════════════╡
│ within_timing   ┆ 0.013605 ┆ 0.546626 ┆ 0.007437     │
└─────────────────┴──────────┴──────────┴──────────────┘
shape: (15, 18)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬──────────┬───────────┬───────────┐
│ treated_c ┆ control_c ┆ compariso ┆ control_a ┆ … ┆ estimate  ┆ weight   ┆ contribut ┆ between_w │
│ ohort     ┆ ohort     ┆ n_type    ┆ lready_tr ┆   ┆ ---       ┆ ---      ┆ ion       ┆ eight     │
│ ---       ┆ ---       ┆ ---       ┆ eated     ┆   ┆ f64       ┆ f64      ┆ ---       ┆ ---       │
│ i64       ┆ i64       ┆ str       ┆ ---       ┆   ┆           ┆          ┆ f64       ┆ f64       │
│           ┆           ┆           ┆ bool      ┆   ┆           ┆          ┆           ┆           │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪══════════╪═══════════╪═══════════╡
│ 2005      ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 0.079433  ┆ 0.046102 ┆ 0.003662  ┆ 0.046738  │
│           ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2006      ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 0.070326  ┆ 0.575313 ┆ 0.04046   ┆ 0.583248  │
│           ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2007      ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 0.11494   ┆ 0.172155 ┆ 0.019788  ┆ 0.17453   │
│           ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2008      ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 0.151013  ┆ 0.07461  ┆ 0.011267  ┆ 0.075639  │
│           ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2009      ┆ 0         ┆ treated_v ┆ false     ┆ … ┆ 0.195505  ┆ 0.028242 ┆ 0.005521  ┆ 0.028632  │
│           ┆           ┆ s_never   ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2005      ┆ 2006      ┆ both_trea ┆ null      ┆ … ┆ -0.108187 ┆ 0.007395 ┆ -0.0008   ┆ 0.007497  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ …         ┆ …         ┆ …         ┆ …         ┆ … ┆ …         ┆ …        ┆ …         ┆ …         │
│ 2006      ┆ 2007      ┆ both_trea ┆ null      ┆ … ┆ 0.108842  ┆ 0.028478 ┆ 0.0031    ┆ 0.028871  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2006      ┆ 2008      ┆ both_trea ┆ null      ┆ … ┆ 0.051003  ┆ 0.023135 ┆ 0.00118   ┆ 0.023454  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2006      ┆ 2009      ┆ both_trea ┆ null      ┆ … ┆ -0.049818 ┆ 0.014662 ┆ -0.00073  ┆ 0.014864  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2007      ┆ 2008      ┆ both_trea ┆ null      ┆ … ┆ 0.13489   ┆ 0.003823 ┆ 0.000516  ┆ 0.003876  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2007      ┆ 2009      ┆ both_trea ┆ null      ┆ … ┆ -0.026249 ┆ 0.003373 ┆ -0.000089 ┆ 0.00342   │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
│ 2008      ┆ 2009      ┆ both_trea ┆ null      ┆ … ┆ -0.216043 ┆ 0.000948 ┆ -0.000205 ┆ 0.000961  │
│           ┆           ┆ ted       ┆           ┆   ┆           ┆          ┆           ┆           │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴──────────┴───────────┴───────────┘
Reconstruction error: -2.7755575615628914e-17