|
7 | 7 | CausalForestEstimator,
|
8 | 8 | LogisticRegressionEstimator,
|
9 | 9 | InstrumentalVariableEstimator,
|
| 10 | + PolynomialRegressionEstimator |
10 | 11 | )
|
11 | 12 | from causal_testing.specification.variable import Input
|
12 | 13 | from causal_testing.utils.validation import CausalValidator
|
@@ -409,6 +410,43 @@ def test_program_11_2_with_robustness_validation(self):
|
409 | 410 | self.assertEqual(round(cv.estimate_robustness(model)["treatments"], 4), 0.7353)
|
410 | 411 |
|
411 | 412 |
|
| 413 | +class TestPolynomialRegressionEstimator(TestLinearRegressionEstimator): |
| 414 | + |
| 415 | + @classmethod |
| 416 | + |
| 417 | + def setUpClass(cls): |
| 418 | + |
| 419 | + super().setUpClass() |
| 420 | + def test_program_11_3_polynomial(self): |
| 421 | + |
| 422 | + """Test whether the polynomial regression implementation produces the same results as program 11.3 (p. 162). |
| 423 | + https://www.hsph.harvard.edu/miguel-hernan/wp-content/uploads/sites/1268/2023/10/hernanrobins_WhatIf_30sep23.pdf |
| 424 | + """ |
| 425 | + |
| 426 | + df = self.chapter_11_df.copy() |
| 427 | + |
| 428 | + polynomial_estimator = PolynomialRegressionEstimator( |
| 429 | + "treatments", None, None, set(), "outcomes", 3, df) |
| 430 | + |
| 431 | + model = polynomial_estimator._run_linear_regression() |
| 432 | + |
| 433 | + ate, _ = polynomial_estimator.estimate_coefficient() |
| 434 | + |
| 435 | + self.assertEqual( |
| 436 | + round( |
| 437 | + model.params["Intercept"] |
| 438 | + + 90 * model.params["treatments"] |
| 439 | + + 90 * 90 * model.params["np.power(treatments, 2)"], |
| 440 | + 1, |
| 441 | + ), |
| 442 | + 197.1, |
| 443 | + ) |
| 444 | + # Increasing treatments from 90 to 100 should be the same as 10 times the unit ATE |
| 445 | + self.assertEqual(round(model.params["treatments"], 3), round(ate, 3)) |
| 446 | + |
| 447 | + |
| 448 | + |
| 449 | + |
412 | 450 | class TestCausalForestEstimator(unittest.TestCase):
|
413 | 451 | """Test the linear regression estimator against the programming exercises in Section 2 of Hernán and Robins [1].
|
414 | 452 |
|
@@ -491,3 +529,5 @@ def test_X1_effect(self):
|
491 | 529 | test_results = lr_model.estimate_ate()
|
492 | 530 | ate = test_results[0]
|
493 | 531 | self.assertAlmostEqual(ate, 2.0)
|
| 532 | + |
| 533 | + |
0 commit comments