Skip to content

REF: Remove unreachable code in mlemodel - #11063

Draft
bashtage wants to merge 1 commit into
statsmodels:mainfrom
bashtage:wip-mlemodel-unreachable
Draft

bashtage wants to merge 1 commit into
statsmodels:mainfrom
bashtage:wip-mlemodel-unreachable

Conversation

@bashtage

Copy link
Copy Markdown
Member

#11029 (the C10 coverage chunk) appends tests to the same test file, so whichever merges second needs a rebase (keep both test blocks). The other C10F PRs and #11043 also append to test_mlemodel.py: keep every appended block.

A coverage audit of statespace/mlemodel.py found private code that no
public path can reach. It is removed; results do not change.

Changes

  • _forecasts_error_partial_derivatives: its two callers
    (observed_information_matrix and the Harvey scores) always pass the
    filter output at the parameters and a resolved approx_complex_step, and
    check that complex-step derivatives are only used with transformed
    parameters (the Harvey scores always pass transformed parameters). The
    default for approx_complex_step, the check, and the filtering when res
    is None are removed; res is a required keyword argument.
  • MLEModel.simulate, MLEModel.impulse_responses and MLEResults.simulate:
    the location of the anchor from _get_index_loc is never negative (a
    negative integer is converted to a location from the end, and dates
    before the sample raise a KeyError), so the "if iloc < 0" branches are
    removed.
  • _cov_params_robust_oim and _cov_params_robust_approx: they compute the
    OPG matrix first, which sets the rank of the covariance matrix, so their
    own "if self._rank is None" updates never ran and are removed.
  • _get_robustcov_results: the statements after "raise
    NotImplementedError" are removed.

Not changed

  • The _state_names hook, which mirrors _param_names.
  • The rank of the robust covariance matrices, which stays the rank of the
    OPG matrix.

Tests

  • A negative integer anchor in MLEModel.simulate and MLEResults.simulate
    gives the same simulations as the equivalent location, with and without
    a date index, for a model with exog (so that the simulations depend on
    the anchor). The negative anchor of impulse_responses is already tested
    in test_impulse_responses.py.

  • The removed lines were not covered by the statespace tests on main; no
    line that was covered before is missing now.

  • 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 (Claude Opus 5.5).
    Used for: finding the unreachable code in a coverage audit, removing it, and checking that the results do not change.
    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

bashtage commented Nov 24, 2026 •

Copy link
Copy Markdown
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
bashtage force-pushed the wip-mlemodel-unreachable branch from ba0cdfd to cedd370 Compare November 24, 2026 16:16
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 24, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from cedd370 to e944939 Compare November 25, 2026 12:12
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from e944939 to 55853d3 Compare November 25, 2026 18:39
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from 55853d3 to 0f57fa3 Compare November 26, 2026 11:46
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 26, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from 0f57fa3 to 5ca33b4 Compare November 27, 2026 00:31
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 27, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from 5ca33b4 to 4327bb4 Compare November 27, 2026 23:06
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 27, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
A coverage audit of statespace/mlemodel.py found private code that no
public path can reach. It is removed; results do not change.

Changes
- _forecasts_error_partial_derivatives: its two callers
  (observed_information_matrix and the Harvey scores) always pass the
  filter output at the parameters and a resolved approx_complex_step, and
  check that complex-step derivatives are only used with transformed
  parameters (the Harvey scores always pass transformed parameters). The
  default for approx_complex_step, the check, and the filtering when `res`
  is None are removed; `res` is a required keyword argument.
- MLEModel.simulate, MLEModel.impulse_responses and MLEResults.simulate:
  the location of the anchor from _get_index_loc is never negative (a
  negative integer is converted to a location from the end, and dates
  before the sample raise a KeyError), so the "if iloc < 0" branches are
  removed.
- _cov_params_robust_oim and _cov_params_robust_approx: they compute the
  OPG matrix first, which sets the rank of the covariance matrix, so their
  own "if self._rank is None" updates never ran and are removed.
- _get_robustcov_results: the statements after "raise
  NotImplementedError" are removed.

Not changed
- The _state_names hook, which mirrors _param_names.
- The rank of the robust covariance matrices, which stays the rank of the
  OPG matrix.

Tests
- A negative integer anchor in MLEModel.simulate and MLEResults.simulate
  gives the same simulations as the equivalent location, with and without
  a date index, for a model with exog (so that the simulations depend on
  the anchor). The negative anchor of impulse_responses is already tested
  in test_impulse_responses.py.
- The removed lines were not covered by the statespace tests on main; no
  line that was covered before is missing now.
@bashtage
bashtage force-pushed the wip-mlemodel-unreachable branch from 4327bb4 to 7e8b168 Compare November 28, 2026 21:36
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 28, 2026
The summary of state space results (MLEResults) warns that the covariance
matrix is singular when the results' _rank is below the number of free
parameters. With cov_type="robust_approx" (and "robust"/"robust_oim"),
_rank was the rank of the outer product of gradients (OPG) matrix: the
robust matrix, inv(H inv(OPG) H), computes the OPG matrix first, which
sets the rank, so the robust matrix's own rank update could never run
(statsmodels#11063 removes it as unreachable). The robust matrix has the rank of H,
which can be lower. For white noise with a regressor, at the OLS
coefficient and twice the mean squared residual as variance, the Hessian
of the loglikelihood is diag(-sum(x**2) / s2, 0) (rank 1) and the OPG
matrix has rank 2; the robust_approx matrix has rank 1, but the summary
did not warn. This is the same change as statsmodels#11068 for the Markov switching
models.

Changes
- _cov_params_robust_oim and _cov_params_robust_approx set _rank to the
  rank of the robust matrix when no rank is set yet, that is when it is
  the covariance matrix of the results, as the approx, oim and opg
  matrices do for theirs. Results with another cov_type keep the rank of
  their own matrix when a robust matrix is computed later.
- versionchanged in the cov_params_robust_oim and cov_params_robust_approx
  docstrings.

The summary of robust_approx results can now warn that the covariance
matrix is singular (or stop warning, if the OPG rank was lower). For
"robust" (Harvey's OIM matrix as H), the two ranks are equal in exact
arithmetic: a direction in which the OIM matrix is singular has zero
derivatives of the forecast errors and their variances, so every score is
zero in it and the OPG matrix is singular there too; the numerical ranks
can only differ through rounding.

Not changed
- The covariance matrices and standard errors.
- The rank and warning of results with cov_type "approx", "oim", "opg",
  "none" and "custom".

Tests
- test_summary_singular_warning_follows_robust_cov_type: at the singular
  point above, robust_approx and approx results warn and the others do
  not, and the rank of cov_params() is the closed-form rank of each
  matrix (robust_approx fails on statsmodels#11063's branch and on main); after the
  other robust matrices are computed as well (rank 2 and 1), each result
  keeps the rank of its own matrix. At the MLE, all six cov types have
  rank 2 and none warns.
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