diff-diff 2.7.3__cp39-cp39-manylinux_2_28_aarch64.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.
Files changed (49) hide show
  1. diff_diff/__init__.py +320 -0
  2. diff_diff/_backend.py +133 -0
  3. diff_diff/_rust_backend.cpython-39-aarch64-linux-gnu.so +0 -0
  4. diff_diff/bacon.py +979 -0
  5. diff_diff/bootstrap_utils.py +403 -0
  6. diff_diff/continuous_did.py +1155 -0
  7. diff_diff/continuous_did_bspline.py +188 -0
  8. diff_diff/continuous_did_results.py +353 -0
  9. diff_diff/datasets.py +708 -0
  10. diff_diff/diagnostics.py +921 -0
  11. diff_diff/efficient_did.py +843 -0
  12. diff_diff/efficient_did_bootstrap.py +315 -0
  13. diff_diff/efficient_did_results.py +289 -0
  14. diff_diff/efficient_did_weights.py +538 -0
  15. diff_diff/estimators.py +1147 -0
  16. diff_diff/honest_did.py +1511 -0
  17. diff_diff/imputation.py +1756 -0
  18. diff_diff/imputation_bootstrap.py +333 -0
  19. diff_diff/imputation_results.py +426 -0
  20. diff_diff/linalg.py +1727 -0
  21. diff_diff/power.py +1350 -0
  22. diff_diff/prep.py +1242 -0
  23. diff_diff/prep_dgp.py +932 -0
  24. diff_diff/pretrends.py +1104 -0
  25. diff_diff/results.py +794 -0
  26. diff_diff/stacked_did.py +871 -0
  27. diff_diff/stacked_did_results.py +318 -0
  28. diff_diff/staggered.py +1785 -0
  29. diff_diff/staggered_aggregation.py +592 -0
  30. diff_diff/staggered_bootstrap.py +628 -0
  31. diff_diff/staggered_results.py +328 -0
  32. diff_diff/sun_abraham.py +1162 -0
  33. diff_diff/synthetic_did.py +847 -0
  34. diff_diff/triple_diff.py +1581 -0
  35. diff_diff/trop.py +2738 -0
  36. diff_diff/trop_results.py +322 -0
  37. diff_diff/twfe.py +428 -0
  38. diff_diff/two_stage.py +1451 -0
  39. diff_diff/two_stage_bootstrap.py +459 -0
  40. diff_diff/two_stage_results.py +379 -0
  41. diff_diff/utils.py +1838 -0
  42. diff_diff/visualization.py +1727 -0
  43. diff_diff-2.7.3.dist-info/METADATA +2981 -0
  44. diff_diff-2.7.3.dist-info/RECORD +49 -0
  45. diff_diff-2.7.3.dist-info/WHEEL +4 -0
  46. diff_diff-2.7.3.dist-info/sboms/auditwheel.cdx.json +58 -0
  47. diff_diff-2.7.3.dist-info/sboms/diff_diff_rust.cyclonedx.json +5995 -0
  48. diff_diff.libs/libgfortran-e1b7dfc8.so.5 +0 -0
  49. diff_diff.libs/libopenblas-e9c9f581.so.0 +0 -0
diff_diff/__init__.py ADDED
@@ -0,0 +1,320 @@
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_continuous_did_data,
74
+ generate_did_data,
75
+ generate_ddd_data,
76
+ generate_event_study_data,
77
+ generate_factor_data,
78
+ generate_panel_data,
79
+ generate_staggered_data,
80
+ make_post_indicator,
81
+ make_treatment_indicator,
82
+ rank_control_units,
83
+ summarize_did_data,
84
+ validate_did_data,
85
+ wide_to_long,
86
+ )
87
+ from diff_diff.results import (
88
+ DiDResults,
89
+ MultiPeriodDiDResults,
90
+ PeriodEffect,
91
+ SyntheticDiDResults,
92
+ )
93
+ from diff_diff.staggered import (
94
+ CallawaySantAnna,
95
+ CallawaySantAnnaResults,
96
+ CSBootstrapResults,
97
+ GroupTimeEffect,
98
+ )
99
+ from diff_diff.imputation import (
100
+ ImputationBootstrapResults,
101
+ ImputationDiD,
102
+ ImputationDiDResults,
103
+ imputation_did,
104
+ )
105
+ from diff_diff.two_stage import (
106
+ TwoStageBootstrapResults,
107
+ TwoStageDiD,
108
+ TwoStageDiDResults,
109
+ two_stage_did,
110
+ )
111
+ from diff_diff.stacked_did import (
112
+ StackedDiD,
113
+ StackedDiDResults,
114
+ stacked_did,
115
+ )
116
+ from diff_diff.sun_abraham import (
117
+ SABootstrapResults,
118
+ SunAbraham,
119
+ SunAbrahamResults,
120
+ )
121
+ from diff_diff.triple_diff import (
122
+ TripleDifference,
123
+ TripleDifferenceResults,
124
+ triple_difference,
125
+ )
126
+ from diff_diff.continuous_did import (
127
+ ContinuousDiD,
128
+ ContinuousDiDResults,
129
+ DoseResponseCurve,
130
+ )
131
+ from diff_diff.efficient_did import (
132
+ EfficientDiD,
133
+ EfficientDiDResults,
134
+ EDiDBootstrapResults,
135
+ )
136
+ from diff_diff.trop import (
137
+ TROP,
138
+ TROPResults,
139
+ trop,
140
+ )
141
+ from diff_diff.utils import (
142
+ WildBootstrapResults,
143
+ check_parallel_trends,
144
+ check_parallel_trends_robust,
145
+ equivalence_test_trends,
146
+ wild_bootstrap_se,
147
+ )
148
+ from diff_diff.visualization import (
149
+ plot_bacon,
150
+ plot_event_study,
151
+ plot_group_effects,
152
+ plot_honest_event_study,
153
+ plot_power_curve,
154
+ plot_pretrends_power,
155
+ plot_sensitivity,
156
+ )
157
+ from diff_diff.datasets import (
158
+ clear_cache,
159
+ list_datasets,
160
+ load_card_krueger,
161
+ load_castle_doctrine,
162
+ load_dataset,
163
+ load_divorce_laws,
164
+ load_mpdta,
165
+ )
166
+
167
+ # Estimator aliases — short names for convenience
168
+ DiD = DifferenceInDifferences
169
+ TWFE = TwoWayFixedEffects
170
+ EventStudy = MultiPeriodDiD
171
+ SDiD = SyntheticDiD
172
+ CS = CallawaySantAnna
173
+ CDiD = ContinuousDiD
174
+ SA = SunAbraham
175
+ BJS = ImputationDiD
176
+ Gardner = TwoStageDiD
177
+ DDD = TripleDifference
178
+ Stacked = StackedDiD
179
+ Bacon = BaconDecomposition
180
+ EDiD = EfficientDiD
181
+
182
+ __version__ = "2.7.3"
183
+ __all__ = [
184
+ # Estimators
185
+ "DifferenceInDifferences",
186
+ "TwoWayFixedEffects",
187
+ "MultiPeriodDiD",
188
+ "SyntheticDiD",
189
+ "CallawaySantAnna",
190
+ "ContinuousDiD",
191
+ "SunAbraham",
192
+ "ImputationDiD",
193
+ "TwoStageDiD",
194
+ "TripleDifference",
195
+ "TROP",
196
+ "StackedDiD",
197
+ # Estimator aliases (short names)
198
+ "DiD",
199
+ "TWFE",
200
+ "EventStudy",
201
+ "SDiD",
202
+ "CS",
203
+ "CDiD",
204
+ "SA",
205
+ "BJS",
206
+ "Gardner",
207
+ "DDD",
208
+ "Stacked",
209
+ "Bacon",
210
+ # Bacon Decomposition
211
+ "BaconDecomposition",
212
+ "BaconDecompositionResults",
213
+ "Comparison2x2",
214
+ "bacon_decompose",
215
+ "plot_bacon",
216
+ # Results
217
+ "DiDResults",
218
+ "MultiPeriodDiDResults",
219
+ "SyntheticDiDResults",
220
+ "PeriodEffect",
221
+ "CallawaySantAnnaResults",
222
+ "CSBootstrapResults",
223
+ "GroupTimeEffect",
224
+ "ContinuousDiDResults",
225
+ "DoseResponseCurve",
226
+ "SunAbrahamResults",
227
+ "SABootstrapResults",
228
+ "ImputationDiDResults",
229
+ "ImputationBootstrapResults",
230
+ "imputation_did",
231
+ "TwoStageDiDResults",
232
+ "TwoStageBootstrapResults",
233
+ "two_stage_did",
234
+ "TripleDifferenceResults",
235
+ "triple_difference",
236
+ "TROPResults",
237
+ "trop",
238
+ "StackedDiDResults",
239
+ "stacked_did",
240
+ # EfficientDiD
241
+ "EfficientDiD",
242
+ "EfficientDiDResults",
243
+ "EDiDBootstrapResults",
244
+ "EDiD",
245
+ # Visualization
246
+ "plot_event_study",
247
+ "plot_group_effects",
248
+ "plot_sensitivity",
249
+ "plot_honest_event_study",
250
+ # Parallel trends testing
251
+ "check_parallel_trends",
252
+ "check_parallel_trends_robust",
253
+ "equivalence_test_trends",
254
+ # Wild cluster bootstrap
255
+ "WildBootstrapResults",
256
+ "wild_bootstrap_se",
257
+ # Placebo tests / diagnostics
258
+ "PlaceboTestResults",
259
+ "run_placebo_test",
260
+ "placebo_timing_test",
261
+ "placebo_group_test",
262
+ "permutation_test",
263
+ "leave_one_out_test",
264
+ "run_all_placebo_tests",
265
+ # Data preparation utilities
266
+ "make_treatment_indicator",
267
+ "make_post_indicator",
268
+ "wide_to_long",
269
+ "balance_panel",
270
+ "validate_did_data",
271
+ "summarize_did_data",
272
+ "generate_did_data",
273
+ "generate_staggered_data",
274
+ "generate_factor_data",
275
+ "generate_ddd_data",
276
+ "generate_panel_data",
277
+ "generate_event_study_data",
278
+ "generate_continuous_did_data",
279
+ "create_event_time",
280
+ "aggregate_to_cohorts",
281
+ "rank_control_units",
282
+ # Honest DiD sensitivity analysis
283
+ "HonestDiD",
284
+ "HonestDiDResults",
285
+ "SensitivityResults",
286
+ "DeltaSD",
287
+ "DeltaRM",
288
+ "DeltaSDRM",
289
+ "compute_honest_did",
290
+ "sensitivity_plot",
291
+ # Power analysis
292
+ "PowerAnalysis",
293
+ "PowerResults",
294
+ "SimulationPowerResults",
295
+ "compute_mde",
296
+ "compute_power",
297
+ "compute_sample_size",
298
+ "simulate_power",
299
+ "plot_power_curve",
300
+ # Pre-trends power analysis
301
+ "PreTrendsPower",
302
+ "PreTrendsPowerResults",
303
+ "PreTrendsPowerCurve",
304
+ "compute_pretrends_power",
305
+ "compute_mdv",
306
+ "plot_pretrends_power",
307
+ # Rust backend
308
+ "HAS_RUST_BACKEND",
309
+ # Linear algebra helpers
310
+ "LinearRegression",
311
+ "InferenceResult",
312
+ # Datasets
313
+ "load_card_krueger",
314
+ "load_castle_doctrine",
315
+ "load_divorce_laws",
316
+ "load_mpdta",
317
+ "load_dataset",
318
+ "list_datasets",
319
+ "clear_cache",
320
+ ]
diff_diff/_backend.py ADDED
@@ -0,0 +1,133 @@
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
+ # TROP estimator acceleration (local method)
27
+ compute_unit_distance_matrix as _rust_unit_distance_matrix,
28
+ loocv_grid_search as _rust_loocv_grid_search,
29
+ bootstrap_trop_variance as _rust_bootstrap_trop_variance,
30
+ # TROP estimator acceleration (global method)
31
+ loocv_grid_search_global as _rust_loocv_grid_search_global,
32
+ bootstrap_trop_variance_global as _rust_bootstrap_trop_variance_global,
33
+ # SDID weights (Frank-Wolfe matching R's synthdid)
34
+ compute_sdid_unit_weights as _rust_sdid_unit_weights,
35
+ compute_time_weights as _rust_compute_time_weights,
36
+ compute_noise_level as _rust_compute_noise_level,
37
+ sc_weight_fw as _rust_sc_weight_fw,
38
+ # Diagnostics
39
+ rust_backend_info as _rust_backend_info,
40
+ )
41
+ _rust_available = True
42
+ except ImportError:
43
+ _rust_available = False
44
+ _rust_bootstrap_weights = None
45
+ _rust_synthetic_weights = None
46
+ _rust_project_simplex = None
47
+ _rust_solve_ols = None
48
+ _rust_compute_robust_vcov = None
49
+ # TROP estimator acceleration (local method)
50
+ _rust_unit_distance_matrix = None
51
+ _rust_loocv_grid_search = None
52
+ _rust_bootstrap_trop_variance = None
53
+ # TROP estimator acceleration (global method)
54
+ _rust_loocv_grid_search_global = None
55
+ _rust_bootstrap_trop_variance_global = None
56
+ # SDID weights (Frank-Wolfe matching R's synthdid)
57
+ _rust_sdid_unit_weights = None
58
+ _rust_compute_time_weights = None
59
+ _rust_compute_noise_level = None
60
+ _rust_sc_weight_fw = None
61
+ _rust_backend_info = None
62
+
63
+ # Determine final backend based on environment variable and availability
64
+ if _backend_env == 'python':
65
+ # Force pure Python mode - disable Rust even if available
66
+ HAS_RUST_BACKEND = False
67
+ _rust_bootstrap_weights = None
68
+ _rust_synthetic_weights = None
69
+ _rust_project_simplex = None
70
+ _rust_solve_ols = None
71
+ _rust_compute_robust_vcov = None
72
+ # TROP estimator acceleration (local method)
73
+ _rust_unit_distance_matrix = None
74
+ _rust_loocv_grid_search = None
75
+ _rust_bootstrap_trop_variance = None
76
+ # TROP estimator acceleration (global method)
77
+ _rust_loocv_grid_search_global = None
78
+ _rust_bootstrap_trop_variance_global = None
79
+ # SDID weights (Frank-Wolfe matching R's synthdid)
80
+ _rust_sdid_unit_weights = None
81
+ _rust_compute_time_weights = None
82
+ _rust_compute_noise_level = None
83
+ _rust_sc_weight_fw = None
84
+ _rust_backend_info = None
85
+ elif _backend_env == 'rust':
86
+ # Force Rust mode - fail if not available
87
+ if not _rust_available:
88
+ raise ImportError(
89
+ "DIFF_DIFF_BACKEND=rust but Rust backend is not available. "
90
+ "Install with: pip install diff-diff[rust]"
91
+ )
92
+ HAS_RUST_BACKEND = True
93
+ else:
94
+ # Auto mode - use Rust if available
95
+ HAS_RUST_BACKEND = _rust_available
96
+
97
+
98
+ def rust_backend_info():
99
+ """Return compile-time BLAS feature information for the Rust backend.
100
+
101
+ Returns a dict with keys:
102
+ - 'blas': True if any BLAS backend is linked
103
+ - 'accelerate': True if Apple Accelerate is linked (macOS)
104
+ - 'openblas': True if OpenBLAS is linked (Linux)
105
+
106
+ If the Rust backend is not available, all values are False.
107
+ """
108
+ if _rust_backend_info is not None:
109
+ return _rust_backend_info()
110
+ return {"blas": False, "accelerate": False, "openblas": False}
111
+
112
+
113
+ __all__ = [
114
+ 'HAS_RUST_BACKEND',
115
+ 'rust_backend_info',
116
+ '_rust_bootstrap_weights',
117
+ '_rust_synthetic_weights',
118
+ '_rust_project_simplex',
119
+ '_rust_solve_ols',
120
+ '_rust_compute_robust_vcov',
121
+ # TROP estimator acceleration (local method)
122
+ '_rust_unit_distance_matrix',
123
+ '_rust_loocv_grid_search',
124
+ '_rust_bootstrap_trop_variance',
125
+ # TROP estimator acceleration (global method)
126
+ '_rust_loocv_grid_search_global',
127
+ '_rust_bootstrap_trop_variance_global',
128
+ # SDID weights (Frank-Wolfe matching R's synthdid)
129
+ '_rust_sdid_unit_weights',
130
+ '_rust_compute_time_weights',
131
+ '_rust_compute_noise_level',
132
+ '_rust_sc_weight_fw',
133
+ ]