Skip to content

Commit

Permalink
Fix bug in Wald test with term names without intercept (#797)
Browse files Browse the repository at this point in the history
* fix bug in term names test w/o intercept

* add changelog
  • Loading branch information
MatthiasSchmidtblaicherQC authored May 23, 2024
1 parent 5d47555 commit 51ecde4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ Changelog
=========


3.0.2 - 2024-XX-XX
------------------

**Bug fix**

- Fixed :meth:`~glum.GeneralizedLinearRegressor.wald_test` when using ``terms`` and no intercept.

3.0.1 - 2024-05-23
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/glum/_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ def _wald_test_term_names(
names = np.array(["intercept"] + list(self.term_names_))
beta = np.concatenate([[self.intercept_], self.coef_])
else:
names = np.array(self.feature_names_)
names = np.array(self.term_names_)
beta = self.coef_

R_list = []
Expand Down
19 changes: 15 additions & 4 deletions tests/glm/test_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,14 +2470,17 @@ def test_wald_test_term_names(regression_data, names, R, r, r_feat):


@pytest.mark.parametrize(
"names, R, r, r_feat",
"names, R, r, r_feat, fit_intercept",
[
pytest.param(["col_1"], np.array([[0, 1] + 5 * [0]]), None, None, id="single"),
pytest.param(
["col_1"], np.array([[0, 1] + 5 * [0]]), None, None, True, id="single"
),
pytest.param(
["col_1", "col_2"],
np.array([[0, 1, 0] + 4 * [0], [0, 0, 1] + 4 * [0]]),
None,
None,
True,
id="multiple",
),
pytest.param(
Expand All @@ -2490,6 +2493,7 @@ def test_wald_test_term_names(regression_data, names, R, r, r_feat):
),
None,
None,
True,
id="multifeature",
),
pytest.param(
Expand All @@ -2502,24 +2506,31 @@ def test_wald_test_term_names(regression_data, names, R, r, r_feat):
),
[1],
[1] * 4,
True,
id="rhs_not_zero",
),
pytest.param(
["intercept", "col_1"],
np.array([[1, 0] + 5 * [0], [0, 1] + 5 * [0]]),
[1, 2],
[1, 2],
True,
id="intercept",
),
pytest.param(
["col_1"], np.array([[1] + 5 * [0]]), None, None, False, id="no_intercept"
),
],
)
def test_wald_test_term_names_public(regression_data, names, R, r, r_feat):
def test_wald_test_term_names_public(
regression_data, names, R, r, r_feat, fit_intercept
):
X, y = regression_data
X_df = pd.DataFrame(X, columns=[f"col_{i}" for i in range(X.shape[1])])
X_df = X_df[["col_1", "col_2"]].assign(term_3=pd.cut(X_df["col_3"], bins=5))

mdl = GeneralizedLinearRegressor(
family="gaussian", fit_intercept=True, drop_first=True
family="gaussian", fit_intercept=fit_intercept, drop_first=True
).fit(X=X_df, y=y, store_covariance_matrix=True)

term_names_results = mdl.wald_test(terms=names, r=r)
Expand Down

0 comments on commit 51ecde4

Please sign in to comment.