Skip to content

Commit

Permalink
Fix bug in GLR(warm_start=True).fit(X, y) if X has a const. column (
Browse files Browse the repository at this point in the history
#647)

Fix a bug in GeneralizedLinearRegression when fit on a const. column with warm_start=True.
  • Loading branch information
mlondschien authored Jun 1, 2023
1 parent 3173354 commit 6c657ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Changelog
**Bug fix**

- Fix the ``glm_benchmarks_analyze`` command line tool. See `here <https://github.com/Quantco/glum/issues/642>`_.
- Fixed a bug in :class:`~glum.GeneralizedLinearRegressor` when fit on a data set with a constant column and ``warm_start=True``. See `here <https://github.com/Quantco/glum/issues/645>`_.

**Other changes:**

Expand Down
4 changes: 3 additions & 1 deletion src/glum/_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ def _standardize_warm_start(
coef[0] += np.squeeze(col_means).dot(coef[1:])
else:
coef[1:] *= col_stds
coef[0] += np.squeeze(col_means / col_stds).dot(coef[1:])
coef[0] += np.squeeze(col_means * _one_over_var_inf_to_val(col_stds, 1)).dot(
coef[1:]
)


def get_family(
Expand Down
12 changes: 12 additions & 0 deletions tests/glm/test_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ def test_glm_warm_start_argument(estimator, warm_start):
glm.fit(X, y)


# https://github.com/Quantco/glum/issues/645
@pytest.mark.parametrize(
"estimator", [GeneralizedLinearRegressor, GeneralizedLinearRegressorCV]
)
def test_glm_warm_start_with_constant_column(estimator):
X, y = make_regression()
X[:, 0] = 0
glm = estimator(warm_start=True)
glm.fit(X, y)
glm.fit(X, y)


@pytest.mark.parametrize(
"estimator", [GeneralizedLinearRegressor, GeneralizedLinearRegressorCV]
)
Expand Down

0 comments on commit 6c657ef

Please sign in to comment.