diff-diff 2.1.0__cp39-cp39-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diff_diff/__init__.py +234 -0
- diff_diff/_backend.py +64 -0
- diff_diff/_rust_backend.cpython-39-darwin.so +0 -0
- diff_diff/bacon.py +979 -0
- diff_diff/datasets.py +708 -0
- diff_diff/diagnostics.py +927 -0
- diff_diff/estimators.py +1000 -0
- diff_diff/honest_did.py +1493 -0
- diff_diff/linalg.py +980 -0
- diff_diff/power.py +1350 -0
- diff_diff/prep.py +1338 -0
- diff_diff/pretrends.py +1067 -0
- diff_diff/results.py +703 -0
- diff_diff/staggered.py +2297 -0
- diff_diff/sun_abraham.py +1176 -0
- diff_diff/synthetic_did.py +738 -0
- diff_diff/triple_diff.py +1291 -0
- diff_diff/trop.py +1348 -0
- diff_diff/twfe.py +344 -0
- diff_diff/utils.py +1481 -0
- diff_diff/visualization.py +1627 -0
- diff_diff-2.1.0.dist-info/METADATA +2511 -0
- diff_diff-2.1.0.dist-info/RECORD +24 -0
- diff_diff-2.1.0.dist-info/WHEEL +4 -0
diff_diff/__init__.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""
|
|
2
|
+
diff-diff: A library for Difference-in-Differences analysis.
|
|
3
|
+
|
|
4
|
+
This library provides sklearn-like estimators for causal inference
|
|
5
|
+
using the difference-in-differences methodology.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# Import backend detection from dedicated module (avoids circular imports)
|
|
9
|
+
from diff_diff._backend import (
|
|
10
|
+
HAS_RUST_BACKEND,
|
|
11
|
+
_rust_bootstrap_weights,
|
|
12
|
+
_rust_compute_robust_vcov,
|
|
13
|
+
_rust_project_simplex,
|
|
14
|
+
_rust_solve_ols,
|
|
15
|
+
_rust_synthetic_weights,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from diff_diff.bacon import (
|
|
19
|
+
BaconDecomposition,
|
|
20
|
+
BaconDecompositionResults,
|
|
21
|
+
Comparison2x2,
|
|
22
|
+
bacon_decompose,
|
|
23
|
+
)
|
|
24
|
+
from diff_diff.diagnostics import (
|
|
25
|
+
PlaceboTestResults,
|
|
26
|
+
leave_one_out_test,
|
|
27
|
+
permutation_test,
|
|
28
|
+
placebo_group_test,
|
|
29
|
+
placebo_timing_test,
|
|
30
|
+
run_all_placebo_tests,
|
|
31
|
+
run_placebo_test,
|
|
32
|
+
)
|
|
33
|
+
from diff_diff.linalg import (
|
|
34
|
+
InferenceResult,
|
|
35
|
+
LinearRegression,
|
|
36
|
+
)
|
|
37
|
+
from diff_diff.estimators import (
|
|
38
|
+
DifferenceInDifferences,
|
|
39
|
+
MultiPeriodDiD,
|
|
40
|
+
SyntheticDiD,
|
|
41
|
+
TwoWayFixedEffects,
|
|
42
|
+
)
|
|
43
|
+
from diff_diff.honest_did import (
|
|
44
|
+
DeltaRM,
|
|
45
|
+
DeltaSD,
|
|
46
|
+
DeltaSDRM,
|
|
47
|
+
HonestDiD,
|
|
48
|
+
HonestDiDResults,
|
|
49
|
+
SensitivityResults,
|
|
50
|
+
compute_honest_did,
|
|
51
|
+
sensitivity_plot,
|
|
52
|
+
)
|
|
53
|
+
from diff_diff.power import (
|
|
54
|
+
PowerAnalysis,
|
|
55
|
+
PowerResults,
|
|
56
|
+
SimulationPowerResults,
|
|
57
|
+
compute_mde,
|
|
58
|
+
compute_power,
|
|
59
|
+
compute_sample_size,
|
|
60
|
+
simulate_power,
|
|
61
|
+
)
|
|
62
|
+
from diff_diff.pretrends import (
|
|
63
|
+
PreTrendsPower,
|
|
64
|
+
PreTrendsPowerCurve,
|
|
65
|
+
PreTrendsPowerResults,
|
|
66
|
+
compute_mdv,
|
|
67
|
+
compute_pretrends_power,
|
|
68
|
+
)
|
|
69
|
+
from diff_diff.prep import (
|
|
70
|
+
aggregate_to_cohorts,
|
|
71
|
+
balance_panel,
|
|
72
|
+
create_event_time,
|
|
73
|
+
generate_did_data,
|
|
74
|
+
make_post_indicator,
|
|
75
|
+
make_treatment_indicator,
|
|
76
|
+
rank_control_units,
|
|
77
|
+
summarize_did_data,
|
|
78
|
+
validate_did_data,
|
|
79
|
+
wide_to_long,
|
|
80
|
+
)
|
|
81
|
+
from diff_diff.results import (
|
|
82
|
+
DiDResults,
|
|
83
|
+
MultiPeriodDiDResults,
|
|
84
|
+
PeriodEffect,
|
|
85
|
+
SyntheticDiDResults,
|
|
86
|
+
)
|
|
87
|
+
from diff_diff.staggered import (
|
|
88
|
+
CallawaySantAnna,
|
|
89
|
+
CallawaySantAnnaResults,
|
|
90
|
+
CSBootstrapResults,
|
|
91
|
+
GroupTimeEffect,
|
|
92
|
+
)
|
|
93
|
+
from diff_diff.sun_abraham import (
|
|
94
|
+
SABootstrapResults,
|
|
95
|
+
SunAbraham,
|
|
96
|
+
SunAbrahamResults,
|
|
97
|
+
)
|
|
98
|
+
from diff_diff.triple_diff import (
|
|
99
|
+
TripleDifference,
|
|
100
|
+
TripleDifferenceResults,
|
|
101
|
+
triple_difference,
|
|
102
|
+
)
|
|
103
|
+
from diff_diff.trop import (
|
|
104
|
+
TROP,
|
|
105
|
+
TROPResults,
|
|
106
|
+
trop,
|
|
107
|
+
)
|
|
108
|
+
from diff_diff.utils import (
|
|
109
|
+
WildBootstrapResults,
|
|
110
|
+
check_parallel_trends,
|
|
111
|
+
check_parallel_trends_robust,
|
|
112
|
+
equivalence_test_trends,
|
|
113
|
+
wild_bootstrap_se,
|
|
114
|
+
)
|
|
115
|
+
from diff_diff.visualization import (
|
|
116
|
+
plot_bacon,
|
|
117
|
+
plot_event_study,
|
|
118
|
+
plot_group_effects,
|
|
119
|
+
plot_honest_event_study,
|
|
120
|
+
plot_power_curve,
|
|
121
|
+
plot_pretrends_power,
|
|
122
|
+
plot_sensitivity,
|
|
123
|
+
)
|
|
124
|
+
from diff_diff.datasets import (
|
|
125
|
+
clear_cache,
|
|
126
|
+
list_datasets,
|
|
127
|
+
load_card_krueger,
|
|
128
|
+
load_castle_doctrine,
|
|
129
|
+
load_dataset,
|
|
130
|
+
load_divorce_laws,
|
|
131
|
+
load_mpdta,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
__version__ = "2.1.0"
|
|
135
|
+
__all__ = [
|
|
136
|
+
# Estimators
|
|
137
|
+
"DifferenceInDifferences",
|
|
138
|
+
"TwoWayFixedEffects",
|
|
139
|
+
"MultiPeriodDiD",
|
|
140
|
+
"SyntheticDiD",
|
|
141
|
+
"CallawaySantAnna",
|
|
142
|
+
"SunAbraham",
|
|
143
|
+
"TripleDifference",
|
|
144
|
+
"TROP",
|
|
145
|
+
# Bacon Decomposition
|
|
146
|
+
"BaconDecomposition",
|
|
147
|
+
"BaconDecompositionResults",
|
|
148
|
+
"Comparison2x2",
|
|
149
|
+
"bacon_decompose",
|
|
150
|
+
"plot_bacon",
|
|
151
|
+
# Results
|
|
152
|
+
"DiDResults",
|
|
153
|
+
"MultiPeriodDiDResults",
|
|
154
|
+
"SyntheticDiDResults",
|
|
155
|
+
"PeriodEffect",
|
|
156
|
+
"CallawaySantAnnaResults",
|
|
157
|
+
"CSBootstrapResults",
|
|
158
|
+
"GroupTimeEffect",
|
|
159
|
+
"SunAbrahamResults",
|
|
160
|
+
"SABootstrapResults",
|
|
161
|
+
"TripleDifferenceResults",
|
|
162
|
+
"triple_difference",
|
|
163
|
+
"TROPResults",
|
|
164
|
+
"trop",
|
|
165
|
+
# Visualization
|
|
166
|
+
"plot_event_study",
|
|
167
|
+
"plot_group_effects",
|
|
168
|
+
"plot_sensitivity",
|
|
169
|
+
"plot_honest_event_study",
|
|
170
|
+
# Parallel trends testing
|
|
171
|
+
"check_parallel_trends",
|
|
172
|
+
"check_parallel_trends_robust",
|
|
173
|
+
"equivalence_test_trends",
|
|
174
|
+
# Wild cluster bootstrap
|
|
175
|
+
"WildBootstrapResults",
|
|
176
|
+
"wild_bootstrap_se",
|
|
177
|
+
# Placebo tests / diagnostics
|
|
178
|
+
"PlaceboTestResults",
|
|
179
|
+
"run_placebo_test",
|
|
180
|
+
"placebo_timing_test",
|
|
181
|
+
"placebo_group_test",
|
|
182
|
+
"permutation_test",
|
|
183
|
+
"leave_one_out_test",
|
|
184
|
+
"run_all_placebo_tests",
|
|
185
|
+
# Data preparation utilities
|
|
186
|
+
"make_treatment_indicator",
|
|
187
|
+
"make_post_indicator",
|
|
188
|
+
"wide_to_long",
|
|
189
|
+
"balance_panel",
|
|
190
|
+
"validate_did_data",
|
|
191
|
+
"summarize_did_data",
|
|
192
|
+
"generate_did_data",
|
|
193
|
+
"create_event_time",
|
|
194
|
+
"aggregate_to_cohorts",
|
|
195
|
+
"rank_control_units",
|
|
196
|
+
# Honest DiD sensitivity analysis
|
|
197
|
+
"HonestDiD",
|
|
198
|
+
"HonestDiDResults",
|
|
199
|
+
"SensitivityResults",
|
|
200
|
+
"DeltaSD",
|
|
201
|
+
"DeltaRM",
|
|
202
|
+
"DeltaSDRM",
|
|
203
|
+
"compute_honest_did",
|
|
204
|
+
"sensitivity_plot",
|
|
205
|
+
# Power analysis
|
|
206
|
+
"PowerAnalysis",
|
|
207
|
+
"PowerResults",
|
|
208
|
+
"SimulationPowerResults",
|
|
209
|
+
"compute_mde",
|
|
210
|
+
"compute_power",
|
|
211
|
+
"compute_sample_size",
|
|
212
|
+
"simulate_power",
|
|
213
|
+
"plot_power_curve",
|
|
214
|
+
# Pre-trends power analysis
|
|
215
|
+
"PreTrendsPower",
|
|
216
|
+
"PreTrendsPowerResults",
|
|
217
|
+
"PreTrendsPowerCurve",
|
|
218
|
+
"compute_pretrends_power",
|
|
219
|
+
"compute_mdv",
|
|
220
|
+
"plot_pretrends_power",
|
|
221
|
+
# Rust backend
|
|
222
|
+
"HAS_RUST_BACKEND",
|
|
223
|
+
# Linear algebra helpers
|
|
224
|
+
"LinearRegression",
|
|
225
|
+
"InferenceResult",
|
|
226
|
+
# Datasets
|
|
227
|
+
"load_card_krueger",
|
|
228
|
+
"load_castle_doctrine",
|
|
229
|
+
"load_divorce_laws",
|
|
230
|
+
"load_mpdta",
|
|
231
|
+
"load_dataset",
|
|
232
|
+
"list_datasets",
|
|
233
|
+
"clear_cache",
|
|
234
|
+
]
|
diff_diff/_backend.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Backend detection and configuration for diff-diff.
|
|
3
|
+
|
|
4
|
+
This module handles:
|
|
5
|
+
1. Detection of optional Rust backend
|
|
6
|
+
2. Environment variable configuration (DIFF_DIFF_BACKEND)
|
|
7
|
+
3. Exports HAS_RUST_BACKEND and Rust function references
|
|
8
|
+
|
|
9
|
+
Other modules should import from here to avoid circular imports with __init__.py.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
# Check for backend override via environment variable
|
|
15
|
+
# DIFF_DIFF_BACKEND can be: 'auto' (default), 'python', or 'rust'
|
|
16
|
+
_backend_env = os.environ.get('DIFF_DIFF_BACKEND', 'auto').lower()
|
|
17
|
+
|
|
18
|
+
# Try to import Rust backend for accelerated operations
|
|
19
|
+
try:
|
|
20
|
+
from diff_diff._rust_backend import (
|
|
21
|
+
generate_bootstrap_weights_batch as _rust_bootstrap_weights,
|
|
22
|
+
compute_synthetic_weights as _rust_synthetic_weights,
|
|
23
|
+
project_simplex as _rust_project_simplex,
|
|
24
|
+
solve_ols as _rust_solve_ols,
|
|
25
|
+
compute_robust_vcov as _rust_compute_robust_vcov,
|
|
26
|
+
)
|
|
27
|
+
_rust_available = True
|
|
28
|
+
except ImportError:
|
|
29
|
+
_rust_available = False
|
|
30
|
+
_rust_bootstrap_weights = None
|
|
31
|
+
_rust_synthetic_weights = None
|
|
32
|
+
_rust_project_simplex = None
|
|
33
|
+
_rust_solve_ols = None
|
|
34
|
+
_rust_compute_robust_vcov = None
|
|
35
|
+
|
|
36
|
+
# Determine final backend based on environment variable and availability
|
|
37
|
+
if _backend_env == 'python':
|
|
38
|
+
# Force pure Python mode - disable Rust even if available
|
|
39
|
+
HAS_RUST_BACKEND = False
|
|
40
|
+
_rust_bootstrap_weights = None
|
|
41
|
+
_rust_synthetic_weights = None
|
|
42
|
+
_rust_project_simplex = None
|
|
43
|
+
_rust_solve_ols = None
|
|
44
|
+
_rust_compute_robust_vcov = None
|
|
45
|
+
elif _backend_env == 'rust':
|
|
46
|
+
# Force Rust mode - fail if not available
|
|
47
|
+
if not _rust_available:
|
|
48
|
+
raise ImportError(
|
|
49
|
+
"DIFF_DIFF_BACKEND=rust but Rust backend is not available. "
|
|
50
|
+
"Install with: pip install diff-diff[rust]"
|
|
51
|
+
)
|
|
52
|
+
HAS_RUST_BACKEND = True
|
|
53
|
+
else:
|
|
54
|
+
# Auto mode - use Rust if available
|
|
55
|
+
HAS_RUST_BACKEND = _rust_available
|
|
56
|
+
|
|
57
|
+
__all__ = [
|
|
58
|
+
'HAS_RUST_BACKEND',
|
|
59
|
+
'_rust_bootstrap_weights',
|
|
60
|
+
'_rust_synthetic_weights',
|
|
61
|
+
'_rust_project_simplex',
|
|
62
|
+
'_rust_solve_ols',
|
|
63
|
+
'_rust_compute_robust_vcov',
|
|
64
|
+
]
|
|
Binary file
|