diff-diff 2.3.2__cp313-cp313-win_amd64.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 ADDED
@@ -0,0 +1,254 @@
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
+ generate_ddd_data,
75
+ generate_event_study_data,
76
+ generate_factor_data,
77
+ generate_panel_data,
78
+ generate_staggered_data,
79
+ make_post_indicator,
80
+ make_treatment_indicator,
81
+ rank_control_units,
82
+ summarize_did_data,
83
+ validate_did_data,
84
+ wide_to_long,
85
+ )
86
+ from diff_diff.results import (
87
+ DiDResults,
88
+ MultiPeriodDiDResults,
89
+ PeriodEffect,
90
+ SyntheticDiDResults,
91
+ )
92
+ from diff_diff.staggered import (
93
+ CallawaySantAnna,
94
+ CallawaySantAnnaResults,
95
+ CSBootstrapResults,
96
+ GroupTimeEffect,
97
+ )
98
+ from diff_diff.imputation import (
99
+ ImputationBootstrapResults,
100
+ ImputationDiD,
101
+ ImputationDiDResults,
102
+ imputation_did,
103
+ )
104
+ from diff_diff.sun_abraham import (
105
+ SABootstrapResults,
106
+ SunAbraham,
107
+ SunAbrahamResults,
108
+ )
109
+ from diff_diff.triple_diff import (
110
+ TripleDifference,
111
+ TripleDifferenceResults,
112
+ triple_difference,
113
+ )
114
+ from diff_diff.trop import (
115
+ TROP,
116
+ TROPResults,
117
+ trop,
118
+ )
119
+ from diff_diff.utils import (
120
+ WildBootstrapResults,
121
+ check_parallel_trends,
122
+ check_parallel_trends_robust,
123
+ equivalence_test_trends,
124
+ wild_bootstrap_se,
125
+ )
126
+ from diff_diff.visualization import (
127
+ plot_bacon,
128
+ plot_event_study,
129
+ plot_group_effects,
130
+ plot_honest_event_study,
131
+ plot_power_curve,
132
+ plot_pretrends_power,
133
+ plot_sensitivity,
134
+ )
135
+ from diff_diff.datasets import (
136
+ clear_cache,
137
+ list_datasets,
138
+ load_card_krueger,
139
+ load_castle_doctrine,
140
+ load_dataset,
141
+ load_divorce_laws,
142
+ load_mpdta,
143
+ )
144
+
145
+ __version__ = "2.3.2"
146
+ __all__ = [
147
+ # Estimators
148
+ "DifferenceInDifferences",
149
+ "TwoWayFixedEffects",
150
+ "MultiPeriodDiD",
151
+ "SyntheticDiD",
152
+ "CallawaySantAnna",
153
+ "SunAbraham",
154
+ "ImputationDiD",
155
+ "TripleDifference",
156
+ "TROP",
157
+ # Bacon Decomposition
158
+ "BaconDecomposition",
159
+ "BaconDecompositionResults",
160
+ "Comparison2x2",
161
+ "bacon_decompose",
162
+ "plot_bacon",
163
+ # Results
164
+ "DiDResults",
165
+ "MultiPeriodDiDResults",
166
+ "SyntheticDiDResults",
167
+ "PeriodEffect",
168
+ "CallawaySantAnnaResults",
169
+ "CSBootstrapResults",
170
+ "GroupTimeEffect",
171
+ "SunAbrahamResults",
172
+ "SABootstrapResults",
173
+ "ImputationDiDResults",
174
+ "ImputationBootstrapResults",
175
+ "imputation_did",
176
+ "TripleDifferenceResults",
177
+ "triple_difference",
178
+ "TROPResults",
179
+ "trop",
180
+ # Visualization
181
+ "plot_event_study",
182
+ "plot_group_effects",
183
+ "plot_sensitivity",
184
+ "plot_honest_event_study",
185
+ # Parallel trends testing
186
+ "check_parallel_trends",
187
+ "check_parallel_trends_robust",
188
+ "equivalence_test_trends",
189
+ # Wild cluster bootstrap
190
+ "WildBootstrapResults",
191
+ "wild_bootstrap_se",
192
+ # Placebo tests / diagnostics
193
+ "PlaceboTestResults",
194
+ "run_placebo_test",
195
+ "placebo_timing_test",
196
+ "placebo_group_test",
197
+ "permutation_test",
198
+ "leave_one_out_test",
199
+ "run_all_placebo_tests",
200
+ # Data preparation utilities
201
+ "make_treatment_indicator",
202
+ "make_post_indicator",
203
+ "wide_to_long",
204
+ "balance_panel",
205
+ "validate_did_data",
206
+ "summarize_did_data",
207
+ "generate_did_data",
208
+ "generate_staggered_data",
209
+ "generate_factor_data",
210
+ "generate_ddd_data",
211
+ "generate_panel_data",
212
+ "generate_event_study_data",
213
+ "create_event_time",
214
+ "aggregate_to_cohorts",
215
+ "rank_control_units",
216
+ # Honest DiD sensitivity analysis
217
+ "HonestDiD",
218
+ "HonestDiDResults",
219
+ "SensitivityResults",
220
+ "DeltaSD",
221
+ "DeltaRM",
222
+ "DeltaSDRM",
223
+ "compute_honest_did",
224
+ "sensitivity_plot",
225
+ # Power analysis
226
+ "PowerAnalysis",
227
+ "PowerResults",
228
+ "SimulationPowerResults",
229
+ "compute_mde",
230
+ "compute_power",
231
+ "compute_sample_size",
232
+ "simulate_power",
233
+ "plot_power_curve",
234
+ # Pre-trends power analysis
235
+ "PreTrendsPower",
236
+ "PreTrendsPowerResults",
237
+ "PreTrendsPowerCurve",
238
+ "compute_pretrends_power",
239
+ "compute_mdv",
240
+ "plot_pretrends_power",
241
+ # Rust backend
242
+ "HAS_RUST_BACKEND",
243
+ # Linear algebra helpers
244
+ "LinearRegression",
245
+ "InferenceResult",
246
+ # Datasets
247
+ "load_card_krueger",
248
+ "load_castle_doctrine",
249
+ "load_divorce_laws",
250
+ "load_mpdta",
251
+ "load_dataset",
252
+ "list_datasets",
253
+ "clear_cache",
254
+ ]
diff_diff/_backend.py ADDED
@@ -0,0 +1,112 @@
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 (twostep 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 (joint method)
31
+ loocv_grid_search_joint as _rust_loocv_grid_search_joint,
32
+ bootstrap_trop_variance_joint as _rust_bootstrap_trop_variance_joint,
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
+ )
39
+ _rust_available = True
40
+ except ImportError:
41
+ _rust_available = False
42
+ _rust_bootstrap_weights = None
43
+ _rust_synthetic_weights = None
44
+ _rust_project_simplex = None
45
+ _rust_solve_ols = None
46
+ _rust_compute_robust_vcov = None
47
+ # TROP estimator acceleration (twostep method)
48
+ _rust_unit_distance_matrix = None
49
+ _rust_loocv_grid_search = None
50
+ _rust_bootstrap_trop_variance = None
51
+ # TROP estimator acceleration (joint method)
52
+ _rust_loocv_grid_search_joint = None
53
+ _rust_bootstrap_trop_variance_joint = None
54
+ # SDID weights (Frank-Wolfe matching R's synthdid)
55
+ _rust_sdid_unit_weights = None
56
+ _rust_compute_time_weights = None
57
+ _rust_compute_noise_level = None
58
+ _rust_sc_weight_fw = None
59
+
60
+ # Determine final backend based on environment variable and availability
61
+ if _backend_env == 'python':
62
+ # Force pure Python mode - disable Rust even if available
63
+ HAS_RUST_BACKEND = False
64
+ _rust_bootstrap_weights = None
65
+ _rust_synthetic_weights = None
66
+ _rust_project_simplex = None
67
+ _rust_solve_ols = None
68
+ _rust_compute_robust_vcov = None
69
+ # TROP estimator acceleration (twostep method)
70
+ _rust_unit_distance_matrix = None
71
+ _rust_loocv_grid_search = None
72
+ _rust_bootstrap_trop_variance = None
73
+ # TROP estimator acceleration (joint method)
74
+ _rust_loocv_grid_search_joint = None
75
+ _rust_bootstrap_trop_variance_joint = None
76
+ # SDID weights (Frank-Wolfe matching R's synthdid)
77
+ _rust_sdid_unit_weights = None
78
+ _rust_compute_time_weights = None
79
+ _rust_compute_noise_level = None
80
+ _rust_sc_weight_fw = None
81
+ elif _backend_env == 'rust':
82
+ # Force Rust mode - fail if not available
83
+ if not _rust_available:
84
+ raise ImportError(
85
+ "DIFF_DIFF_BACKEND=rust but Rust backend is not available. "
86
+ "Install with: pip install diff-diff[rust]"
87
+ )
88
+ HAS_RUST_BACKEND = True
89
+ else:
90
+ # Auto mode - use Rust if available
91
+ HAS_RUST_BACKEND = _rust_available
92
+
93
+ __all__ = [
94
+ 'HAS_RUST_BACKEND',
95
+ '_rust_bootstrap_weights',
96
+ '_rust_synthetic_weights',
97
+ '_rust_project_simplex',
98
+ '_rust_solve_ols',
99
+ '_rust_compute_robust_vcov',
100
+ # TROP estimator acceleration (twostep method)
101
+ '_rust_unit_distance_matrix',
102
+ '_rust_loocv_grid_search',
103
+ '_rust_bootstrap_trop_variance',
104
+ # TROP estimator acceleration (joint method)
105
+ '_rust_loocv_grid_search_joint',
106
+ '_rust_bootstrap_trop_variance_joint',
107
+ # SDID weights (Frank-Wolfe matching R's synthdid)
108
+ '_rust_sdid_unit_weights',
109
+ '_rust_compute_time_weights',
110
+ '_rust_compute_noise_level',
111
+ '_rust_sc_weight_fw',
112
+ ]