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