Repository navigation
BUG: Fix MLEModel.score_obs with transformed=False - #11056
Merged
bashtage merged 4 commits intoNov 25, 2026
Merged
Conversation
This was referenced Nov 23, 2026
Member
Author
|
Merge order. These open PRs build on this one (their branches contain its commits), so please merge this PR first:
Indented PRs build on the PR above them, so merge them after it. Once this PR is merged, their diffs shrink to their own changes. Each of them also says "Stacked on #..." at the top of its description. Merge-order note generated with Claude Code (updated 2026-11-25). |
bashtage
added a commit
to bashtage/statsmodels
that referenced
this pull request
Nov 24, 2026
…arams Two problems of `MLEModel.score_obs` with parameters fixed by `fix_params` (and `includes_fixed=False`, the default): - It had a column for every parameter, as with `includes_fixed=True`, although it is given the free parameters only and `score` has one entry for each free parameter. `opg_information_matrix` and `hessian(method="opg")` inherited it: for SARIMAX(infl, order=(1, 0, 0), trend="c") with `ar.L1` fixed, `score_obs` had 3 columns and the OPG matrix was 3 x 3, while `score` and `hessian()` have 2 entries. - With `transformed=False`, the fixed values, which are constrained, were transformed as if they were unconstrained, so that the scores were evaluated at other parameters. In the example above, with `ar.L1` fixed at 0.5, the column sums were [239.95, -1097.78, 941.33], while `score` gives [49.71, 84.47]. This happened with the chain rule of statsmodels#11056 and also with finite differences (`approx_complex_step=False`), and `opg_information_matrix(transformed=False)` inherited it. Changes - With fixed parameters and `includes_fixed=False`, the score is with respect to the free parameters: the complex-step and "harvey" paths select their columns, the finite differences differentiate `loglikeobs` with respect to the free parameters as given, and the chain rule of `transformed=False` uses the Jacobian of `handle_params` with respect to the free unconstrained parameters, which sets the fixed values after the transformation (as `score` and `hessian` do). - The docstring says so, with a `versionchanged` note. Not changed - Models without fixed parameters, and `includes_fixed=True`, which still treats all of the given parameters alike (they are all transformed with `transformed=False`, as in `score`). - The covariance matrices of results, which call these methods with `includes_fixed=True` and select the free parameters themselves. Tests - For an ARMA(2, 1) with the MA coefficient fixed, with the default complex step, finite differences and the "harvey" method: the scores equal the free columns of those of the model without fixed parameters at the same values, they sum to `score`, and the OPG matrix and `hessian(method="opg")` equal the free block of that model's OPG matrix. With `transformed=False`, the scores equal central differences of `loglikeobs` with respect to the unconstrained free parameters (the definition), they sum to `score`, and the OPG matrix is their outer product. All 6 fail on the base.
bashtage
force-pushed
the
wip-mlemodel-score-obs-options
branch
from
November 24, 2026 16:16
2b79f39 to
4bfac12
Compare
bashtage
added a commit
to bashtage/statsmodels
that referenced
this pull request
Nov 24, 2026
…arams Two problems of `MLEModel.score_obs` with parameters fixed by `fix_params` (and `includes_fixed=False`, the default): - It had a column for every parameter, as with `includes_fixed=True`, although it is given the free parameters only and `score` has one entry for each free parameter. `opg_information_matrix` and `hessian(method="opg")` inherited it: for SARIMAX(infl, order=(1, 0, 0), trend="c") with `ar.L1` fixed, `score_obs` had 3 columns and the OPG matrix was 3 x 3, while `score` and `hessian()` have 2 entries. - With `transformed=False`, the fixed values, which are constrained, were transformed as if they were unconstrained, so that the scores were evaluated at other parameters. In the example above, with `ar.L1` fixed at 0.5, the column sums were [239.95, -1097.78, 941.33], while `score` gives [49.71, 84.47]. This happened with the chain rule of statsmodels#11056 and also with finite differences (`approx_complex_step=False`), and `opg_information_matrix(transformed=False)` inherited it. Changes - With fixed parameters and `includes_fixed=False`, the score is with respect to the free parameters: the complex-step and "harvey" paths select their columns, the finite differences differentiate `loglikeobs` with respect to the free parameters as given, and the chain rule of `transformed=False` uses the Jacobian of `handle_params` with respect to the free unconstrained parameters, which sets the fixed values after the transformation (as `score` and `hessian` do). - The docstring says so, with a `versionchanged` note. Not changed - Models without fixed parameters, and `includes_fixed=True`, which still treats all of the given parameters alike (they are all transformed with `transformed=False`, as in `score`). - The covariance matrices of results, which call these methods with `includes_fixed=True` and select the free parameters themselves. Tests - For an ARMA(2, 1) with the MA coefficient fixed, with the default complex step, finite differences and the "harvey" method: the scores equal the free columns of those of the model without fixed parameters at the same values, they sum to `score`, and the OPG matrix and `hessian(method="opg")` equal the free block of that model's OPG matrix. With `transformed=False`, the scores equal central differences of `loglikeobs` with respect to the unconstrained free parameters (the definition), they sum to `score`, and the OPG matrix is their outer product. All 6 fail on the base.
MLEModel.score_obs with transformed=False gave wrong scores in two cases. - The "harvey" method dropped `transformed` and treated the untransformed parameters 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]. - The check that rejects complex-step derivatives with transformed=False 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 - With transformed=False, the "harvey" method and complex-step 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. - With the default complex step, the result for models whose transformation is complex differentiable (for example SARIMAX) is that of 0.15.0 to about 1e-10 relative. - The docstring says so, with a versionchanged note. Not changed - Finite differences with the "approx" method still differentiate loglikeobs through the transformation, so their results are unchanged. - An explicit approx_complex_step=True with transformed=False still raises (as statsmodels#11029 tests). - With fixed parameters and includes_fixed=False, all of the paths of 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 - For SARIMAX(2, 0, 1), a VAR(2), a dynamic factor model and an AR(1), 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. - The default and harvey results are the chain rule applied to the results for the transformed parameters, to 1e-8 (for SARIMAX, this is main's default result). - The "approx" finite differences are unchanged, and with includes_fixed=True and fixed parameters the scores are those of the model without fixed parameters.
The CodeQL check failed with 4 "wrong number of arguments" errors at numdiff.py lines 172, 180 and 187, the calls f(x, *args, **kwargs) in approx_fprime. The alerts exist on main; they counted as new because their related locations, the one-argument functions passed as f, gained two lines that this pull request adds: the local constrain function in MLEModel.score_obs and a lambda in test_score_obs_untransformed_finite_difference. Pass self.handle_params and mod.loglikeobs with the keyword arguments through approx_fprime's kwargs instead. They are called with the same arguments, so the results are unchanged.
bashtage
added a commit
to bashtage/statsmodels
that referenced
this pull request
Nov 25, 2026
…arams Two problems of `MLEModel.score_obs` with parameters fixed by `fix_params` (and `includes_fixed=False`, the default): - It had a column for every parameter, as with `includes_fixed=True`, although it is given the free parameters only and `score` has one entry for each free parameter. `opg_information_matrix` and `hessian(method="opg")` inherited it: for SARIMAX(infl, order=(1, 0, 0), trend="c") with `ar.L1` fixed, `score_obs` had 3 columns and the OPG matrix was 3 x 3, while `score` and `hessian()` have 2 entries. - With `transformed=False`, the fixed values, which are constrained, were transformed as if they were unconstrained, so that the scores were evaluated at other parameters. In the example above, with `ar.L1` fixed at 0.5, the column sums were [239.95, -1097.78, 941.33], while `score` gives [49.71, 84.47]. This happened with the chain rule of statsmodels#11056 and also with finite differences (`approx_complex_step=False`), and `opg_information_matrix(transformed=False)` inherited it. Changes - With fixed parameters and `includes_fixed=False`, the score is with respect to the free parameters: the complex-step and "harvey" paths select their columns, the finite differences differentiate `loglikeobs` with respect to the free parameters as given, and the chain rule of `transformed=False` uses the Jacobian of `handle_params` with respect to the free unconstrained parameters, which sets the fixed values after the transformation (as `score` and `hessian` do). - The docstring says so, with a `versionchanged` note. Not changed - Models without fixed parameters, and `includes_fixed=True`, which still treats all of the given parameters alike (they are all transformed with `transformed=False`, as in `score`). - The covariance matrices of results, which call these methods with `includes_fixed=True` and select the free parameters themselves. Tests - For an ARMA(2, 1) with the MA coefficient fixed, with the default complex step, finite differences and the "harvey" method: the scores equal the free columns of those of the model without fixed parameters at the same values, they sum to `score`, and the OPG matrix and `hessian(method="opg")` equal the free block of that model's OPG matrix. With `transformed=False`, the scores equal central differences of `loglikeobs` with respect to the unconstrained free parameters (the definition), they sum to `score`, and the OPG matrix is their outer product. All 6 fail on the base.
bashtage
force-pushed
the
wip-mlemodel-score-obs-options
branch
from
November 25, 2026 12:12
4bfac12 to
ae665fc
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.
#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. The Harveyapprox_centeredPR is stacked on this one.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
For SARIMAX(2, 0, 1), a VAR(2), a dynamic factor model and an AR(1),
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.
The default and harvey results are the chain rule applied to the
results for the transformed parameters, to 1e-8 (for SARIMAX, this is
main's default result).
The "approx" finite differences are unchanged, and with
includes_fixed=True and fixed parameters the scores are those of the
model without fixed parameters.
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.
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