Skip to content

DOC: Follow up #10737 on the NegativeBinomialP Hessian, with tests - #10749

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-nbp-hessian-followup
Nov 16, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-nbp-hessian-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10737 (the cross derivative of the NegativeBinomialP Hessian for p other 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.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    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: hessian and hessian_factor have a .. versionchanged:: 0.15.1 note. hessian_factor documented a 2-D hessian that it does not return; its Returns section now lists hfbb, hfba and hfaa (one value per observation) and how each enters the Hessian ((exog.T * hfbb) @ exog, hfba @ exog, hfaa.sum()).
  • DOC: The class docstring said p : int; it is float, optional, 'other values of p are allowed', with a Notes section for the variance mu + alpha * mu**p. Checked in the code: __init__ has no range check, hessian and score agree 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 of loglikeobs has mean mu and variance mu + alpha * mu**p for p = 1, 1.5, 2, 2.5.
  • TST: test_negbinp_hessian_numdiff asserted only the cross block of the factors. It now also asserts the hfbb block, (hf0 * exog.T) @ exog against hess[:-1, :-1], and hf2.sum() against hess[-1, -1] (rtol=1e-10). The atol of the comparison with approx_hess now has its reason in a comment: the error of approx_hess is at most 4.6e-7 of the largest entry, the smallest entries are 4e-4 to 1e-2 of it, and atol is 1e-5 times the largest entry, a margin of about 20.
  • TST: test_negbinp_bse_p15 is parametrized with p=1.5 and p=2.5 (the name is kept) and asserts convergence, finite bse and agreement with the numerical bse (rtol=1e-5). p=2.5 is the case with nan standard errors on the old code.

Mutation checks (the tree mutated, each restored afterwards): hfbb with (1 + a4)**2 replaced by 1 fails 7 of the 8 Hessian cases (the p=2 case passes because a4 = 0 there); dropping the a1 * a3 / a2**2 term of hfaa fails all 8; the old cross term put back into both hessian and hessian_factor fails 6 Hessian cases and both bse cases (only p=1 and p=2 pass).

Not changed

Tests

  • statsmodels/discrete/tests/test_discrete.py: 843 passed, 11 skipped, 42 xfailed, and test_count_model.py -k NegativeBinomial: 20 passed, on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1); rtol 1e-10 and 1e-5 hold unchanged on 3.10.
  • ruff check statsmodels/discrete and git diff --check pass.

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.

  • No AI tools were used to develop this pull request.
  • AI tools were 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

bashtage and others added 2 commits November 15, 2026 18:45
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]>
@bashtage
bashtage marked this pull request as ready for review November 15, 2026 22:54
@bashtage
bashtage merged commit cfaf125 into statsmodels:main Nov 16, 2026
14 checks passed
@bashtage
bashtage deleted the wip-nbp-hessian-followup branch November 20, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant