1
1
import unittest
2
2
import pandas as pd
3
3
import numpy as np
4
- import matplotlib .pyplot as plt
5
- from causal_testing .specification .variable import Input
4
+ from causal_testing .specification .variable import Input , Output
6
5
from causal_testing .utils .validation import CausalValidator
7
6
8
7
from causal_testing .estimation .linear_regression_estimator import LinearRegressionEstimator
9
8
from causal_testing .testing .base_test_case import BaseTestCase
10
- from causal_testing .specification .variable import Input , Output
11
9
12
10
13
11
def load_nhefs_df ():
@@ -77,7 +75,7 @@ def test_linear_regression_categorical_ate(self):
77
75
df = self .scarf_df .copy ()
78
76
base_test_case = BaseTestCase (Input ("color" , float ), Output ("completed" , float ))
79
77
logistic_regression_estimator = LinearRegressionEstimator (base_test_case , None , None , set (), df )
80
- ate , confidence = logistic_regression_estimator .estimate_coefficient ()
78
+ _ , confidence = logistic_regression_estimator .estimate_coefficient ()
81
79
self .assertTrue (all ([ci_low < 0 < ci_high for ci_low , ci_high in zip (confidence [0 ], confidence [1 ])]))
82
80
83
81
def test_program_11_2 (self ):
@@ -86,22 +84,8 @@ def test_program_11_2(self):
86
84
linear_regression_estimator = LinearRegressionEstimator (self .base_test_case , None , None , set (), df )
87
85
ate , _ = linear_regression_estimator .estimate_coefficient ()
88
86
89
- self .assertEqual (
90
- round (
91
- linear_regression_estimator .model .params ["Intercept" ]
92
- + 90 * linear_regression_estimator .model .params ["treatments" ],
93
- 1 ,
94
- ),
95
- 216.9 ,
96
- )
97
-
98
87
# Increasing treatments from 90 to 100 should be the same as 10 times the unit ATE
99
- self .assertTrue (
100
- all (
101
- round (linear_regression_estimator .model .params ["treatments" ], 1 ) == round (ate_single , 1 )
102
- for ate_single in ate
103
- )
104
- )
88
+ self .assertTrue (all (round (ate ["treatments" ], 1 ) == round (ate_single , 1 ) for ate_single in ate ))
105
89
106
90
def test_program_11_3 (self ):
107
91
"""Test whether our linear regression implementation produces the same results as program 11.3 (p. 144)."""
@@ -110,23 +94,8 @@ def test_program_11_3(self):
110
94
self .base_test_case , None , None , set (), df , formula = "outcomes ~ treatments + I(treatments ** 2)"
111
95
)
112
96
ate , _ = linear_regression_estimator .estimate_coefficient ()
113
- print (linear_regression_estimator .model .summary ())
114
- self .assertEqual (
115
- round (
116
- linear_regression_estimator .model .params ["Intercept" ]
117
- + 90 * linear_regression_estimator .model .params ["treatments" ]
118
- + 90 * 90 * linear_regression_estimator .model .params ["I(treatments ** 2)" ],
119
- 1 ,
120
- ),
121
- 197.1 ,
122
- )
123
97
# Increasing treatments from 90 to 100 should be the same as 10 times the unit ATE
124
- self .assertTrue (
125
- all (
126
- round (linear_regression_estimator .model .params ["treatments" ], 3 ) == round (ate_single , 3 )
127
- for ate_single in ate
128
- )
129
- )
98
+ self .assertTrue (all (round (ate ["treatments" ], 3 ) == round (ate_single , 3 ) for ate_single in ate ))
130
99
131
100
def test_program_15_1A (self ):
132
101
"""Test whether our linear regression implementation produces the same results as program 15.1 (p. 163, 184)."""
@@ -161,15 +130,9 @@ def test_program_15_1A(self):
161
130
I(smokeyrs ** 2) +
162
131
(qsmk * smokeintensity)""" ,
163
132
)
164
- # terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
165
- # terms_to_product = [("qsmk", "smokeintensity")]
166
- # for term_to_square in terms_to_square:
167
- # for term_a, term_b in terms_to_product:
168
- # linear_regression_estimator.add_product_term_to_df(term_a, term_b)
169
133
170
- linear_regression_estimator .estimate_coefficient ()
171
- self .assertEqual (round (linear_regression_estimator .model .params ["qsmk" ], 1 ), 2.6 )
172
- self .assertEqual (round (linear_regression_estimator .model .params ["qsmk:smokeintensity" ], 2 ), 0.05 )
134
+ coefficient , _ = linear_regression_estimator .estimate_coefficient ()
135
+ self .assertEqual (round (coefficient ["qsmk" ], 1 ), 2.6 )
173
136
174
137
def test_program_15_no_interaction (self ):
175
138
"""Test whether our linear regression implementation produces the same results as program 15.1 (p. 163, 184)
@@ -281,10 +244,11 @@ def test_program_11_2_with_robustness_validation(self):
281
244
"""Test whether our linear regression estimator, as used in test_program_11_2 can correctly estimate robustness."""
282
245
df = self .chapter_11_df .copy ()
283
246
linear_regression_estimator = LinearRegressionEstimator (self .base_test_case , 100 , 90 , set (), df )
284
- linear_regression_estimator .estimate_coefficient ()
285
247
286
248
cv = CausalValidator ()
287
- self .assertEqual (round (cv .estimate_robustness (linear_regression_estimator .model )["treatments" ], 4 ), 0.7353 )
249
+ self .assertEqual (
250
+ round (cv .estimate_robustness (linear_regression_estimator .fit_model ())["treatments" ], 4 ), 0.7353
251
+ )
288
252
289
253
def test_gp (self ):
290
254
df = pd .DataFrame ()
0 commit comments