Repository navigation
Conversation
bhhh stepped along +H^{-1}g, an ascent direction for the minimized sum of
contributions. The tests passed because they paired the negative
loglikelihood with the positive score, so the two sign errors cancelled.
The new tests use consistent derivatives, verify the score against a
numerical derivative and cover numerical derivatives and maximize.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The line search computed the Hessian approximation from the Jacobian at the first trial point of each iteration, even if that point was rejected, and accepted a worse point once the step size fell to 0.01. With error_handling="continue", this meant using the rank-one Jacobian of the error penalty and accepting penalized points. The Hessian approximation is now computed only at accepted points, and the optimization stops if the line search finds no decrease. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
janosg
force-pushed
the
fix/bhhh-descent-direction
branch
from
October 19, 2026 17:28
2c53ef1 to
1770d9c
Compare
This branch has not been deployed
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.
Bug 1: ascent direction
bhhhminimizes the sum of contributionsbhhh_internalusedbhhhslowly walked uphill and ended at a point worse than the start.This affects every use of
bhhhthroughminimizeandmaximize, with numerical derivatives and with correct closed-form derivatives. The bug has been there sincebhhhwas added in #266 (March 2022).The fix flips the sign of the direction in all three places it is computed, and of the convergence criterion$g^\top H^{-1} g$ , which is now computed as $-g^\top d$ . I also corrected the docstring of
bhhh_internal, whose argument names no longer matched the signature.Bug 2: the line search uses rejected points
The old line search had two more problems:
Both matter with$H$ is rank one. The forced acceptance then moves
error_handling="continue". At a failed evaluation, the Jacobian is that of the error penalty, whose rows are all identical, sobhhhinto the region where errors occur. With only the sign fix,bhhhstill fails the end-to-end tests from #716.The rewritten loop computes the direction and$H$ only at accepted points, through a new helper
_get_direction. It stops withsuccess=Falseand a message if halving the step below1e-10still finds no decrease. It also setssuccessand a meaningfulmessageinstead of"Under development", and no longer evaluatesfun_and_jactwice at the same point.Why the existing tests did not catch it
Both tests passed
fun_and_jacreturning(-loglike(x), score(x)): the negative loglikelihood as the objective, but the derivative of the positive loglikelihood. The derivative therefore had the wrong sign for the function being minimized, and that sign error cancelled the one in the algorithm.test_maximum_likelihoodcalledbhhh_internaldirectly with this inconsistent pair.test_maximum_likelihood_external_interfaacepassed the same pair tominimizeviajac=True. optimagic does not check that a user-provided derivative matches the function, so the inconsistency went through.maximize. Either would have exposed the bug, because then the derivative is consistent with the objective.test_many_algorithms.pyexcludesbhhh, so the generic tests never ran it either.New tests
tests/optimagic/optimizers/test_bhhh.pynow contains, for logit and probit:test_score_is_derivative_of_loglike: checks the closed-form score against a numerical derivative of the loglikelihood. This guards the test inputs themselves, which was the missing piece.test_bhhh_internal_minimizes_negative_loglike: callsbhhh_internalwith the consistent pair(-loglike, -score).test_bhhh_external_interface: runsminimizewith-loglikeandmaximizewithloglike, each with closed-form and with numerical derivatives.All results are compared to statsmodels. On
main, all 10 optimization tests fail; the 2 score checks pass, as expected.In addition, this PR removes the
xfailmarkers from the threebhhhcases intests/optimagic/optimization/test_error_handling_end_to_end.py, added in #716. There, a Poisson regression and a Gaussian nonlinear regression run into a region where evaluations fail. These cases fail with only the sign fix and pass with both fixes.Not addressed here
bhhhshould not be expected to work on general likelihood-marked or least-squares problems that are not loglikelihoods.🤖 Generated with Claude Code