Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the alpha grid search in the DebiasedLasso. #211

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions econml/sklearn_extensions/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,10 @@ def fit(self, X, y, sample_weight=None, check_input=True):
Allow to bypass several input checking.
Don't use this parameter unless you know what you do.
"""
alpha_grid = [0.01, 0.02, 0.03, 0.06, 0.1, 0.2, 0.3, 0.5, 0.8, 1]
self.selected_alpha_ = None
if self.alpha == 'auto':
# Select optimal penalty
self.alpha = self._get_optimal_alpha(
alpha_grid, X, y, sample_weight)
self.alpha = self._get_optimal_alpha(X, y, sample_weight)
self.selected_alpha_ = self.alpha
else:
# Warn about consistency
Expand Down Expand Up @@ -794,9 +792,9 @@ def _get_coef_correction(self, X, y, y_pred, sample_weight, theta_hat):
np.matmul(theta_hat, X.T), y_res_scaled)
return delta_coef

def _get_optimal_alpha(self, alpha_grid, X, y, sample_weight):
def _get_optimal_alpha(self, X, y, sample_weight):
# To be done once per target. Assumes y can be flattened.
cv_estimator = WeightedLassoCV(alphas=alpha_grid, cv=5, fit_intercept=self.fit_intercept)
cv_estimator = WeightedLassoCV(cv=5, fit_intercept=self.fit_intercept)
cv_estimator.fit(X, y.flatten(), sample_weight=sample_weight)
return cv_estimator.alpha_

Expand Down
16 changes: 8 additions & 8 deletions econml/tests/test_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_debiased_lasso_one_DGP(self):
expected_coefs=TestLassoExtensions.coefs1)
expanded_debiased_coefs = self._check_debiased_coefs(X_expanded, y_expanded, sample_weight=None,
expected_coefs=TestLassoExtensions.coefs1)
self.assertTrue(np.allclose(weighted_debiased_coefs, expanded_debiased_coefs))
np.testing.assert_allclose(weighted_debiased_coefs, expanded_debiased_coefs, atol=5e-2)

def test_debiased_lasso_mixed_DGP(self):
"""Test WeightedLasso with two sets of coefficients."""
Expand Down Expand Up @@ -368,7 +368,7 @@ def test_multi_output_debiased_lasso(self):
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]))
np.testing.assert_allclose(weighted_debiased_coefs[i], expanded_debiased_coefs[i], atol=5e-2)

def _check_debiased_CI(self,
X, y, sample_weight, expected_coefs,
Expand Down Expand Up @@ -421,11 +421,11 @@ def _check_debiased_coefs(self, X, y, sample_weight, expected_coefs, expected_in
# Check coeffcients and intercept are the same within tolerance
if np.ndim(y) > 1:
for i in range(y.shape[1]):
self.assertTrue(np.allclose(debiased_lasso.coef_[i], expected_coefs[i], atol=5e-2))
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:
self.assertTrue(np.allclose(debiased_lasso.coef_, expected_coefs, atol=5e-2))
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_
Expand All @@ -441,11 +441,11 @@ def _compare_with_lasso(self, lasso_X, lasso_y, wlasso_X, wlasso_y, sample_weigh
# Check results are similar with tolerance 1e-6
if np.ndim(lasso_y) > 1:
for i in range(lasso_y.shape[1]):
self.assertTrue(np.allclose(lasso.coef_[i], wlasso.coef_[i]))
np.testing.assert_allclose(lasso.coef_[i], wlasso.coef_[i])
if lasso.get_params()["fit_intercept"]:
self.assertAlmostEqual(lasso.intercept_[i], wlasso.intercept_[i])
else:
self.assertTrue(np.allclose(lasso.coef_, wlasso.coef_))
np.testing.assert_allclose(lasso.coef_, wlasso.coef_)
self.assertAlmostEqual(lasso.intercept_, wlasso.intercept_)

def _compare_with_lasso_cv(self, lasso_X, lasso_y, wlasso_X, wlasso_y,
Expand All @@ -466,11 +466,11 @@ def _compare_with_lasso_cv(self, lasso_X, lasso_y, wlasso_X, wlasso_y,
# Check that the coefficients are similar
if np.ndim(lasso_y) > 1:
for i in range(lasso_y.shape[1]):
self.assertTrue(np.allclose(lassoCV.coef_[i], wlassoCV.coef_[i], atol=tol))
np.testing.assert_allclose(lassoCV.coef_[i], wlassoCV.coef_[i], atol=tol)
if lassoCV.get_params()["fit_intercept"]:
self.assertAlmostEqual(lassoCV.intercept_[i], wlassoCV.intercept_[i])
else:
self.assertTrue(np.allclose(lassoCV.coef_, wlassoCV.coef_, atol=tol))
np.testing.assert_allclose(lassoCV.coef_, wlassoCV.coef_, atol=tol)
self.assertAlmostEqual(lassoCV.intercept_, wlassoCV.intercept_)

def _map_splitter(self, weighted_splits, n_expanded, index_mapper):
Expand Down