Skip to main content

Difference-in-Differences with Heterogeneous Adoption Design (HAD) estimator

Project description

did-had

A Python implementation of the Heterogeneous Adoption Design (HAD) estimator for difference-in-differences analysis from de Chaisemartin et al. (2025).

PyPI version Python 3.8+ License: MIT

Overview

The did-had package implements the HAD estimator for settings where:

  • All groups receive treatment but with different intensities (no pure control group)
  • Treatment adoption occurs at the same time for all groups, but doses vary
  • Some groups have treatment doses close to zero, serving as "quasi-untreated" groups

This is a Python port of the official Stata package did_had, producing numerically identical results.

Installation

pip install did-had

For plotting support:

pip install did-had[plot]

Quick Start

import pandas as pd
from did_had import DidHad

# Load your panel data
df = pd.read_stata("tutorial_data.dta")

# Create and fit the model
model = DidHad(kernel="tri")
results = model.fit(
    df=df,
    outcome="y",        # outcome variable
    group="g",          # group identifier
    time="t",           # time period
    treatment="d",      # treatment dose
    effects=5,          # post-treatment periods
    placebo=4           # pre-treatment placebos
)

# View results
print(results)

# Get ATT
print(f"Average Treatment Effect: {results.att():.4f}")

# Save results
model.save_results("results.csv", format="csv")

Example Output

===========================================================================
DID-HAD Estimation Results
===========================================================================
Number of groups: 1,000
Number of periods: 10
Adoption period (F): 6.0
Kernel: tri
Confidence level: 95%

---------------------------------------------------------------------------
                          Effect Estimates                      QUG* Test
         --------------------------------------------------- ---------------
          Estimate       SE     LB.CI     UB.CI     N      BW    N.BW        T    p.val
Effect_1   4.28198  0.55814   2.71751   4.90538 1,000 0.36133    371  3.96182  0.20154
Effect_2   3.59260  0.66675   2.02563   4.63925 1,000 0.27104    282  3.96182  0.20154
Effect_3   4.25466  0.67544   2.71354   5.36123 1,000 0.31407    324  3.96182  0.20154
...

Features

  • Exact replication of Stata's did_had command
  • Bias-corrected inference using local polynomial regression (lprobust-style)
  • Quasi-untreated group tests to validate the estimation strategy
  • Event-study plots for visualization
  • Multiple output formats: CSV, Stata, Excel, pickle

API Reference

DidHad Class

DidHad(kernel="epa", alpha=0.05, nnmatch=3)

Parameters:

  • kernel: Kernel function - "epa" (Epanechnikov), "tri" (triangular), "uni" (uniform), "gau" (Gaussian)
  • alpha: Significance level for confidence intervals (default: 0.05 for 95% CI)
  • nnmatch: Number of nearest neighbors for variance estimation

fit() Method

results = model.fit(
    df,                      # Panel DataFrame
    outcome,                 # Outcome variable name
    group,                   # Group identifier name
    time,                    # Time period name
    treatment,               # Treatment dose name
    effects=1,               # Number of post-treatment periods
    placebo=0,               # Number of pre-treatment placebos
    dynamic=False,           # Use cumulative treatment dose
    bandwidth=None,          # Global bandwidth (or Silverman if None)
    bandwidth_effect=None,   # Effect-specific bandwidths (dict or scalar)
    bandwidth_placebo=None   # Placebo-specific bandwidths (dict or scalar)
)

DidHadResults Object

results.summary()           # Formatted summary string
results.to_dataframe()      # Full results as DataFrame
results.att()               # Average treatment effect on treated
results.effects             # DataFrame of effect estimates
results.placebos            # DataFrame of placebo estimates

Plotting

# Basic plot
model.plot()

# Customized plot
model.plot(
    figsize=(10, 6),
    title="My Event Study",
    xlabel="Periods since treatment",
    ylabel="Treatment Effect",
    show_ci=True
)

Saving Results

model.save_results("results.csv", format="csv")
model.save_results("results.dta", format="stata")
model.save_results("results.xlsx", format="excel")
model.save_results("results.pkl", format="pickle")

Comparison with Stata

This package produces numerically identical results to the official Stata did_had command when using the same bandwidths:

Estimate Python Stata Match
Effect_1 4.28198 4.28198 Yes
Effect_2 3.59260 3.59260 Yes
Effect_3 4.25466 4.25466 Yes
... ... ... ...

Requirements

  • Python >= 3.8
  • NumPy >= 1.20.0
  • Pandas >= 1.3.0
  • Matplotlib >= 3.4.0 (optional, for plotting)

References

  • de Chaisemartin, C., D'Haultfoeuille, X., Pasquier, F., & Vazquez-Bare, G. (2025). "Difference-in-Differences Estimators for Treatments Continuously Distributed at Every Period". SSRN

  • Calonico, S., Cattaneo, M. D., & Farrell, M. H. (2019). "nprobust: Nonparametric Kernel-Based Estimation and Robust Bias-Corrected Inference". Journal of Statistical Software.

License

MIT License. See LICENSE for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

did_had-0.2.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

did_had-0.2.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file did_had-0.2.0.tar.gz.

File metadata

  • Download URL: did_had-0.2.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for did_had-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94df38d661d8b582cf0e412201feefd814f54e84443998cacf0f252d716a2a64
MD5 9a6d8930029218746c89ef211afe62b5
BLAKE2b-256 58073d7f3b6fdd78c96276742eeaf8a0b68a585805a29cc0316fee2c2402d31e

See more details on using hashes here.

File details

Details for the file did_had-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: did_had-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for did_had-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d95734515b528dd28d378e71b754bd92ff38d7956e107fda131d77d90b899bb
MD5 33df2a6ac53ff64e015d8f4b177c542e
BLAKE2b-256 211f177c3587127db2520555509f38b78e305e3cc633447370b05e5d96cf5b0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page