Robust Inference in Difference-in-Differences and Event Study Designs
Project description
HonestDiD (Python)
Robust inference for Difference-in-Differences and event study designs.
This is a Python implementation of the methods proposed in Rambachan and Roth (2023), "A More Credible Approach to Parallel Trends."
Overview
The honestdid package implements methods for constructing robust confidence intervals in difference-in-differences and event study designs. These methods allow researchers to relax the parallel trends assumption and perform sensitivity analysis to evaluate how violations of parallel trends affect their conclusions.
The key idea is that pre-trends are informative about violations of parallel trends. The package formalizes this intuition through two main approaches:
Smoothness Restrictions (Delta^{SD})
Restrictions that bound how the slope of the underlying trend can change between consecutive periods. Setting $M = 0$ implies the counterfactual trend is exactly linear, while larger values of $M$ allow for more non-linearity.
Relative Magnitudes Restrictions (Delta^{RM})
Restrictions that bound the post-treatment violation of parallel trends relative to the maximum pre-treatment violation. Setting $\bar{M} = 1$ means post-treatment violations can be no larger than the maximum pre-treatment violation.
Installation
Install from GitHub (recommended)
pip install git+https://github.com/anzonyquispe/honestdid.git
Install from source
git clone https://github.com/anzonyquispe/honestdid.git
cd honestdid
pip install -e .
Dependencies
- Python >= 3.8
- PyTorch >= 1.9.0
- NumPy >= 1.19.0
- SciPy >= 1.6.0
- CVXPY >= 1.1.0
- Pandas >= 1.2.0
Basic Usage
import torch
import honestdid as hd
# Load your event study estimates (betahat) and variance-covariance matrix (sigma)
# betahat: coefficient estimates for pre and post periods (excluding reference period)
# sigma: variance-covariance matrix
# Example: 4 pre-treatment periods + 4 post-treatment periods
numPrePeriods = 4
numPostPeriods = 4
# Target parameter: effect in first post-treatment period
l_vec = hd.basis_vector(index=1, size=numPostPeriods)
# Original confidence set (assuming parallel trends holds exactly)
original_cs = hd.constructOriginalCS(
betahat=betahat,
sigma=sigma,
numPrePeriods=numPrePeriods,
numPostPeriods=numPostPeriods,
l_vec=l_vec,
alpha=0.05
)
# Sensitivity analysis using smoothness restrictions (DeltaSD)
sensitivity_results = hd.createSensitivityResults(
betahat=betahat,
sigma=sigma,
numPrePeriods=numPrePeriods,
numPostPeriods=numPostPeriods,
l_vec=l_vec,
Mvec=[0, 0.01, 0.02, 0.03], # different values of M
alpha=0.05
)
print(sensitivity_results)
# Sensitivity analysis using relative magnitudes (DeltaRM)
sensitivity_rm = hd.createSensitivityResults_relativeMagnitudes(
betahat=betahat,
sigma=sigma,
numPrePeriods=numPrePeriods,
numPostPeriods=numPostPeriods,
l_vec=l_vec,
Mbarvec=[0.5, 1.0, 1.5, 2.0], # different values of Mbar
alpha=0.05
)
print(sensitivity_rm)
Main Functions
| Function | Description |
|---|---|
constructOriginalCS |
Constructs the original confidence set assuming parallel trends holds |
find_optimal_flci |
Finds the optimal fixed-length confidence interval |
createSensitivityResults |
Sensitivity analysis under smoothness restrictions (DeltaSD) |
createSensitivityResults_relativeMagnitudes |
Sensitivity analysis under relative magnitudes (DeltaRM) |
computeConditionalCS_DeltaSD |
Conditional confidence set for DeltaSD |
computeConditionalCS_DeltaRM |
Conditional confidence set for DeltaRM |
DeltaSD_upperBound_Mpre |
Upper bound for M from pre-treatment periods |
DeltaSD_lowerBound_Mpre |
Lower bound for M from pre-treatment periods |
Advanced Usage
Fixed-Length Confidence Intervals (FLCI)
flci = hd.find_optimal_flci(
betahat=betahat,
sigma=sigma,
numPrePeriods=numPrePeriods,
numPostPeriods=numPostPeriods,
l_vec=l_vec,
M=0, # smoothness parameter
alpha=0.05
)
print(f"FLCI: [{flci['FLCI'][0]:.4f}, {flci['FLCI'][1]:.4f}]")
Breakdown Values
Calculate the value of M at which the confidence set includes zero:
# Get upper bound for M from pre-treatment data
M_upper = hd.DeltaSD_upperBound_Mpre(
betahat=betahat,
sigma=sigma,
numPrePeriods=numPrePeriods,
alpha=0.05
)
print(f"Upper bound for M: {M_upper:.4f}")
Combined Restrictions
The package also supports combining multiple restrictions:
computeConditionalCS_DeltaSDB: Smoothness + sign restrictionscomputeConditionalCS_DeltaSDM: Smoothness + monotonicitycomputeConditionalCS_DeltaRMB: Relative magnitudes + sign restrictionscomputeConditionalCS_DeltaRMM: Relative magnitudes + monotonicitycomputeConditionalCS_DeltaSDRM: Smoothness + relative magnitudes
Related Implementations
Citation
If you use this package, please cite:
@article{rambachan2023more,
title={A More Credible Approach to Parallel Trends},
author={Rambachan, Ashesh and Roth, Jonathan},
journal={Review of Economic Studies},
volume={90},
number={5},
pages={2555--2591},
year={2023},
publisher={Oxford University Press}
}
References
- Rambachan, Ashesh and Jonathan Roth. "A More Credible Approach to Parallel Trends." Review of Economic Studies 90.5 (2023): 2555-2591. [Paper]
- Video presentation by the authors
- Interactive Shiny application
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file honestdid-0.1.1.tar.gz.
File metadata
- Download URL: honestdid-0.1.1.tar.gz
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
029db67477a375d490fc59ca9ef08ec763a6d291b9ec603c0f5d81cf2928969a
|
|
| MD5 |
89270a9062d0536c45b63ce1a841441f
|
|
| BLAKE2b-256 |
5ac035ecbc1a1a0be91c5f6719919a9d4ba7ae2bec52ad3b0e5140cee5a69c33
|
Provenance
The following attestation bundles were made for honestdid-0.1.1.tar.gz:
Publisher:
publish.yml on anzonyquispe/honestdid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
honestdid-0.1.1.tar.gz -
Subject digest:
029db67477a375d490fc59ca9ef08ec763a6d291b9ec603c0f5d81cf2928969a - Sigstore transparency entry: 1018138806
- Sigstore integration time:
-
Permalink:
anzonyquispe/honestdid@cac131019fd3247e286fdbdce078cd7c43ae7ea2 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/anzonyquispe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cac131019fd3247e286fdbdce078cd7c43ae7ea2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file honestdid-0.1.1-py3-none-any.whl.
File metadata
- Download URL: honestdid-0.1.1-py3-none-any.whl
- Upload date:
- Size: 24.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66828bc8b4856033fbc1c6aea575ed64f0382425f5f3ee2f8e5c93a5f62063d9
|
|
| MD5 |
a8cd8e373620a4bf4bdeb8c5535d1183
|
|
| BLAKE2b-256 |
e6aef3e759c398d7efdc7ac1f13ce997102fb16fc47ec07ad72b2d5eb3abc0b1
|
Provenance
The following attestation bundles were made for honestdid-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on anzonyquispe/honestdid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
honestdid-0.1.1-py3-none-any.whl -
Subject digest:
66828bc8b4856033fbc1c6aea575ed64f0382425f5f3ee2f8e5c93a5f62063d9 - Sigstore transparency entry: 1018138836
- Sigstore integration time:
-
Permalink:
anzonyquispe/honestdid@cac131019fd3247e286fdbdce078cd7c43ae7ea2 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/anzonyquispe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cac131019fd3247e286fdbdce078cd7c43ae7ea2 -
Trigger Event:
release
-
Statement type: