-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathtest_apo_exceptions.py
More file actions
224 lines (192 loc) · 8.37 KB
/
test_apo_exceptions.py
File metadata and controls
224 lines (192 loc) · 8.37 KB
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
import numpy as np
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.linear_model import Lasso, LogisticRegression
from doubleml import DoubleMLAPO, DoubleMLData
from doubleml.irm.datasets import make_iivm_data, make_irm_data, make_irm_data_discrete_treatments
n = 100
data_apo = make_irm_data_discrete_treatments(n_obs=n)
df_apo = pd.DataFrame(
np.column_stack((data_apo["y"], data_apo["d"], data_apo["x"])),
columns=["y", "d"] + ["x" + str(i) for i in range(data_apo["x"].shape[1])],
)
dml_data = DoubleMLData(df_apo, "y", "d")
ml_g = Lasso()
ml_m = LogisticRegression()
@pytest.mark.ci
def test_apo_exception_data():
msg = (
r"The data must be of DoubleMLData or DoubleMLClusterData or DoubleMLDIDData or DoubleMLSSMData or "
r"DoubleMLRDDData type\. Empty DataFrame\nColumns: \[\]\nIndex: \[\] of type "
r"<class 'pandas\..*DataFrame'> was passed\."
)
with pytest.raises(TypeError, match=msg):
_ = DoubleMLAPO(pd.DataFrame(), ml_g, ml_m, treatment_level=0)
msg = "Only one treatment variable is allowed. Got 2 treatment variables."
with pytest.raises(ValueError, match=msg):
dml_data_multiple = DoubleMLData(df_apo, "y", ["d", "x1"])
_ = DoubleMLAPO(dml_data_multiple, ml_g, ml_m, treatment_level=0)
dml_data_z = make_iivm_data()
msg = r"Incompatible data. z have been set as instrumental variable\(s\)."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data_z, ml_g, ml_m, treatment_level=0)
msg = "The number of treated observations is less than 5. Number of treated observations: 0 for treatment level 1.1."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=1.1)
msg = r"The proportion of observations with treatment level 42 is less than 5\%. Got 0.70\%."
# test warning
with pytest.warns(UserWarning, match=msg):
data_apo_warn = make_irm_data_discrete_treatments(n_obs=1000)
data_apo_warn["d"][0:7] = 42
df_apo_warn = pd.DataFrame(
np.column_stack((data_apo_warn["y"], data_apo_warn["d"], data_apo_warn["x"])),
columns=["y", "d"] + ["x" + str(i) for i in range(data_apo_warn["x"].shape[1])],
)
dml_data_warn = DoubleMLData(df_apo_warn, "y", "d")
_ = DoubleMLAPO(dml_data_warn, ml_g, ml_m, treatment_level=42)
@pytest.mark.ci
def test_apo_exception_learner():
msg = (
r"The ml_g learner LogisticRegression\(\) was identified as classifier but the outcome variable is not"
" binary with values 0 and 1."
)
with pytest.raises(ValueError, match=msg):
ml_g_classifier = LogisticRegression()
_ = DoubleMLAPO(dml_data, ml_g_classifier, ml_m, treatment_level=0)
@pytest.mark.ci
def test_apo_exception_scores():
msg = "Invalid score MAR. Valid score APO."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, score="MAR")
@pytest.mark.ci
def test_apo_exception_ipw_normalization():
msg = "Normalization indicator has to be boolean. Object of type <class 'int'> passed."
with pytest.raises(TypeError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, normalize_ipw=1)
@pytest.mark.ci
def test_apo_exception_weights():
msg = "weights must be a numpy array or dictionary. weights of type <class 'int'> was passed."
with pytest.raises(TypeError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights=1)
msg = r"weights must have keys \['weights', 'weights_bar'\]. keys dict_keys\(\['d'\]\) were passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights={"d": [1, 2, 3]})
# shape checks
msg = rf"weights must have shape \({n},\). weights of shape \(1,\) was passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights=np.ones(1))
msg = rf"weights must have shape \({n},\). weights of shape \({n}, 2\) was passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights=np.ones((n, 2)))
msg = rf"weights must have shape \({n},\). weights of shape \(1,\) was passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights={"weights": np.ones(1), "weights_bar": np.ones(1)})
msg = rf"weights must have shape \({n},\). weights of shape \({n}, 2\) was passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data, ml_g, ml_m, treatment_level=0, weights={"weights": np.ones((n, 2)), "weights_bar": np.ones((n, 2))}
)
msg = rf"weights_bar must have shape \({n}, 1\). weights_bar of shape \({n}, 2\) was passed."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data, ml_g, ml_m, treatment_level=0, weights={"weights": np.ones(n), "weights_bar": np.ones((n, 2))}
)
# value checks
msg = "All weights values must be greater or equal 0."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data,
ml_g,
ml_m,
treatment_level=0,
weights=-1
* np.ones(
n,
),
)
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data,
ml_g,
ml_m,
treatment_level=0,
weights={
"weights": (
-1
* np.ones(
n,
)
),
"weights_bar": np.ones((n, 1)),
},
)
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data,
ml_g,
ml_m,
treatment_level=0,
weights={
"weights": np.ones(
n,
),
"weights_bar": -1 * np.ones((n, 1)),
},
)
msg = "At least one weight must be non-zero."
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(dml_data, ml_g, ml_m, treatment_level=0, weights=np.zeros((dml_data.d.shape[0],)))
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data,
ml_g,
ml_m,
treatment_level=0,
weights={"weights": np.zeros((dml_data.d.shape[0],)), "weights_bar": np.ones((dml_data.d.shape[0], 1))},
)
with pytest.raises(ValueError, match=msg):
_ = DoubleMLAPO(
dml_data,
ml_g,
ml_m,
treatment_level=0,
weights={"weights": np.ones((dml_data.d.shape[0],)), "weights_bar": np.zeros((dml_data.d.shape[0], 1))},
)
@pytest.mark.ci
def test_apo_exception_capo_gapo():
n = 20
# collect data
np.random.seed(42)
obj_dml_data = make_irm_data(n_obs=n, dim_x=2)
# First stage estimation
ml_g = RandomForestRegressor(n_estimators=10)
ml_m = RandomForestClassifier(n_estimators=10)
dml_obj = DoubleMLAPO(obj_dml_data, ml_m=ml_m, ml_g=ml_g, treatment_level=0)
dml_obj.fit()
# create a random basis
random_basis = pd.DataFrame(np.random.normal(0, 1, size=(n, 5)))
msg = "Invalid score APO_2. Valid score APO."
with pytest.raises(ValueError, match=msg):
dml_obj._score = "APO_2"
_ = dml_obj.capo(random_basis)
# reset the score
dml_obj._score = "APO"
msg = "Only implemented for one repetition. Number of repetitions is 2."
with pytest.raises(NotImplementedError, match=msg):
dml_obj._n_rep = 2
dml_obj.capo(random_basis)
# reset the number of repetitions
dml_obj._n_rep = 1
msg = "Groups must be of DataFrame type. Groups of type <class 'int'> was passed."
with pytest.raises(TypeError, match=msg):
_ = dml_obj.gapo(1)
groups_1 = pd.DataFrame(
np.column_stack([obj_dml_data.data["X1"] > 0.2, np.ones_like(obj_dml_data.data["X1"])]), columns=["Group 1", "Group 2"]
)
msg = (
r"Columns of groups must be of bool type or int type \(dummy coded\). Alternatively,"
" groups should only contain one column."
)
with pytest.raises(TypeError, match=msg):
_ = dml_obj.gapo(groups_1)