diff-diff 3.0.1__cp314-cp314-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.
Files changed (62) hide show
  1. diff_diff/__init__.py +382 -0
  2. diff_diff/_backend.py +134 -0
  3. diff_diff/_rust_backend.cpython-314-darwin.so +0 -0
  4. diff_diff/bacon.py +1140 -0
  5. diff_diff/bootstrap_utils.py +730 -0
  6. diff_diff/continuous_did.py +1626 -0
  7. diff_diff/continuous_did_bspline.py +190 -0
  8. diff_diff/continuous_did_results.py +374 -0
  9. diff_diff/datasets.py +815 -0
  10. diff_diff/diagnostics.py +882 -0
  11. diff_diff/efficient_did.py +1770 -0
  12. diff_diff/efficient_did_bootstrap.py +359 -0
  13. diff_diff/efficient_did_covariates.py +899 -0
  14. diff_diff/efficient_did_results.py +368 -0
  15. diff_diff/efficient_did_weights.py +617 -0
  16. diff_diff/estimators.py +1501 -0
  17. diff_diff/honest_did.py +2585 -0
  18. diff_diff/imputation.py +2458 -0
  19. diff_diff/imputation_bootstrap.py +418 -0
  20. diff_diff/imputation_results.py +448 -0
  21. diff_diff/linalg.py +2538 -0
  22. diff_diff/power.py +2588 -0
  23. diff_diff/practitioner.py +869 -0
  24. diff_diff/prep.py +1738 -0
  25. diff_diff/prep_dgp.py +1718 -0
  26. diff_diff/pretrends.py +1105 -0
  27. diff_diff/results.py +918 -0
  28. diff_diff/stacked_did.py +1049 -0
  29. diff_diff/stacked_did_results.py +339 -0
  30. diff_diff/staggered.py +3895 -0
  31. diff_diff/staggered_aggregation.py +864 -0
  32. diff_diff/staggered_bootstrap.py +752 -0
  33. diff_diff/staggered_results.py +416 -0
  34. diff_diff/staggered_triple_diff.py +1545 -0
  35. diff_diff/staggered_triple_diff_results.py +416 -0
  36. diff_diff/sun_abraham.py +1685 -0
  37. diff_diff/survey.py +1981 -0
  38. diff_diff/synthetic_did.py +1136 -0
  39. diff_diff/triple_diff.py +2047 -0
  40. diff_diff/trop.py +952 -0
  41. diff_diff/trop_global.py +1270 -0
  42. diff_diff/trop_local.py +1307 -0
  43. diff_diff/trop_results.py +356 -0
  44. diff_diff/twfe.py +542 -0
  45. diff_diff/two_stage.py +1952 -0
  46. diff_diff/two_stage_bootstrap.py +520 -0
  47. diff_diff/two_stage_results.py +400 -0
  48. diff_diff/utils.py +1902 -0
  49. diff_diff/visualization/__init__.py +61 -0
  50. diff_diff/visualization/_common.py +328 -0
  51. diff_diff/visualization/_continuous.py +274 -0
  52. diff_diff/visualization/_diagnostic.py +817 -0
  53. diff_diff/visualization/_event_study.py +1086 -0
  54. diff_diff/visualization/_power.py +661 -0
  55. diff_diff/visualization/_staggered.py +833 -0
  56. diff_diff/visualization/_synthetic.py +197 -0
  57. diff_diff/wooldridge.py +1285 -0
  58. diff_diff/wooldridge_results.py +349 -0
  59. diff_diff-3.0.1.dist-info/METADATA +2997 -0
  60. diff_diff-3.0.1.dist-info/RECORD +62 -0
  61. diff_diff-3.0.1.dist-info/WHEEL +4 -0
  62. diff_diff-3.0.1.dist-info/sboms/diff_diff_rust.cyclonedx.json +5971 -0
diff_diff/__init__.py ADDED
@@ -0,0 +1,382 @@
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
+ For rigorous analysis, follow the 8-step practitioner workflow in
8
+ docs/llms-practitioner.txt (based on Baker et al. 2025). After
9
+ estimation, call ``practitioner_next_steps(results)`` for context-aware
10
+ guidance on remaining diagnostic steps.
11
+
12
+ AI agent reference: docs/llms.txt
13
+ """
14
+
15
+ # Import backend detection from dedicated module (avoids circular imports)
16
+ from diff_diff._backend import (
17
+ HAS_RUST_BACKEND,
18
+ _rust_bootstrap_weights,
19
+ _rust_compute_robust_vcov,
20
+ _rust_project_simplex,
21
+ _rust_solve_ols,
22
+ _rust_synthetic_weights,
23
+ )
24
+
25
+ from diff_diff.bacon import (
26
+ BaconDecomposition,
27
+ BaconDecompositionResults,
28
+ Comparison2x2,
29
+ bacon_decompose,
30
+ )
31
+ from diff_diff.diagnostics import (
32
+ PlaceboTestResults,
33
+ leave_one_out_test,
34
+ permutation_test,
35
+ placebo_group_test,
36
+ placebo_timing_test,
37
+ run_all_placebo_tests,
38
+ run_placebo_test,
39
+ )
40
+ from diff_diff.linalg import (
41
+ InferenceResult,
42
+ LinearRegression,
43
+ )
44
+ from diff_diff.estimators import (
45
+ DifferenceInDifferences,
46
+ MultiPeriodDiD,
47
+ SyntheticDiD,
48
+ TwoWayFixedEffects,
49
+ )
50
+ from diff_diff.honest_did import (
51
+ DeltaRM,
52
+ DeltaSD,
53
+ DeltaSDRM,
54
+ HonestDiD,
55
+ HonestDiDResults,
56
+ SensitivityResults,
57
+ compute_honest_did,
58
+ sensitivity_plot,
59
+ )
60
+ from diff_diff.power import (
61
+ PowerAnalysis,
62
+ PowerResults,
63
+ SimulationMDEResults,
64
+ SimulationPowerResults,
65
+ SimulationSampleSizeResults,
66
+ compute_mde,
67
+ compute_power,
68
+ compute_sample_size,
69
+ simulate_mde,
70
+ simulate_power,
71
+ simulate_sample_size,
72
+ )
73
+ from diff_diff.pretrends import (
74
+ PreTrendsPower,
75
+ PreTrendsPowerCurve,
76
+ PreTrendsPowerResults,
77
+ compute_mdv,
78
+ compute_pretrends_power,
79
+ )
80
+ from diff_diff.prep import (
81
+ aggregate_survey,
82
+ aggregate_to_cohorts,
83
+ balance_panel,
84
+ create_event_time,
85
+ generate_continuous_did_data,
86
+ generate_did_data,
87
+ generate_ddd_data,
88
+ generate_event_study_data,
89
+ generate_factor_data,
90
+ generate_panel_data,
91
+ generate_staggered_data,
92
+ generate_staggered_ddd_data,
93
+ generate_survey_did_data,
94
+ make_post_indicator,
95
+ make_treatment_indicator,
96
+ rank_control_units,
97
+ summarize_did_data,
98
+ trim_weights,
99
+ validate_did_data,
100
+ wide_to_long,
101
+ )
102
+ from diff_diff.results import (
103
+ DiDResults,
104
+ MultiPeriodDiDResults,
105
+ PeriodEffect,
106
+ SyntheticDiDResults,
107
+ )
108
+ from diff_diff.survey import (
109
+ DEFFDiagnostics,
110
+ SurveyDesign,
111
+ SurveyMetadata,
112
+ compute_deff_diagnostics,
113
+ )
114
+ from diff_diff.staggered import (
115
+ CallawaySantAnna,
116
+ CallawaySantAnnaResults,
117
+ CSBootstrapResults,
118
+ GroupTimeEffect,
119
+ )
120
+ from diff_diff.imputation import (
121
+ ImputationBootstrapResults,
122
+ ImputationDiD,
123
+ ImputationDiDResults,
124
+ imputation_did,
125
+ )
126
+ from diff_diff.two_stage import (
127
+ TwoStageBootstrapResults,
128
+ TwoStageDiD,
129
+ TwoStageDiDResults,
130
+ two_stage_did,
131
+ )
132
+ from diff_diff.stacked_did import (
133
+ StackedDiD,
134
+ StackedDiDResults,
135
+ stacked_did,
136
+ )
137
+ from diff_diff.sun_abraham import (
138
+ SABootstrapResults,
139
+ SunAbraham,
140
+ SunAbrahamResults,
141
+ )
142
+ from diff_diff.triple_diff import (
143
+ TripleDifference,
144
+ TripleDifferenceResults,
145
+ triple_difference,
146
+ )
147
+ from diff_diff.staggered_triple_diff import (
148
+ StaggeredTripleDifference,
149
+ )
150
+ from diff_diff.staggered_triple_diff_results import (
151
+ StaggeredTripleDiffResults,
152
+ )
153
+ from diff_diff.continuous_did import (
154
+ ContinuousDiD,
155
+ ContinuousDiDResults,
156
+ DoseResponseCurve,
157
+ )
158
+ from diff_diff.efficient_did import (
159
+ EfficientDiD,
160
+ EfficientDiDResults,
161
+ EDiDBootstrapResults,
162
+ )
163
+ from diff_diff.trop import (
164
+ TROP,
165
+ TROPResults,
166
+ trop,
167
+ )
168
+ from diff_diff.wooldridge import WooldridgeDiD
169
+ from diff_diff.wooldridge_results import WooldridgeDiDResults
170
+ from diff_diff.utils import (
171
+ WildBootstrapResults,
172
+ check_parallel_trends,
173
+ check_parallel_trends_robust,
174
+ equivalence_test_trends,
175
+ wild_bootstrap_se,
176
+ )
177
+ from diff_diff.visualization import (
178
+ plot_bacon,
179
+ plot_dose_response,
180
+ plot_event_study,
181
+ plot_group_effects,
182
+ plot_group_time_heatmap,
183
+ plot_honest_event_study,
184
+ plot_power_curve,
185
+ plot_pretrends_power,
186
+ plot_sensitivity,
187
+ plot_staircase,
188
+ plot_synth_weights,
189
+ )
190
+ from diff_diff.practitioner import practitioner_next_steps
191
+ from diff_diff.datasets import (
192
+ clear_cache,
193
+ list_datasets,
194
+ load_card_krueger,
195
+ load_castle_doctrine,
196
+ load_dataset,
197
+ load_divorce_laws,
198
+ load_mpdta,
199
+ )
200
+
201
+ # Estimator aliases — short names for convenience
202
+ DiD = DifferenceInDifferences
203
+ TWFE = TwoWayFixedEffects
204
+ EventStudy = MultiPeriodDiD
205
+ SDiD = SyntheticDiD
206
+ CS = CallawaySantAnna
207
+ CDiD = ContinuousDiD
208
+ SA = SunAbraham
209
+ BJS = ImputationDiD
210
+ Gardner = TwoStageDiD
211
+ DDD = TripleDifference
212
+ SDDD = StaggeredTripleDifference
213
+ Stacked = StackedDiD
214
+ Bacon = BaconDecomposition
215
+ EDiD = EfficientDiD
216
+ ETWFE = WooldridgeDiD
217
+
218
+ __version__ = "3.0.1"
219
+ __all__ = [
220
+ # Estimators
221
+ "DifferenceInDifferences",
222
+ "TwoWayFixedEffects",
223
+ "MultiPeriodDiD",
224
+ "SyntheticDiD",
225
+ "CallawaySantAnna",
226
+ "ContinuousDiD",
227
+ "SunAbraham",
228
+ "ImputationDiD",
229
+ "TwoStageDiD",
230
+ "TripleDifference",
231
+ "TROP",
232
+ "StackedDiD",
233
+ # Estimator aliases (short names)
234
+ "DiD",
235
+ "TWFE",
236
+ "EventStudy",
237
+ "SDiD",
238
+ "CS",
239
+ "CDiD",
240
+ "SA",
241
+ "BJS",
242
+ "Gardner",
243
+ "DDD",
244
+ "SDDD",
245
+ "Stacked",
246
+ "Bacon",
247
+ # Bacon Decomposition
248
+ "BaconDecomposition",
249
+ "BaconDecompositionResults",
250
+ "Comparison2x2",
251
+ "bacon_decompose",
252
+ # Results
253
+ "DiDResults",
254
+ "MultiPeriodDiDResults",
255
+ "SyntheticDiDResults",
256
+ "PeriodEffect",
257
+ "CallawaySantAnnaResults",
258
+ "CSBootstrapResults",
259
+ "GroupTimeEffect",
260
+ "ContinuousDiDResults",
261
+ "DoseResponseCurve",
262
+ "SunAbrahamResults",
263
+ "SABootstrapResults",
264
+ "ImputationDiDResults",
265
+ "ImputationBootstrapResults",
266
+ "imputation_did",
267
+ "TwoStageDiDResults",
268
+ "TwoStageBootstrapResults",
269
+ "two_stage_did",
270
+ "TripleDifferenceResults",
271
+ "triple_difference",
272
+ "StaggeredTripleDifference",
273
+ "StaggeredTripleDiffResults",
274
+ "TROPResults",
275
+ "trop",
276
+ "StackedDiDResults",
277
+ "stacked_did",
278
+ # EfficientDiD
279
+ "EfficientDiD",
280
+ "EfficientDiDResults",
281
+ "EDiDBootstrapResults",
282
+ "EDiD",
283
+ # WooldridgeDiD (ETWFE)
284
+ "WooldridgeDiD",
285
+ "WooldridgeDiDResults",
286
+ "ETWFE",
287
+ # Visualization
288
+ "plot_bacon",
289
+ "plot_event_study",
290
+ "plot_group_effects",
291
+ "plot_sensitivity",
292
+ "plot_honest_event_study",
293
+ "plot_power_curve",
294
+ "plot_pretrends_power",
295
+ "plot_synth_weights",
296
+ "plot_staircase",
297
+ "plot_dose_response",
298
+ "plot_group_time_heatmap",
299
+ # Parallel trends testing
300
+ "check_parallel_trends",
301
+ "check_parallel_trends_robust",
302
+ "equivalence_test_trends",
303
+ # Wild cluster bootstrap
304
+ "WildBootstrapResults",
305
+ "wild_bootstrap_se",
306
+ # Placebo tests / diagnostics
307
+ "PlaceboTestResults",
308
+ "run_placebo_test",
309
+ "placebo_timing_test",
310
+ "placebo_group_test",
311
+ "permutation_test",
312
+ "leave_one_out_test",
313
+ "run_all_placebo_tests",
314
+ # Data preparation utilities
315
+ "make_treatment_indicator",
316
+ "make_post_indicator",
317
+ "wide_to_long",
318
+ "balance_panel",
319
+ "trim_weights",
320
+ "validate_did_data",
321
+ "summarize_did_data",
322
+ "generate_did_data",
323
+ "generate_staggered_data",
324
+ "generate_factor_data",
325
+ "generate_ddd_data",
326
+ "generate_panel_data",
327
+ "generate_event_study_data",
328
+ "generate_staggered_ddd_data",
329
+ "generate_survey_did_data",
330
+ "generate_continuous_did_data",
331
+ "create_event_time",
332
+ "aggregate_survey",
333
+ "aggregate_to_cohorts",
334
+ "rank_control_units",
335
+ # Honest DiD sensitivity analysis
336
+ "HonestDiD",
337
+ "HonestDiDResults",
338
+ "SensitivityResults",
339
+ "DeltaSD",
340
+ "DeltaRM",
341
+ "DeltaSDRM",
342
+ "compute_honest_did",
343
+ "sensitivity_plot",
344
+ # Power analysis
345
+ "PowerAnalysis",
346
+ "PowerResults",
347
+ "SimulationMDEResults",
348
+ "SimulationPowerResults",
349
+ "SimulationSampleSizeResults",
350
+ "compute_mde",
351
+ "compute_power",
352
+ "compute_sample_size",
353
+ "simulate_mde",
354
+ "simulate_power",
355
+ "simulate_sample_size",
356
+ # Pre-trends power analysis
357
+ "PreTrendsPower",
358
+ "PreTrendsPowerResults",
359
+ "PreTrendsPowerCurve",
360
+ "compute_pretrends_power",
361
+ "compute_mdv",
362
+ # Survey support
363
+ "SurveyDesign",
364
+ "SurveyMetadata",
365
+ "DEFFDiagnostics",
366
+ "compute_deff_diagnostics",
367
+ # Rust backend
368
+ "HAS_RUST_BACKEND",
369
+ # Linear algebra helpers
370
+ "LinearRegression",
371
+ "InferenceResult",
372
+ # Datasets
373
+ "load_card_krueger",
374
+ "load_castle_doctrine",
375
+ "load_divorce_laws",
376
+ "load_mpdta",
377
+ "load_dataset",
378
+ "list_datasets",
379
+ "clear_cache",
380
+ # Practitioner guidance
381
+ "practitioner_next_steps",
382
+ ]
diff_diff/_backend.py ADDED
@@ -0,0 +1,134 @@
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
+
42
+ _rust_available = True
43
+ except ImportError:
44
+ _rust_available = False
45
+ _rust_bootstrap_weights = None
46
+ _rust_synthetic_weights = None
47
+ _rust_project_simplex = None
48
+ _rust_solve_ols = None
49
+ _rust_compute_robust_vcov = None
50
+ # TROP estimator acceleration (local method)
51
+ _rust_unit_distance_matrix = None
52
+ _rust_loocv_grid_search = None
53
+ _rust_bootstrap_trop_variance = None
54
+ # TROP estimator acceleration (global method)
55
+ _rust_loocv_grid_search_global = None
56
+ _rust_bootstrap_trop_variance_global = None
57
+ # SDID weights (Frank-Wolfe matching R's synthdid)
58
+ _rust_sdid_unit_weights = None
59
+ _rust_compute_time_weights = None
60
+ _rust_compute_noise_level = None
61
+ _rust_sc_weight_fw = None
62
+ _rust_backend_info = None
63
+
64
+ # Determine final backend based on environment variable and availability
65
+ if _backend_env == "python":
66
+ # Force pure Python mode - disable Rust even if available
67
+ HAS_RUST_BACKEND = False
68
+ _rust_bootstrap_weights = None
69
+ _rust_synthetic_weights = None
70
+ _rust_project_simplex = None
71
+ _rust_solve_ols = None
72
+ _rust_compute_robust_vcov = None
73
+ # TROP estimator acceleration (local method)
74
+ _rust_unit_distance_matrix = None
75
+ _rust_loocv_grid_search = None
76
+ _rust_bootstrap_trop_variance = None
77
+ # TROP estimator acceleration (global method)
78
+ _rust_loocv_grid_search_global = None
79
+ _rust_bootstrap_trop_variance_global = None
80
+ # SDID weights (Frank-Wolfe matching R's synthdid)
81
+ _rust_sdid_unit_weights = None
82
+ _rust_compute_time_weights = None
83
+ _rust_compute_noise_level = None
84
+ _rust_sc_weight_fw = None
85
+ _rust_backend_info = None
86
+ elif _backend_env == "rust":
87
+ # Force Rust mode - fail if not available
88
+ if not _rust_available:
89
+ raise ImportError(
90
+ "DIFF_DIFF_BACKEND=rust but Rust backend is not available. "
91
+ "Install with: pip install diff-diff[rust]"
92
+ )
93
+ HAS_RUST_BACKEND = True
94
+ else:
95
+ # Auto mode - use Rust if available
96
+ HAS_RUST_BACKEND = _rust_available
97
+
98
+
99
+ def rust_backend_info():
100
+ """Return compile-time BLAS feature information for the Rust backend.
101
+
102
+ Returns a dict with keys:
103
+ - 'blas': True if any BLAS backend is linked
104
+ - 'accelerate': True if Apple Accelerate is linked (macOS)
105
+ - 'openblas': True if OpenBLAS is linked (Linux)
106
+
107
+ If the Rust backend is not available, all values are False.
108
+ """
109
+ if _rust_backend_info is not None:
110
+ return _rust_backend_info()
111
+ return {"blas": False, "accelerate": False, "openblas": False}
112
+
113
+
114
+ __all__ = [
115
+ "HAS_RUST_BACKEND",
116
+ "rust_backend_info",
117
+ "_rust_bootstrap_weights",
118
+ "_rust_synthetic_weights",
119
+ "_rust_project_simplex",
120
+ "_rust_solve_ols",
121
+ "_rust_compute_robust_vcov",
122
+ # TROP estimator acceleration (local method)
123
+ "_rust_unit_distance_matrix",
124
+ "_rust_loocv_grid_search",
125
+ "_rust_bootstrap_trop_variance",
126
+ # TROP estimator acceleration (global method)
127
+ "_rust_loocv_grid_search_global",
128
+ "_rust_bootstrap_trop_variance_global",
129
+ # SDID weights (Frank-Wolfe matching R's synthdid)
130
+ "_rust_sdid_unit_weights",
131
+ "_rust_compute_time_weights",
132
+ "_rust_compute_noise_level",
133
+ "_rust_sc_weight_fw",
134
+ ]