Skip to content

Improve the function evaluations at the start params - #727

Merged
janosg merged 11 commits into
mainfrom
check-finite-start-values
Nov 15, 2026
Merged

janosg merged 11 commits into
mainfrom
check-finite-start-values

Conversation

@janosg

@janosg janosg commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

optimagic evaluates the objective and user provided derivatives at the start params before it optimizes. This PR improves these evaluations in two ways.

Check that the results are finite. So far, only exceptions were caught. If the objective returns inf or nan at the start params, the optimizer starts from an invalid point. For example, with an inf start value, scipy_lbfgsb reports success at a wrong point and scipy_neldermead silently moves away. The default penalty used with error_handling="continue" is also derived from the start value. This PR raises an InvalidFunctionError if the objective, or a derivative provided via jac or fun_and_jac, is not finite at the start params. The message lists the paths of the non-finite entries and abbreviates large outputs.

Only evaluate derivatives that are used. User provided derivatives were evaluated at the start params even for algorithms that do not use derivatives. They are now only evaluated and checked if the algorithm needs them (algo_info.needs_jac).

Evaluate fun_and_jac only once. If fun is derived from fun_and_jac, the derivative from the first evaluation is reused instead of evaluating fun_and_jac a second time.

Share kwargs between fun and fun_and_jac. If fun is derived from fun_and_jac, fun_kwargs and fun_and_jac_kwargs are used for both if only one of them is given.

Numerical derivatives are not evaluated at the start params, because that would cost an extra derivative evaluation. A non-finite numerical derivative still raises at the algorithm's first derivative evaluation, as before.

Behavior changes

  1. Non-finite objective at the start params raises. If the objective value at the start params contains inf, -inf or nan, minimize and maximize raise an InvalidFunctionError before the optimization starts. This applies to all algorithms, including global optimizers and multistart, where the start params matter less. Before, the optimization ran from the invalid point.
  2. Overflowing aggregates raise, too. The check is done on the aggregated scalar value. For least squares and likelihood functions, residuals or contributions that are all finite but whose sum of squares or sum overflows to inf also raise.
  3. Non-finite user provided derivatives at the start params raise earlier and with a different exception. For algorithms that use derivatives, a non-finite derivative provided via jac or fun_and_jac at the start params raises an InvalidFunctionError before the optimization starts. Before, a UserFunctionRuntimeError was raised at the algorithm's first derivative evaluation. Code that catches UserFunctionRuntimeError no longer catches this case.
  4. Derivative-free algorithms ignore user provided derivatives at the start params. For algorithms with needs_jac=False, jac is no longer called at the start params, and neither is fun_and_jac unless it is also used to compute the objective. A derivative function that raises, returns non-finite values, or returns output that cannot be aligned with the params and the objective output no longer causes an error with these algorithms.

🤖 Generated with Claude Code

janosg and others added 2 commits October 25, 2026 12:42
optimagic only caught exceptions in the first evaluation of the objective. With an
inf start value, scipy_lbfgsb reported success at a wrong point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The message for a non-finite objective now contains the complete output of the
objective function instead of positions in the flattened value, which are not
meaningful for pytrees. User provided derivatives (jac and fun_and_jac) are now also
checked for finiteness at the start params.

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

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.08541% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/optimagic/optimization/optimize.py 90.47% 6 Missing ⚠️
src/optimagic/optimization/scipy_aliases.py 62.50% 3 Missing ⚠️
tests/optimagic/optimization/test_optimize.py 98.91% 2 Missing ⚠️
Files with missing lines Coverage Δ
...imagic/optimization/create_optimization_problem.py 89.96% <100.00%> (+0.74%) ⬆️
...agic/optimization/internal_optimization_problem.py 93.08% <100.00%> (+0.03%) ⬆️
...imagic/optimization/test_invalid_jacobian_value.py 100.00% <100.00%> (ø)
tests/optimagic/optimization/test_optimize.py 99.05% <98.91%> (-0.95%) ⬇️
src/optimagic/optimization/scipy_aliases.py 68.96% <62.50%> (+2.29%) ⬆️
src/optimagic/optimization/optimize.py 94.23% <90.47%> (+2.05%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/optimagic/optimization/optimize.py Resolved
janosg and others added 4 commits November 1, 2026 10:38
Check finiteness of the objective on its scalar aggregate instead of selecting
the aggregation level by function type. Check user provided derivatives only
for algorithms that need them, since others never use the derivative.

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

Algorithms that do not need derivatives never call jac or fun_and_jac, so the
evaluation at the start params was wasted. Collect the derivative evaluations
in one dict that feeds both the converter and the finiteness check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The condition `origin == "jac" or "fun_and_jac"` was always true, so a
non-finite numerical derivative was reported as "the derivative provided via
numerical".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@janosg janosg changed the title Raise if the objective or a derivative is not finite at the start params Improve the function evaluations at the start params Nov 1, 2026
@janosg
janosg requested a review from hmgaudecker November 1, 2026 10:06
@janosg

janosg commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

@hmgaudecker and @timmens, this PR introduces a few behavior changes. What is your opinion? Now would be a good time as the next release is probably 0.6.0.

@hmgaudecker hmgaudecker left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This closes a real gap. Starting from a non-finite point is not something any optimizer recovers from, and the origin == "jac" or "fun_and_jac" fix is a nice catch. A few points, the first one I'd like addressed before merging.

  1. Size of the error message: _format_in_full prints the whole output without a limit. For likelihood problems, the objective output is one value per observation. estimagic.estimate_ml, for example, passes loglikeobs. The user-provided derivative has one entry per parameter. In our application (skillmodels, health-cognition), that is one value per person and thus a gradient with some 14k entries. A single non-finite contribution produces a very large exception message. It ends up in pytask's captured traceback and in the SLURM log, where nobody reads the relevant line.

    Suggestion: report the paths of the non-finite leaves. Positions in the flat array are not meaningful, but tree paths are. Report the number of non-finite entries too, and then either the full repr below a size threshold or a truncated one above it. The pytree test cases would still pass with paths: 'b', c.

  2. Derivatives of fixed parameters: The derivative is checked after converter.derivative_to_internal. So non-finite entries for parameters that are fixed by constraints are ignored, and with transforming constraints they spread to the internal entries they enter. I think this is the right behaviour, and it matches the check during the optimization. But it is a user-visible rule, so it deserves:

    1. a sentence in the docstring
    2. a test: nan in the derivative of a fixed parameter does not raise.
  3. Question regarding multistart and global optimizers: Raising for these is a deliberate choice according to the description. Should the message say that the start params must be valid even with multistart? A user there may think of them as one sample among many. Optional.

  4. Double evaluation of fun_and_jac: If only fun_and_jac is given, fun is derived from it via split_fun_and_jac. So fun_and_jac runs once in create_optimization_problem and again here. This is unchanged by the PR, but since the block is rewritten anyway, the derivative from the first call could be kept. For expensive objectives that saves a full evaluation at the start.

  5. Minor stuff:

    1. Tests: a maximize case would show that the check uses the internal (sign-flipped) value correctly. A UserFunctionRuntimeError → InvalidFunctionError case for a user who catches the old exception would document behaviour change 3.
    2. CHANGES.md: the entry mentions only the error. The behaviour change for derivative-free algorithms (no evaluation of jac/fun_and_jac at the start params) seems worth its own line.

Downstream check. We pinned the PR head (c843a18) in health-cognition and ran our pipeline's debug estimation (skillmodels CHS, fides, fun_and_jac plus a likelihood-type fun, one A100) end to end. The run covered AMN start values, two fides iterations, inference and our convergence check. All of it passed, with no new warnings or errors.

Co-authored-by: Hans-Martin von Gaudecker <hmgaudecker@gmail.com>
janosg and others added 3 commits November 15, 2026 17:34
- List the paths of non-finite entries in the error messages and abbreviate
  large outputs instead of printing them in full.
- Say in the error message and the docs that the start params have to be
  valid for global optimizers and multistart, too.
- Evaluate fun_and_jac only once at the start params if fun is derived
  from it.
- Add tests for maximize and for the changed exception type.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…nd_jac

If only fun_and_jac was provided or jac=True, keyword arguments had to be
passed via fun_kwargs and fun_and_jac_kwargs. Providing just one of them
failed, or silently used default values for the other function.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@janosg

janosg commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review, @hmgaudecker. All points are addressed:

  1. The message now lists the number and the paths of the non-finite entries (at most 20) and abbreviates large outputs. For flat arrays the paths are positions.
  2. It turned out that non-finite derivative entries of fixed parameters are not ignored: nan * 0 is nan in the multiplication with the reparametrization Jacobian, so they spread to the whole internal derivative. This is the same on main during the optimization. Ignore derivatives of parameters that do not depend on internal parameters #742 fixes the converter. The test you asked for needs Ignore derivatives of parameters that do not depend on internal parameters #742 and will follow once it is merged.
  3. The message, the docstrings of minimize and maximize, and the how-to guides on errors and multistart now say that the start params must be valid for all algorithms.
  4. fun_and_jac is now evaluated once at the start params if fun is derived from it.
  5. Added the maximize and exception type tests and a separate CHANGES.md entry for derivative-free algorithms.
  6. Also fixed: if fun is derived from fun_and_jac (only fun_and_jac given, or jac=True), it is now enough to pass either fun_kwargs or fun_and_jac_kwargs. Before, giving only one of them failed or silently used default values.

@janosg
janosg merged commit 1aa2368 into main Nov 15, 2026
22 checks passed
@janosg
janosg deleted the check-finite-start-values branch November 15, 2026 17:20
janosg added a commit that referenced this pull request Nov 16, 2026
Resolve conflicts with #727 and #741: keep the removal of read_log.py, of the
deprecated multistart options and of the deprecated type aliases in optimize.py.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

2 participants