Skip to content

BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 - #10737

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/nbp-hessian-general-p
Nov 15, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/nbp-hessian-general-p

Conversation

@bhaskargurram-ai

@bhaskargurram-ai bhaskargurram-ai commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

The cross derivative of the regression parameters and alpha in NegativeBinomialP.hessian and hessian_factor is wrong when p is not 1 or 2. With Q = 2 - p (p in the code), a1 = mu**Q / alpha and a4 = Q * a1 / mu, the derivative of a1 / a2 contributes a4 / a2, but the code had Q * a4 / a2. The two agree only for Q = 0 or 1, i.e. p=2 and p=1, the cases in the tests. The other blocks of the Hessian are correct.

With 3000 simulated NB2 counts, the relative error of the Hessian at the MLE (against the complex-step derivative of score) was 0.16, 0.31, 0.25, 1.8 and 6.1 for p = 1.2, 1.5, 1.8, 2.5 and 3. With p=1.5 the bse of the constant and of alpha were 19% too large (p=1.8: 9%). With p=2.5 and p=3 the Hessian was not invertible, so bse was nan with a HessianInversionWarning.

The fix changes this one term in hessian and in hessian_factor. Afterwards the Hessian agrees with the complex-step derivative of score to 5e-15 and with approx_hess3 of loglike to 2.4e-7 for all these p, and bse agrees with the numerical bse.

Tests: test_negbinp_hessian_numdiff compares hessian with approx_hess of loglike (with an offset) for p in 0.5, 1, 1.2, 1.5, 1.8, 2, 2.5, 3, and checks the cross term of hessian_factor against hessian. test_negbinp_bse_p15 compares bse of a newton fit with p=1.5 with the numerical bse. All fail on main except p=1 and p=2.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

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.
    Used for: drafting the tests and this PR description.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

The derivative of the alpha score with respect to the regression
parameters in NegativeBinomialP.hessian and hessian_factor had the term
p * a4 / a2 where the derivative of a1 / a2 gives a4 / a2, with
a4 = (2 - parameterization) * a1 / mu. Both agree only if
2 - parameterization is 0 or 1, i.e. for p = 2 and p = 1.

With p = 1.5 the standard errors of the constant and of alpha were about
19% too large, and for p larger than 2 the hessian was not invertible.
The hessian now agrees with the numerical hessian of loglike for all p.
@bhaskargurram-ai
bhaskargurram-ai marked this pull request as draft November 11, 2026 15:27
@bhaskargurram-ai
bhaskargurram-ai marked this pull request as ready for review November 11, 2026 15:39
@bashtage
bashtage merged commit b12f3cd into statsmodels:main Nov 15, 2026
12 of 15 checks passed
@bashtage

Copy link
Copy Markdown
Member

Thanks.

bashtage added a commit that referenced this pull request Nov 16, 2026
The release note of #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]>
bashtage added a commit that referenced this pull request Nov 16, 2026
The test added in #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 added a commit that referenced this pull request Nov 16, 2026
DOC: Follow up #10737 on the NegativeBinomialP Hessian, with tests
bashtage added a commit that referenced this pull request Nov 16, 2026
The bullet for the truncated count score of #10736 was in the middle of
the list of the discrete models, without the PR role, and did not say
what users see. Move it to the end of the list, add the PR number, and
state that params, bse and llf of the fitted models change for every p
other than p=1 of the generalized Poisson and p=2 of the negative
binomial model, with the example of the default generalized Poisson
(alpha 0.099 -> 0.219, llf -3582.06 -> -3554.15 for 3000 simulated
counts, measured on main before the PR and with the PR).

The bullet stays in Bug Fixes like the sibling bullets of #10723 and
#10737 for the Hessians of the discrete models.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage added a commit that referenced this pull request Nov 16, 2026
The release note of #10744 named only cov_type="cluster". Every covariance
type that sets df_resid_inference changes: "cluster" (also two-way
clustering), "hac-panel" and "hac-groupsum" for all models and "cluster-crv3"
and "cluster-jk" for linear models, unless df_correction=False. With
use_t=False nothing changes. Checked with seeded OLS, GLM, Logit and Poisson
fits with 10 clusters on main before and after #10744.

The entry stays in Bug Fixes with the neighbouring #10742, #10737 and #10736.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.

2 participants