forked from pymc-labs/CausalPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_integration_pymc_examples.py
513 lines (452 loc) · 18.7 KB
/
test_integration_pymc_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# Copyright 2024 The PyMC Labs Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pandas as pd
import pytest
import causalpy as cp
sample_kwargs = {"tune": 20, "draws": 20, "chains": 2, "cores": 2}
@pytest.mark.integration
def test_did():
"""
Test Difference in Differences (DID) PyMC experiment.
Loads data and checks:
1. data is a dataframe
2. pymc_experiements.DifferenceInDifferences returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = cp.load_data("did")
result = cp.pymc_experiments.DifferenceInDifferences(
df,
formula="y ~ 1 + group*post_treatment",
time_variable_name="t",
group_variable_name="group",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.DifferenceInDifferences)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
# TODO: set up fixture for the banks dataset
@pytest.mark.integration
def test_did_banks_simple():
"""
Test simple Differences In Differences Experiment on the 'banks' data set.
:code: `formula="bib ~ 1 + district * post_treatment"`
Loads, transforms data and checks:
1. data is a dataframe
2. pymc_experiements.DifferenceInDifferences returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
treatment_time = 1930.5
df = (
cp.load_data("banks")
.filter(items=["bib6", "bib8", "year"])
.rename(columns={"bib6": "Sixth District", "bib8": "Eighth District"})
.groupby("year")
.median()
)
# SET TREATMENT TIME TO ZERO =========
df.index = df.index - treatment_time
treatment_time = 0
# ====================================
df.reset_index(level=0, inplace=True)
df_long = pd.melt(
df,
id_vars=["year"],
value_vars=["Sixth District", "Eighth District"],
var_name="district",
value_name="bib",
).sort_values("year")
df_long["unit"] = df_long["district"]
df_long["post_treatment"] = df_long.year >= treatment_time
df_long = df_long.replace({"district": {"Sixth District": 1, "Eighth District": 0}})
result = cp.pymc_experiments.DifferenceInDifferences(
# df_long[df_long.year.isin([1930, 1931])],
df_long[df_long.year.isin([-0.5, 0.5])],
formula="bib ~ 1 + district * post_treatment",
time_variable_name="year",
group_variable_name="district",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.DifferenceInDifferences)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_did_banks_multi():
"""
Test multiple regression Differences In Differences Experiment on the 'banks'
data set.
:code: `formula="bib ~ 1 + year + district + post_treatment + district:post_treatment"` # noqa: E501
Loads, transforms data and checks:
1. data is a dataframe
2. pymc_experiements.DifferenceInDifferences returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
treatment_time = 1930.5
df = (
cp.load_data("banks")
.filter(items=["bib6", "bib8", "year"])
.rename(columns={"bib6": "Sixth District", "bib8": "Eighth District"})
.groupby("year")
.median()
)
# SET TREATMENT TIME TO ZERO =========
df.index = df.index - treatment_time
treatment_time = 0
# ====================================
df.reset_index(level=0, inplace=True)
df_long = pd.melt(
df,
id_vars=["year"],
value_vars=["Sixth District", "Eighth District"],
var_name="district",
value_name="bib",
).sort_values("year")
df_long["unit"] = df_long["district"]
df_long["post_treatment"] = df_long.year >= treatment_time
df_long = df_long.replace({"district": {"Sixth District": 1, "Eighth District": 0}})
result = cp.pymc_experiments.DifferenceInDifferences(
df_long,
formula="bib ~ 1 + year + district + post_treatment + district:post_treatment",
time_variable_name="year",
group_variable_name="district",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.DifferenceInDifferences)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_rd():
"""
Test Regression Discontinuity experiment.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.RegressionDiscontinuity returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = cp.load_data("rd")
result = cp.pymc_experiments.RegressionDiscontinuity(
df,
formula="y ~ 1 + bs(x, df=6) + treated",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
treatment_threshold=0.5,
epsilon=0.001,
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.RegressionDiscontinuity)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_rd_bandwidth():
"""
Test Regression Discontinuity experiment with bandwidth parameter.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.RegressionDiscontinuity returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = cp.load_data("rd")
result = cp.pymc_experiments.RegressionDiscontinuity(
df,
formula="y ~ 1 + x + treated + x:treated",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
treatment_threshold=0.5,
epsilon=0.001,
bandwidth=0.3,
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.RegressionDiscontinuity)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_rd_drinking():
"""
Test Regression Discontinuity experiment on drinking age data.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.RegressionDiscontinuity returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = (
cp.load_data("drinking")
.rename(columns={"agecell": "age"})
.assign(treated=lambda df_: df_.age > 21)
)
result = cp.pymc_experiments.RegressionDiscontinuity(
df,
formula="all ~ 1 + age + treated",
running_variable_name="age",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
treatment_threshold=21,
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.RegressionDiscontinuity)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
def setup_regression_kink_data(kink):
"""Set up data for regression kink design tests"""
# define parameters for data generation
seed = 42
rng = np.random.default_rng(seed)
N = 50
kink = 0.5
beta = [0, -1, 0, 2, 0]
sigma = 0.05
# generate data
x = rng.uniform(-1, 1, N)
y = reg_kink_function(x, beta, kink) + rng.normal(0, sigma, N)
return pd.DataFrame({"x": x, "y": y, "treated": x >= kink})
def reg_kink_function(x, beta, kink):
"""Utility function for regression kink design. Returns a piecewise linear function
evaluated at x with a kink at kink and parameters beta"""
return (
beta[0]
+ beta[1] * x
+ beta[2] * x**2
+ beta[3] * (x - kink) * (x >= kink)
+ beta[4] * (x - kink) ** 2 * (x >= kink)
)
@pytest.mark.integration
def test_rkink():
"""
Test Regression Kink design.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.RegressionKink returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
kink = 0.5
df = setup_regression_kink_data(kink)
result = cp.pymc_experiments.RegressionKink(
df,
formula=f"y ~ 1 + x + I((x-{kink})*treated)",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
kink_point=kink,
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.RegressionKink)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_rkink_bandwidth():
"""
Test Regression Kink experiment with bandwidth parameter.
Generates synthetic data and checks:
1. data is a dataframe
2. pymc_experiments.RegressionKink returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
kink = 0.5
df = setup_regression_kink_data(kink)
result = cp.pymc_experiments.RegressionKink(
df,
formula=f"y ~ 1 + x + I((x-{kink})*treated)",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
kink_point=kink,
bandwidth=0.3,
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.RegressionKink)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_its():
"""
Test Interrupted Time-Series experiment.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.SyntheticControl returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = (
cp.load_data("its")
.assign(date=lambda x: pd.to_datetime(x["date"]))
.set_index("date")
)
treatment_time = pd.to_datetime("2017-01-01")
result = cp.pymc_experiments.SyntheticControl(
df,
treatment_time,
formula="y ~ 1 + t + C(month)",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.SyntheticControl)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_its_covid():
"""
Test Interrupted Time-Series experiment on COVID data.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.InterruptedtimeSeries returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = (
cp.load_data("covid")
.assign(date=lambda x: pd.to_datetime(x["date"]))
.set_index("date")
)
treatment_time = pd.to_datetime("2020-01-01")
result = cp.pymc_experiments.InterruptedTimeSeries(
df,
treatment_time,
formula="standardize(deaths) ~ 0 + standardize(t) + C(month) + standardize(temp)", # noqa E501
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.InterruptedTimeSeries)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_sc():
"""
Test Synthetic Control experiment.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.SyntheticControl returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = cp.load_data("sc")
treatment_time = 70
result = cp.pymc_experiments.SyntheticControl(
df,
treatment_time,
formula="actual ~ 0 + a + b + c + d + e + f + g",
model=cp.pymc_models.WeightedSumFitter(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.SyntheticControl)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_sc_brexit():
"""
Test Synthetic Control experiment on Brexit data.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.SyntheticControl returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = (
cp.load_data("brexit")
.assign(Time=lambda x: pd.to_datetime(x["Time"]))
.set_index("Time")
.loc[lambda x: x.index >= "2009-01-01"]
.drop(["Japan", "Italy", "US", "Spain"], axis=1)
)
treatment_time = pd.to_datetime("2016 June 24")
target_country = "UK"
all_countries = df.columns
other_countries = all_countries.difference({target_country})
all_countries = list(all_countries)
other_countries = list(other_countries)
formula = target_country + " ~ " + "0 + " + " + ".join(other_countries)
result = cp.pymc_experiments.SyntheticControl(
df,
treatment_time,
formula=formula,
model=cp.pymc_models.WeightedSumFitter(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.SyntheticControl)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_ancova():
"""
Test Pre-PostNEGD experiment on anova1 data.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.PrePostNEGD returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = cp.load_data("anova1")
result = cp.pymc_experiments.PrePostNEGD(
df,
formula="post ~ 1 + C(group) + pre",
group_variable_name="group",
pretreatment_variable_name="pre",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.PrePostNEGD)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_geolift1():
"""
Test Synthetic Control experiment on geo lift data.
Loads data and checks:
1. data is a dataframe
2. pymc_experiments.SyntheticControl returns correct type
3. the correct number of MCMC chains exists in the posterior inference data
4. the correct number of MCMC draws exists in the posterior inference data
"""
df = (
cp.load_data("geolift1")
.assign(time=lambda x: pd.to_datetime(x["time"]))
.set_index("time")
)
treatment_time = pd.to_datetime("2022-01-01")
result = cp.pymc_experiments.SyntheticControl(
df,
treatment_time,
formula="""Denmark ~ 0 + Austria + Belgium + Bulgaria + Croatia + Cyprus
+ Czech_Republic""",
model=cp.pymc_models.WeightedSumFitter(sample_kwargs=sample_kwargs),
)
assert isinstance(df, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.SyntheticControl)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]
@pytest.mark.integration
def test_iv_reg():
df = cp.load_data("risk")
instruments_formula = "risk ~ 1 + logmort0"
formula = "loggdp ~ 1 + risk"
instruments_data = df[["risk", "logmort0"]]
data = df[["loggdp", "risk"]]
result = cp.pymc_experiments.InstrumentalVariable(
instruments_data=instruments_data,
data=data,
instruments_formula=instruments_formula,
formula=formula,
model=cp.pymc_models.InstrumentalVariableRegression(
sample_kwargs=sample_kwargs
),
)
result.model.sample_predictive_distribution(ppc_sampler="pymc")
assert isinstance(df, pd.DataFrame)
assert isinstance(data, pd.DataFrame)
assert isinstance(instruments_data, pd.DataFrame)
assert isinstance(result, cp.pymc_experiments.InstrumentalVariable)
assert len(result.idata.posterior.coords["chain"]) == sample_kwargs["chains"]
assert len(result.idata.posterior.coords["draw"]) == sample_kwargs["draws"]