Beispiel #1
0
 def test_multi_output_debiased_lasso(self):
     """Test MultiOutputDebiasedLasso."""
     # Test that attributes propagate correctly
     est = MultiOutputDebiasedLasso()
     multioutput_attrs = est.get_params()
     debiased_attrs = DebiasedLasso().get_params()
     for attr in debiased_attrs:
         self.assertTrue(attr in multioutput_attrs)
     # Test MultiOutputDebiasedLasso without weights
     # --> Check debiased coeffcients without intercept
     params = {'fit_intercept': False}
     self._check_debiased_coefs(TestLassoExtensions.X, TestLassoExtensions.y_2D_consistent,
                                sample_weight=None,
                                expected_coefs=[TestLassoExtensions.coefs1, TestLassoExtensions.coefs2],
                                params=params)
     # --> Check debiased coeffcients with intercept
     intercept_2D = np.array([TestLassoExtensions.intercept1, TestLassoExtensions.intercept2])
     self._check_debiased_coefs(TestLassoExtensions.X,
                                TestLassoExtensions.y_2D_consistent + intercept_2D,
                                sample_weight=None,
                                expected_coefs=[TestLassoExtensions.coefs1, TestLassoExtensions.coefs2],
                                expected_intercept=intercept_2D)
     # --> Check CI coverage
     self._check_debiased_CI_2D(TestLassoExtensions.X,
                                TestLassoExtensions.y_2D_consistent + intercept_2D,
                                sample_weight=None,
                                expected_coefs=np.array([TestLassoExtensions.coefs1, TestLassoExtensions.coefs2]),
                                expected_intercept=intercept_2D)
     # Test MultiOutputDebiasedLasso with weights
     # Define weights
     sample_weight = np.concatenate((np.ones(TestLassoExtensions.n_samples // 2),
                                     np.ones(TestLassoExtensions.n_samples // 2) * 2))
     # Define extended datasets
     X_expanded = np.concatenate(
         (TestLassoExtensions.X, TestLassoExtensions.X[TestLassoExtensions.n_samples // 2:]))
     y_expanded = np.concatenate(
         (TestLassoExtensions.y_2D_consistent,
          TestLassoExtensions.y_2D_consistent[TestLassoExtensions.n_samples // 2:]))
     # --> Check debiased coefficients
     weighted_debiased_coefs = self._check_debiased_coefs(
         TestLassoExtensions.X,
         TestLassoExtensions.y_2D_consistent,
         sample_weight=sample_weight,
         expected_coefs=[TestLassoExtensions.coefs1, TestLassoExtensions.coefs2],
         params=params)
     expanded_debiased_coefs = self._check_debiased_coefs(
         X_expanded, y_expanded, sample_weight=None,
         expected_coefs=[TestLassoExtensions.coefs1, TestLassoExtensions.coefs2],
         params=params)
     for i in range(2):
         self.assertTrue(np.allclose(weighted_debiased_coefs[i], expanded_debiased_coefs[i]))
 def __init__(self,
              model_y=WeightedLassoCVWrapper(), model_t='auto',
              alpha='auto',
              max_iter=1000,
              tol=1e-4,
              featurizer=None,
              fit_cate_intercept=True,
              linear_first_stages=True,
              discrete_treatment=False,
              n_splits=2,
              random_state=None):
     model_final = MultiOutputDebiasedLasso(
         alpha=alpha,
         fit_intercept=False,
         max_iter=max_iter,
         tol=tol)
     super().__init__(model_y=model_y,
                      model_t=model_t,
                      model_final=model_final,
                      featurizer=featurizer,
                      fit_cate_intercept=fit_cate_intercept,
                      linear_first_stages=linear_first_stages,
                      discrete_treatment=discrete_treatment,
                      n_splits=n_splits,
                      random_state=random_state)
 def _check_debiased_coefs(self,
                           X,
                           y,
                           sample_weight,
                           expected_coefs,
                           expected_intercept=0,
                           params={}):
     debiased_lasso = MultiOutputDebiasedLasso(
     ) if np.ndim(y) > 1 else DebiasedLasso()
     debiased_lasso.set_params(**params)
     debiased_lasso.fit(X, y, sample_weight)
     all_params = debiased_lasso.get_params()
     # Check coeffcients and intercept are the same within tolerance
     if np.ndim(y) > 1:
         for i in range(y.shape[1]):
             np.testing.assert_allclose(debiased_lasso.coef_[i],
                                        expected_coefs[i],
                                        atol=5e-2)
             if all_params["fit_intercept"]:
                 self.assertAlmostEqual(debiased_lasso.intercept_[i],
                                        expected_intercept[i],
                                        delta=1e-2)
     else:
         np.testing.assert_allclose(debiased_lasso.coef_,
                                    expected_coefs,
                                    atol=5e-2)
         if all_params["fit_intercept"]:
             self.assertAlmostEqual(debiased_lasso.intercept_,
                                    expected_intercept,
                                    delta=1e-2)
     return debiased_lasso.coef_
 def _check_debiased_CI_2D(self,
                           X,
                           y,
                           sample_weight,
                           expected_coefs,
                           expected_intercept=0,
                           n_experiments=200,
                           params={}):
     # Unit vectors
     X_test = np.eye(TestLassoExtensions.n_dim)
     y_test_mean = expected_intercept + expected_coefs.T
     is_in_interval = np.zeros(
         (y.shape[1], n_experiments, TestLassoExtensions.n_dim))
     for i in range(n_experiments):
         np.random.seed(i)
         X_exp = np.random.normal(size=X.shape)
         err = np.random.normal(scale=TestLassoExtensions.error_sd,
                                size=(X.shape[0], y.shape[1]))
         y_exp = expected_intercept + np.dot(X_exp, expected_coefs.T) + err
         debiased_lasso = MultiOutputDebiasedLasso()
         debiased_lasso.set_params(**params)
         debiased_lasso.fit(X_exp, y_exp, sample_weight)
         y_lower, y_upper = debiased_lasso.predict_interval(X_test,
                                                            alpha=0.1)
         for j in range(y.shape[1]):
             is_in_interval[j,
                            i, :] = ((y_test_mean[:, j] >= y_lower[:, j]) &
                                     (y_test_mean[:, j] <= y_upper[:, j]))
     for i in range(y.shape[1]):
         CI_coverage = np.mean(is_in_interval[i], axis=0)
         self.assertTrue(all(CI_coverage >= 0.85))