Skip to content

Rewrite the BHHH algorithm - #728

Open
janosg wants to merge 4 commits into
likelihood-benchmarksfrom
bhhh-rewrite
Open

janosg wants to merge 4 commits into
likelihood-benchmarksfrom
bhhh-rewrite

Conversation

@janosg

@janosg janosg commented Oct 30, 2026

Copy link
Copy Markdown
Member

Replaces the bhhh optimizer with a new implementation. The name "bhhh" stays; the options change. Supersedes #719. Stacked on #722, whose likelihood benchmark sets the tests use.

Algorithm

  • Step: p = -(J'J)⁻¹g, from an eigendecomposition of the outer product of gradients (OPG) after scaling its diagonal to one. Small eigenvalues are raised to a floor instead of being dropped, as in NL2SOL (Dennis, Gay and Welsch 1981).
  • Bounds: the step minimizes the quadratic model over the feasible box, solved with scipy.optimize.lsq_linear(method="bvls").
  • Line search: Armijo backtracking with quadratic and cubic interpolation (Dennis and Schnabel, Algorithm A6.3.1). An accepted first step is doubled while the objective improves (Train 2009), up to 1024 times the BHHH step.
  • Stopping: on the decrement g'(J'J)⁻¹g (convergence_decrement_tol, default 1e-8), which does not depend on the units of the parameters or the number of observations. convergence_ftol_abs and convergence_xtol_rel are disabled by default. Success means that a convergence criterion is met.
  • Failed evaluations: trial points at which the objective or its derivative is not finite or raises an error are rejected, whatever the value of error_handling. The evaluations at the start params are not caught.
  • Warnings: parameters with zero derivatives at the start params, fewer contributions than parameters, and a nearly singular OPG at the solution.
  • Result: jac is the gradient, hess_inv is None. algorithm_output reports the decrement, the rank and condition number of the OPG, the active bounds and counts of backtracks, expansions and failed evaluations.

The docstring documents the requirements on the objective function: contributions of independent units, sums instead of means, no weights, and only likelihoods.

Benchmark (all four likelihood sets, good and bad starts, 232 runs): 214 runs are solved, including every good start. Without doubling, 200 are solved. The failures are bad starts in non-convex regions (gamma regression, Weibull) and heteroskedastic regression starts that need steps about 10⁸ times the BHHH step. A slow test guards the solved counts of the two small sets.

Related: #726 (non-raising evaluation of derivatives), #727 (finiteness checks at the start params).

bhhh is excluded from the generic tests in test_many_algorithms.py, because it does not converge on their zero-residual least-squares problem; it is tested in test_bhhh.py.

🤖 Generated with Claude Code

janosg and others added 2 commits October 30, 2026 07:56
Replace the bhhh optimizer by a new implementation: an eigendecomposition of the
scaled outer product of gradients with an eigenvalue floor, a box-constrained step
with bounds, an Armijo line search with interpolation and step doubling, and
stopping on the decrement g'(J'J)^-1 g. Trial points at which the objective or its
derivative fails are rejected. The docstring documents the requirements on the
objective function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in main via #722. Adapt the rewrite to changes on main:
- #747: import scipy in the BHHH subproblem only when it is solved, to keep import optimagic fast; regenerate algorithms.py, whose selection classes are no longer dataclasses.
- #716: the end-to-end error handling tests marked bhhh as a strict xfail, because the old bhhh stepped uphill. The rewrite passes them, so the xfail is removed.
- #690: keep the new format of the algorithm docs and add :members: and :inherited-members: to the BHHH autoclass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43424% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/optimagic/algorithms.py 75.00% 12 Missing ⚠️
src/optimagic/optimizers/_bhhh/bhhh_loop.py 97.67% 3 Missing ⚠️
Files with missing lines Coverage Δ
src/optimagic/optimizers/_bhhh/linalg.py 100.00% <100.00%> (ø)
src/optimagic/optimizers/_bhhh/line_search.py 100.00% <100.00%> (ø)
src/optimagic/optimizers/_bhhh/subproblem.py 100.00% <100.00%> (ø)
src/optimagic/optimizers/bhhh.py 100.00% <100.00%> (+3.63%) ⬆️
...gic/optimization/test_error_handling_end_to_end.py 100.00% <ø> (ø)
...sts/optimagic/optimization/test_many_algorithms.py 97.77% <ø> (ø)
tests/optimagic/optimizers/_bhhh/test_linalg.py 100.00% <100.00%> (ø)
...sts/optimagic/optimizers/_bhhh/test_line_search.py 100.00% <100.00%> (ø)
...ests/optimagic/optimizers/_bhhh/test_subproblem.py 100.00% <100.00%> (ø)
tests/optimagic/optimizers/test_bhhh.py 100.00% <100.00%> (ø)
... and 3 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

… of main

- tests/test_optimizer_library_markers.py (#749) read the source files with the platform encoding, which fails on Windows for the non-ASCII characters in the BHHH modules. Read them as UTF-8.
- The LineSearch docs in algorithms.md and numdiff_background.md both cite Dennis1996, which sphinxcontrib-bibtex reports as a duplicate citation; since #741 the docs build fails on warnings. Prefix the keys of the local bibliography of numdiff_background.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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