Repository navigation
BUG: Honour approx_centered in MLEModel score and score_obs with harvey - #11064
Merged
bashtage merged 4 commits intoNov 25, 2026
Merged
Conversation
This was referenced Nov 23, 2026
bashtage
force-pushed
the
wip-mlemodel-harvey-centered
branch
2 times, most recently
from
November 24, 2026 16:17
355d3a3 to
8abf382
Compare
The docstrings of MLEModel.score and score_obs say that approx_centered selects centered finite differences when approx_complex_step is False. With method="harvey", neither passed it on, so the Harvey scores always used forward differences: for an AR(1) on the macrodata inflation series, score(params, method="harvey", approx_complex_step=False, approx_centered=True) equals the forward-difference score exactly. For SARIMAX(1, 0, 0)x(2, 0, 0, 4) the error relative to the largest element is 2.8e-6 (score) and 2.5e-5 (score_obs) instead of 4e-9 and 3e-8. observed_information_matrix, which uses the same derivatives, already honoured it. Changes - score and score_obs pass approx_centered to the Harvey method, so with approx_complex_step=False and approx_centered=True its derivatives are centered differences. Results of such calls change. - Both docstrings have a versionchanged note. Not changed - Complex-step derivatives (the default), forward differences, and the "approx" method. - With transformed=False, score still multiplies by the Jacobian of the transformation by forward differences, as for the "approx" method. Tests - The Harvey score, and its scores per observation with transformed True and False, with centered differences match centered differences of loglike and loglikeobs to 3e-7 of the largest element for a seasonal SARIMAX model. All three fail on main and on the parent branch. Stacked on the score_obs fix (wip-mlemodel-score-obs-options), which changes the same lines of score_obs.
The CodeQL check failed with "Local variable 'params' may be used before it is initialized" at the first argument of the _score_harvey call in MLEModel.score. The same false positive exists on main for that line; it counted as new because the call was split over several lines to add approx_centered. Put approx_centered into kwargs, as includes_fixed is, so that the call is the same text as on main. _score_harvey passes its kwargs on to _score_obs_harvey, so the results are unchanged.
bashtage
force-pushed
the
wip-mlemodel-harvey-centered
branch
from
November 25, 2026 12:12
8abf382 to
877b616
Compare
4 of 5 tasks
bashtage
marked this pull request as ready for review
November 25, 2026 16:27
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.
Stacked on #11056, merge that first (this PR adds two commits). The stack was rebuilt on 2026-11-25 so that it merges without conflicts in this order; notes about conflicts with PRs of the same stack no longer apply. #11029 (the C10 coverage chunk) appends tests to the same test file, so whichever merges second needs a rebase (keep both test blocks). With #11029 there is also a conflict in the Notes of the
score_obsdocstring: keep #11029's first paragraph ("This is a numerical approximation. By default, ..."), then this PR's paragraphs and itsversionchanged. The other C10F PRs and #11043 also append totest_mlemodel.py: keep every appended block.BUG: Fix MLEModel.score_obs with transformed=False
MLEModel.score_obs with transformed=False gave wrong scores in two cases.
transformedand treated the untransformedparameters as transformed. For SARIMAX(1, 0, 0) with a trend on the
macrodata inflation series, at the untransformed values of
[1, 0.5, 4], the scores summed to [532.1, 3413.7, 1202.9]; the score
is [49.7, -178.3, 84.5].
ran before the default of approx_complex_step was resolved, so by
default the complex step was applied through transform_params. That is
only right for transformations that are complex differentiable. For a
VAR(1) with a constant on GDP and consumption growth, 5 of the 9
summed scores were wrong (for example -70.12 instead of -13.27).
Changes
differentiation compute the scores with respect to the transformed
parameters, as with transformed=True, and multiply them by the Jacobian
of the transformation (centered differences), as score and hessian do.
transformation is complex differentiable (for example SARIMAX) is that
of 0.15.0 to about 1e-10 relative.
Not changed
loglikeobs through the transformation, so their results are unchanged.
raises (as TST: Cover the remaining MLEModel and MLEResults paths #11029 tests).
score_obs with transformed=False evaluate the likelihood with the fixed
values transformed as if they were unconstrained, and return a column
for each parameter. That is a separate bug and is left as it is; the
new paths agree with the finite differences there.
Tests
the "approx" and "harvey" scores with the default and with finite
differences match centered differences of loglikeobs with respect to
the untransformed parameters, and sum to score. The harvey cases for all
models and the default for the VAR(2) and the dynamic factor model fail
on main.
results for the transformed parameters, to 1e-8 (for SARIMAX, this is
main's default result).
includes_fixed=True and fixed parameters the scores are those of the
model without fixed parameters.
BUG: Honour approx_centered in MLEModel score and score_obs with harvey
The docstrings of MLEModel.score and score_obs say that approx_centered
selects centered finite differences when approx_complex_step is False.
With method="harvey", neither passed it on, so the Harvey scores always
used forward differences: for an AR(1) on the macrodata inflation series,
score(params, method="harvey", approx_complex_step=False,
approx_centered=True) equals the forward-difference score exactly. For
SARIMAX(1, 0, 0)x(2, 0, 0, 4) the error relative to the largest element
is 2.8e-6 (score) and 2.5e-5 (score_obs) instead of 4e-9 and 3e-8.
observed_information_matrix, which uses the same derivatives, already
honoured it.
Changes
approx_complex_step=False and approx_centered=True its derivatives are
centered differences. Results of such calls change.
Not changed
"approx" method.
transformation by forward differences, as for the "approx" method.
Tests
and False, with centered differences match centered differences of
loglike and loglikeobs to 3e-7 of the largest element for a seasonal
SARIMAX model. All three fail on main and on the parent branch.
Stacked on the score_obs fix (wip-mlemodel-score-obs-options), which
changes the same lines of score_obs.
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.
Tool(s): Claude Code (Claude Opus 5.5).
Used for: finding the bug in a contract audit, writing the fix and its tests, and checking them (tests failing on main, mutation check).
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