|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +from sklearn.gaussian_process import GaussianProcessRegressor |
| 4 | +from sklearn.gaussian_process.kernels import ExpSineSquared, WhiteKernel |
| 5 | +from sklearn.linear_model import LinearRegression |
| 6 | + |
| 7 | +import causalpy as cp |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.integration |
| 11 | +def test_did(): |
| 12 | + data = cp.load_data("did") |
| 13 | + assert type(data) is pd.DataFrame |
| 14 | + result = cp.skl_experiments.DifferenceInDifferences( |
| 15 | + data, |
| 16 | + formula="y ~ 1 + group + t + treated:group", |
| 17 | + time_variable_name="t", |
| 18 | + prediction_model=LinearRegression(), |
| 19 | + ) |
| 20 | + assert type(result) is cp.skl_experiments.DifferenceInDifferences |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.integration |
| 24 | +def test_rd_drinking(): |
| 25 | + df = ( |
| 26 | + cp.load_data("drinking") |
| 27 | + .rename(columns={"agecell": "age"}) |
| 28 | + .assign(treated=lambda df_: df_.age > 21) |
| 29 | + .dropna(axis=0) |
| 30 | + ) |
| 31 | + assert type(df) is pd.DataFrame |
| 32 | + result = cp.skl_experiments.RegressionDiscontinuity( |
| 33 | + df, |
| 34 | + formula="all ~ 1 + age + treated", |
| 35 | + running_variable_name="age", |
| 36 | + prediction_model=LinearRegression(), |
| 37 | + treatment_threshold=21, |
| 38 | + ) |
| 39 | + assert type(result) is cp.skl_experiments.RegressionDiscontinuity |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.integration |
| 43 | +def test_its(): |
| 44 | + df = cp.load_data("its") |
| 45 | + df["date"] = pd.to_datetime(df["date"]) |
| 46 | + df.set_index("date", inplace=True) |
| 47 | + treatment_time = pd.to_datetime("2017-01-01") |
| 48 | + assert type(df) is pd.DataFrame |
| 49 | + result = cp.skl_experiments.SyntheticControl( |
| 50 | + df, |
| 51 | + treatment_time, |
| 52 | + formula="y ~ 1 + t + C(month)", |
| 53 | + prediction_model=LinearRegression(), |
| 54 | + ) |
| 55 | + assert type(result) is cp.skl_experiments.SyntheticControl |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.integration |
| 59 | +def test_sc(): |
| 60 | + df = cp.load_data("sc") |
| 61 | + treatment_time = 70 |
| 62 | + assert type(df) is pd.DataFrame |
| 63 | + result = cp.skl_experiments.SyntheticControl( |
| 64 | + df, |
| 65 | + treatment_time, |
| 66 | + formula="actual ~ 0 + a + b + c + d + e + f + g", |
| 67 | + prediction_model=cp.skl_models.WeightedProportion(), |
| 68 | + ) |
| 69 | + assert type(result) is cp.skl_experiments.SyntheticControl |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.integration |
| 73 | +def test_rd_linear_main_effects(): |
| 74 | + data = cp.load_data("rd") |
| 75 | + assert type(data) is pd.DataFrame |
| 76 | + result = cp.skl_experiments.RegressionDiscontinuity( |
| 77 | + data, |
| 78 | + formula="y ~ 1 + x + treated", |
| 79 | + prediction_model=LinearRegression(), |
| 80 | + treatment_threshold=0.5, |
| 81 | + ) |
| 82 | + assert type(result) is cp.skl_experiments.RegressionDiscontinuity |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.integration |
| 86 | +def test_rd_linear_with_interaction(): |
| 87 | + data = cp.load_data("rd") |
| 88 | + assert type(data) is pd.DataFrame |
| 89 | + result = cp.skl_experiments.RegressionDiscontinuity( |
| 90 | + data, |
| 91 | + formula="y ~ 1 + x + treated + x:treated", |
| 92 | + prediction_model=LinearRegression(), |
| 93 | + treatment_threshold=0.5, |
| 94 | + ) |
| 95 | + assert type(result) is cp.skl_experiments.RegressionDiscontinuity |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.integration |
| 99 | +def test_rd_linear_with_gaussian_process(): |
| 100 | + data = cp.load_data("rd") |
| 101 | + assert type(data) is pd.DataFrame |
| 102 | + kernel = 1.0 * ExpSineSquared(1.0, 5.0) + WhiteKernel(1e-1) |
| 103 | + result = cp.skl_experiments.RegressionDiscontinuity( |
| 104 | + data, |
| 105 | + formula="y ~ 1 + x + treated", |
| 106 | + prediction_model=GaussianProcessRegressor(kernel=kernel), |
| 107 | + treatment_threshold=0.5, |
| 108 | + ) |
| 109 | + assert type(result) is cp.skl_experiments.RegressionDiscontinuity |
0 commit comments