Skip to content

Commit ffd1307

Browse files
danielmeintclaude
andauthored
Fix NegativeBinomialDistribution.theta type check to accept np.number (#977)
The theta setter only accepted (int, float), but internally stores values as np.float32. This caused dist.theta = dist.theta to raise a TypeError. Add np.number to the type check, matching the fix already applied to TweedieDistribution.power. Fixes #631 Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 934da83 commit ffd1307

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Changelog
1313
**Bug fix:**
1414

1515
- Fixed ``deviance_path_`` in :class:`~glum.GeneralizedLinearRegressorCV` being scaled down by a factor of ``n_folds`` because test fold weights were not normalized to sum to 1.
16+
- Fixed :class:`~glum.NegativeBinomialDistribution` ``theta`` setter rejecting ``np.number`` types, causing ``dist.theta = dist.theta`` to raise a ``TypeError``.
1617

1718
**Other changes:**
1819

src/glum/_distribution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def theta(self):
13901390

13911391
@theta.setter
13921392
def theta(self, theta):
1393-
if not isinstance(theta, (int, float)):
1393+
if not isinstance(theta, (int, float, np.number)):
13941394
raise TypeError(f"Theta must be numeric; got {theta}.")
13951395
if not theta > 0:
13961396
raise ValueError(f"Theta must be strictly positive; got was {theta}.")

tests/glm/test_distribution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ def test_negative_binomial_distribution_alpha():
9898
dist.theta = 1j
9999

100100

101+
def test_distribution_parameter_reassignment():
102+
"""Test that distribution parameters can be reassigned to themselves."""
103+
dist = NegativeBinomialDistribution(theta=1.5)
104+
dist.theta = dist.theta
105+
assert dist.theta == 1.5
106+
107+
dist2 = TweedieDistribution(power=1.5)
108+
dist2.power = dist2.power
109+
assert dist2.power == 1.5
110+
111+
101112
def test_negative_binomial_distribution_parsing():
102113
dist = get_family("negative.binomial")
103114

0 commit comments

Comments
 (0)