Repository navigation
DOC: Follow up #10737 on the NegativeBinomialP Hessian, with tests - #10749
Merged
Merged
Conversation
The release note of statsmodels#10737 had no :pr: role, sat in the middle of its list and quoted "about 19% too large", which is a single simulated sample. Move it to the end of the list and say what users see: bse, cov_params, conf_int, p-values and the HC0 covariance change for p other than 1 and 2, fit(method="newton") converges, score_test with a tuple exog_extra changes, p=1 and p=2 are unchanged. The numbers are from 20 simulated samples of 3000 counts (bse of the constant and alpha 1.2 to 2.2 times the numerical bse at p=1.5, 1.1 to 1.3 at p=1.8, within 8% at p=1.2, nan at p=2.5 and 3). Add versionchanged 0.15.1 to hessian and hessian_factor, document the three returned factors of hessian_factor (it returned a tuple, not a matrix), and document p as a float in the class docstring with the variance function mu + alpha * mu**p. NegativeBinomialP does not restrict p to a range. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The test added in statsmodels#10737 compared only the cross block of hessian_factor with the hessian. Mutating the other two factors (hfbb: 1 + a4 -> 1, hfaa: dropping the a1 * a3 / a2**2 term) left all 9 tests green. Compare the block of the regression parameters and the alpha element too, and explain the atol of the numerical Hessian comparison (error of approx_hess up to 5e-7 times the largest entry, smallest entries 4e-4 to 1e-2 of it). Parametrize test_negbinp_bse_p15 with p=2.5, where bse is nan with the old cross derivative, and assert that the newton fit converges and that bse is finite. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up to #10737 (the cross derivative of the
NegativeBinomialPHessian forpother than 1 and 2). Its review left the points below. The code of the Hessian and its factors is unchanged; the docstrings and the tests change.NumPy's guide.
Changes
DOC:Release note: moved from the middle to the end of the 'Generalized linear models and discrete models' bug-fix list,:pr:10737`` added, and rewritten to say what a user sees instead of '19% from one sample'.bse, `cov_params`, `conf_int`, the p-values and the HC0 covariance change for `p` other than 1 and 2, and `bse` now agrees with the standard errors of a numerical Hessian. Measured on the code before BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 #10737 and now (20 simulated samples of 3000 NB2 counts, `bse` of a BFGS fit over the standard errors from `approx_hess`): the constant and `alpha` were too large by a factor of 1.2 to 2.2 for `p=1.5` (median 1.38) and 1.1 to 1.3 for `p=1.8`, within 8% for `p=1.2`, and `nan` for `p=2.5` and `p=3` in all 20 samples; the ratio is 1.0 for every `p` now. `fit(method="newton")` used the Hessian, so in one sample of 1000 counts it ran 100 iterations without convergence for `p=1.5`, `2.5` and `3`, had a `nan` log-likelihood for `p=1.8`, and converged after 71 iterations for `p=1.2`; it converges in 4 iterations for each of them now. `score_test` with a tuple `exog_extra` changes too (at `p=1.5` the statistic is 4.6186 before and 4.5708 now) and is unchanged for `p=1` and `p=2`. The Hessian and its factors differ between the two code versions at `p=1` and `p=2` by at most 3.6e-14. The note stays under 'Bug Fixes' next to BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723, although `AGENTS.md` puts changed results under 'Behavior changes'; move it if you prefer.DOC:hessianandhessian_factorhave a.. versionchanged:: 0.15.1note.hessian_factordocumented a 2-Dhessianthat it does not return; itsReturnssection now listshfbb,hfbaandhfaa(one value per observation) and how each enters the Hessian ((exog.T * hfbb) @ exog,hfba @ exog,hfaa.sum()).DOC:The class docstring saidp : int; it isfloat, optional, 'other values of p are allowed', with aNotessection for the variancemu + alpha * mu**p. Checked in the code:__init__has no range check,hessianandscoreagree with numerical derivatives for p = -1, 0, 0.5, 1, 4 (Hessian error at most 6e-7 of its largest entry, score relative error at most 3e-5), and the pmf ofloglikeobshas meanmuand variancemu + alpha * mu**pfor p = 1, 1.5, 2, 2.5.TST:test_negbinp_hessian_numdiffasserted only the cross block of the factors. It now also asserts thehfbbblock,(hf0 * exog.T) @ exogagainsthess[:-1, :-1], andhf2.sum()againsthess[-1, -1](rtol=1e-10). Theatolof the comparison withapprox_hessnow has its reason in a comment: the error ofapprox_hessis at most 4.6e-7 of the largest entry, the smallest entries are 4e-4 to 1e-2 of it, andatolis 1e-5 times the largest entry, a margin of about 20.TST:test_negbinp_bse_p15is parametrized withp=1.5andp=2.5(the name is kept) and asserts convergence, finitebseand agreement with the numericalbse(rtol=1e-5).p=2.5is the case withnanstandard errors on the old code.Mutation checks (the tree mutated, each restored afterwards):
hfbbwith(1 + a4)**2replaced by 1 fails 7 of the 8 Hessian cases (thep=2case passes becausea4 = 0there); dropping thea1 * a3 / a2**2term ofhfaafails all 8; the old cross term put back into bothhessianandhessian_factorfails 6 Hessian cases and bothbsecases (onlyp=1andp=2pass).Not changed
Tests
statsmodels/discrete/tests/test_discrete.py: 843 passed, 11 skipped, 42 xfailed, andtest_count_model.py -k NegativeBinomial: 20 passed, on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1);rtol1e-10 and 1e-5 hold unchanged on 3.10.ruff check statsmodels/discreteandgit diff --checkpass.AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tick exactly one of the following. If AI tools were used, replace the
<...>placeholders in the second option with the tool name(s) and how the tool's output was
used.
Tool(s): Claude Code (Claude Sonnet 5.5).
Used for: reviewing BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 #10737, and writing and checking the follow-up changes.
I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.
🤖 Generated with Claude Code