Sampling weights

The three estimators with a sampling weight per unit. weight_units counts the units behind each cell, weight_total their weights.

import polars as pl

from diff_stream import did_bjs, did_callaway_santanna, did_sun_abraham
from diff_stream.simulate import simulate_conditional_panel

data = simulate_conditional_panel(1200, 8, seed=72).with_columns(
    (0.2 + pl.col("id") % 9).alias("sampling_weight")
)
for estimator in (did_callaway_santanna, did_sun_abraham, did_bjs):
    fit = estimator(
        "y ~ x + z" if estimator is did_callaway_santanna else "y ~ 1",
        data,
        panel=("id", "time"),
        cohort="first_treat",
        weights="sampling_weight",
        anticipation=2,
        n_threads=2,
    )
    try:
        print(estimator.__name__)
        print(fit.aggregate("overall").tidy())
        print(fit.cohort_time().select("cohort", "time", "weight_units", "weight_total"))
    finally:
        fit.cleanup()

Output:

did_callaway_santanna
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.313632 ┆ 0.08104    ┆ 16.209714 ┆ 4.3057e-59 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (21, 4)
┌────────┬──────┬──────────────┬──────────────┐
│ cohort ┆ time ┆ weight_units ┆ weight_total │
│ ---    ┆ ---  ┆ ---          ┆ ---          │
│ i64    ┆ i64  ┆ i64          ┆ f64          │
╞════════╪══════╪══════════════╪══════════════╡
│ 6      ┆ 2    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 3    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 4    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 5    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 6    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 7    ┆ 270          ┆ 1170.0       │
│ …      ┆ …    ┆ …            ┆ …            │
│ 8      ┆ 3    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 4    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 5    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 6    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 7    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 8    ┆ 299          ┆ 1237.8       │
└────────┴──────┴──────────────┴──────────────┘
did_sun_abraham
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.442636 ┆ 0.119136   ┆ 12.109195 ┆ 9.4417e-34 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (21, 4)
┌────────┬──────┬──────────────┬──────────────┐
│ cohort ┆ time ┆ weight_units ┆ weight_total │
│ ---    ┆ ---  ┆ ---          ┆ ---          │
│ i64    ┆ i64  ┆ i64          ┆ f64          │
╞════════╪══════╪══════════════╪══════════════╡
│ 6      ┆ 1    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 2    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 4    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 5    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 6    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 7    ┆ 270          ┆ 1170.0       │
│ …      ┆ …    ┆ …            ┆ …            │
│ 8      ┆ 2    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 3    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 4    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 6    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 7    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 8    ┆ 299          ┆ 1237.8       │
└────────┴──────┴──────────────┴──────────────┘
did_bjs
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.484825 ┆ 0.133304   ┆ 11.138612 ┆ 8.1377e-29 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (12, 4)
┌────────┬──────┬──────────────┬──────────────┐
│ cohort ┆ time ┆ weight_units ┆ weight_total │
│ ---    ┆ ---  ┆ ---          ┆ ---          │
│ i64    ┆ i64  ┆ i64          ┆ f64          │
╞════════╪══════╪══════════════╪══════════════╡
│ 6      ┆ 4    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 5    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 6    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 7    ┆ 270          ┆ 1170.0       │
│ 6      ┆ 8    ┆ 270          ┆ 1170.0       │
│ 7      ┆ 5    ┆ 340          ┆ 1431.0       │
│ 7      ┆ 6    ┆ 340          ┆ 1431.0       │
│ 7      ┆ 7    ┆ 340          ┆ 1431.0       │
│ 7      ┆ 8    ┆ 340          ┆ 1431.0       │
│ 8      ┆ 6    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 7    ┆ 299          ┆ 1237.8       │
│ 8      ┆ 8    ┆ 299          ┆ 1237.8       │
└────────┴──────┴──────────────┴──────────────┘