forked from uber/causalml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_causal_trees.py
194 lines (174 loc) · 6.84 KB
/
test_causal_trees.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
import multiprocessing as mp
from abc import abstractmethod
import pandas as pd
import pytest
from sklearn.model_selection import train_test_split
from causalml.inference.tree import CausalTreeRegressor, CausalRandomForestRegressor
from causalml.metrics import ape
from causalml.metrics import qini_score
from .const import RANDOM_SEED, ERROR_THRESHOLD
class CausalTreeBase:
test_size: float = 0.2
control_name: int or str = 0
@abstractmethod
def prepare_model(self, *args, **kwargs):
return
@abstractmethod
def test_fit(self, *args, **kwargs):
return
@abstractmethod
def test_predict(self, *args, **kwargs):
return
def prepare_data(self, generate_regression_data) -> tuple:
y, X, treatment, tau, b, e = generate_regression_data(mode=2)
df = pd.DataFrame(X)
feature_names = [f"feature_{i}" for i in range(X.shape[1])]
df.columns = feature_names
df["outcome"] = y
df["treatment"] = treatment
df["treatment_effect"] = tau
self.df_train, self.df_test = train_test_split(
df, test_size=self.test_size, random_state=RANDOM_SEED
)
X_train, X_test = (
self.df_train[feature_names].values,
self.df_test[feature_names].values,
)
y_train, y_test = (
self.df_train["outcome"].values,
self.df_test["outcome"].values,
)
treatment_train, treatment_test = (
self.df_train["treatment"].values,
self.df_test["treatment"].values,
)
return X_train, X_test, y_train, y_test, treatment_train, treatment_test
class TestCausalTreeRegressor(CausalTreeBase):
def prepare_model(self) -> CausalTreeRegressor:
ctree = CausalTreeRegressor(
control_name=self.control_name, groups_cnt=True, random_state=RANDOM_SEED
)
return ctree
def test_fit(self, generate_regression_data):
ctree = self.prepare_model()
(
X_train,
X_test,
y_train,
y_test,
treatment_train,
treatment_test,
) = self.prepare_data(generate_regression_data)
ctree.fit(X=X_train, treatment=treatment_train, y=y_train)
df_result = pd.DataFrame(
{
"ctree_ite_pred": ctree.predict(X_test),
"is_treated": treatment_test,
"treatment_effect": self.df_test["treatment_effect"],
}
)
df_qini = qini_score(
df_result,
outcome_col="outcome",
treatment_col="is_treated",
treatment_effect_col="treatment_effect",
)
assert df_qini["ctree_ite_pred"] > df_qini["Random"]
@pytest.mark.parametrize("return_ci", (False, True))
@pytest.mark.parametrize("bootstrap_size", (500, 800))
@pytest.mark.parametrize("n_bootstraps", (1000,))
def test_fit_predict(
self, generate_regression_data, return_ci, bootstrap_size, n_bootstraps
):
y, X, treatment, tau, b, e = generate_regression_data(mode=1)
ctree = self.prepare_model()
output = ctree.fit_predict(
X=X,
treatment=treatment,
y=y,
return_ci=return_ci,
n_bootstraps=n_bootstraps,
bootstrap_size=bootstrap_size,
n_jobs=mp.cpu_count() - 1,
verbose=False,
)
if return_ci:
te, te_lower, te_upper = output
assert len(output) == 3
assert (te_lower <= te).all() and (te_upper >= te).all()
else:
te = output
assert te.shape[0] == y.shape[0]
def test_predict(self, generate_regression_data):
y, X, treatment, tau, b, e = generate_regression_data(mode=2)
ctree = self.prepare_model()
ctree.fit(X=X, y=y, treatment=treatment)
y_pred = ctree.predict(X[:1, :])
y_pred_with_outcomes = ctree.predict(X[:1, :], with_outcomes=True)
assert y_pred.shape == (1,)
assert y_pred_with_outcomes.shape == (1, 3)
def test_ate(self, generate_regression_data):
y, X, treatment, tau, b, e = generate_regression_data(mode=2)
ctree = self.prepare_model()
ate, ate_lower, ate_upper = ctree.estimate_ate(X=X, y=y, treatment=treatment)
assert (ate >= ate_lower) and (ate <= ate_upper)
assert ape(tau.mean(), ate) < ERROR_THRESHOLD
class TestCausalRandomForestRegressor(CausalTreeBase):
def prepare_model(self, n_estimators: int) -> CausalRandomForestRegressor:
crforest = CausalRandomForestRegressor(
criterion="causal_mse",
control_name=self.control_name,
n_estimators=n_estimators,
n_jobs=mp.cpu_count() - 1,
)
return crforest
@pytest.mark.parametrize("n_estimators", (5, 10, 50))
def test_fit(self, generate_regression_data, n_estimators):
crforest = self.prepare_model(n_estimators=n_estimators)
(
X_train,
X_test,
y_train,
y_test,
treatment_train,
treatment_test,
) = self.prepare_data(generate_regression_data)
crforest.fit(X=X_train, treatment=treatment_train, y=y_train)
df_result = pd.DataFrame(
{
"crforest_ite_pred": crforest.predict(X_test),
"is_treated": treatment_test,
"treatment_effect": self.df_test["treatment_effect"],
}
)
df_qini = qini_score(
df_result,
outcome_col="outcome",
treatment_col="is_treated",
treatment_effect_col="treatment_effect",
)
assert df_qini["crforest_ite_pred"] > df_qini["Random"]
@pytest.mark.parametrize("n_estimators", (5,))
def test_predict(self, generate_regression_data, n_estimators):
y, X, treatment, tau, b, e = generate_regression_data(mode=2)
ctree = self.prepare_model(n_estimators=n_estimators)
ctree.fit(X=X, y=y, treatment=treatment)
y_pred = ctree.predict(X[:1, :])
y_pred_with_outcomes = ctree.predict(X[:1, :], with_outcomes=True)
assert y_pred.shape == (1,)
assert y_pred_with_outcomes.shape == (1, 3)
@pytest.mark.parametrize("n_estimators", (5,))
def test_unbiased_sampling_error(self, generate_regression_data, n_estimators):
crforest = self.prepare_model(n_estimators=n_estimators)
(
X_train,
X_test,
y_train,
y_test,
treatment_train,
treatment_test,
) = self.prepare_data(generate_regression_data)
crforest.fit(X=X_train, treatment=treatment_train, y=y_train)
crforest_test_var = crforest.calculate_error(X_train=X_train, X_test=X_test)
assert (crforest_test_var > 0).all()
assert crforest_test_var.shape[0] == y_test.shape[0]