diff-diff 2.0.4__cp312-cp312-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 ADDED
@@ -0,0 +1,226 @@
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.utils import (
104
+ WildBootstrapResults,
105
+ check_parallel_trends,
106
+ check_parallel_trends_robust,
107
+ equivalence_test_trends,
108
+ wild_bootstrap_se,
109
+ )
110
+ from diff_diff.visualization import (
111
+ plot_bacon,
112
+ plot_event_study,
113
+ plot_group_effects,
114
+ plot_honest_event_study,
115
+ plot_power_curve,
116
+ plot_pretrends_power,
117
+ plot_sensitivity,
118
+ )
119
+ from diff_diff.datasets import (
120
+ clear_cache,
121
+ list_datasets,
122
+ load_card_krueger,
123
+ load_castle_doctrine,
124
+ load_dataset,
125
+ load_divorce_laws,
126
+ load_mpdta,
127
+ )
128
+
129
+ __version__ = "2.0.4"
130
+ __all__ = [
131
+ # Estimators
132
+ "DifferenceInDifferences",
133
+ "TwoWayFixedEffects",
134
+ "MultiPeriodDiD",
135
+ "SyntheticDiD",
136
+ "CallawaySantAnna",
137
+ "SunAbraham",
138
+ "TripleDifference",
139
+ # Bacon Decomposition
140
+ "BaconDecomposition",
141
+ "BaconDecompositionResults",
142
+ "Comparison2x2",
143
+ "bacon_decompose",
144
+ "plot_bacon",
145
+ # Results
146
+ "DiDResults",
147
+ "MultiPeriodDiDResults",
148
+ "SyntheticDiDResults",
149
+ "PeriodEffect",
150
+ "CallawaySantAnnaResults",
151
+ "CSBootstrapResults",
152
+ "GroupTimeEffect",
153
+ "SunAbrahamResults",
154
+ "SABootstrapResults",
155
+ "TripleDifferenceResults",
156
+ "triple_difference",
157
+ # Visualization
158
+ "plot_event_study",
159
+ "plot_group_effects",
160
+ "plot_sensitivity",
161
+ "plot_honest_event_study",
162
+ # Parallel trends testing
163
+ "check_parallel_trends",
164
+ "check_parallel_trends_robust",
165
+ "equivalence_test_trends",
166
+ # Wild cluster bootstrap
167
+ "WildBootstrapResults",
168
+ "wild_bootstrap_se",
169
+ # Placebo tests / diagnostics
170
+ "PlaceboTestResults",
171
+ "run_placebo_test",
172
+ "placebo_timing_test",
173
+ "placebo_group_test",
174
+ "permutation_test",
175
+ "leave_one_out_test",
176
+ "run_all_placebo_tests",
177
+ # Data preparation utilities
178
+ "make_treatment_indicator",
179
+ "make_post_indicator",
180
+ "wide_to_long",
181
+ "balance_panel",
182
+ "validate_did_data",
183
+ "summarize_did_data",
184
+ "generate_did_data",
185
+ "create_event_time",
186
+ "aggregate_to_cohorts",
187
+ "rank_control_units",
188
+ # Honest DiD sensitivity analysis
189
+ "HonestDiD",
190
+ "HonestDiDResults",
191
+ "SensitivityResults",
192
+ "DeltaSD",
193
+ "DeltaRM",
194
+ "DeltaSDRM",
195
+ "compute_honest_did",
196
+ "sensitivity_plot",
197
+ # Power analysis
198
+ "PowerAnalysis",
199
+ "PowerResults",
200
+ "SimulationPowerResults",
201
+ "compute_mde",
202
+ "compute_power",
203
+ "compute_sample_size",
204
+ "simulate_power",
205
+ "plot_power_curve",
206
+ # Pre-trends power analysis
207
+ "PreTrendsPower",
208
+ "PreTrendsPowerResults",
209
+ "PreTrendsPowerCurve",
210
+ "compute_pretrends_power",
211
+ "compute_mdv",
212
+ "plot_pretrends_power",
213
+ # Rust backend
214
+ "HAS_RUST_BACKEND",
215
+ # Linear algebra helpers
216
+ "LinearRegression",
217
+ "InferenceResult",
218
+ # Datasets
219
+ "load_card_krueger",
220
+ "load_castle_doctrine",
221
+ "load_divorce_laws",
222
+ "load_mpdta",
223
+ "load_dataset",
224
+ "list_datasets",
225
+ "clear_cache",
226
+ ]
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
+ ]