Skip to content

Keep length-one dimensions in second_derivative - #755

Open
anshurajbisoyi98-ctrl wants to merge 1 commit into
optimagic-dev:mainfrom
anshurajbisoyi98-ctrl:fix-752-hessian-length-one-dims
Open

anshurajbisoyi98-ctrl wants to merge 1 commit into
optimagic-dev:mainfrom
anshurajbisoyi98-ctrl:fix-752-hessian-length-one-dims

Conversation

@anshurajbisoyi98-ctrl

Copy link
Copy Markdown
Contributor

What problem do you want to solve?

Closes #752, reported by janosg.

I used the fix proposed in the issue: hessian_to_block_tree now keeps the full
(*s0, *s1, *s2) shape instead of dropping every dimension of length one. All the
examples from the issue now give the expected shapes, and estimate_ml works with a
single parameter.

One side effect worth pointing out: a one-row DataFrame with a value column used to
get a bare scalar block back. It now gets a one-by-one DataFrame, the same as DataFrames
with more rows.

Tests: a parametrized shape test for the cases in the issue, and an estimate_ml test
with one float parameter that checks the Hessian covariance against 1 / n. Both fail on
main. I ran tests-fast and the slow tests in tests/estimagic,
tests/optimagic/differentiation and tests/optimagic/parameters. I didn't touch the
jax comparison tests. Their helper uses assert_array_almost_equal, which treats a 0-d
array as equal to a (1, 1) one, so they wouldn't have caught this.

Checklist

  • I have read the contributor guide.
  • I have described any additional validation I performed (e.g., manual checks, numerical comparisons, or benchmarks) and any remaining validation gaps.
  • I have added or updated tests for changed behavior.
  • I have updated documentation for user-facing changes.
  • I have linked any issues this PR resolves using Closes #XXXX.
  • I have described any substantial AI assistance and how I checked the result.
  • I have selected the appropriate base branch and described any remaining work.

I used Claude Code to help check the fix and write the tests. I reviewed the change,
checked that the new tests fail without it, and ran the test suites above and ty
locally.

hessian_to_block_tree dropped every dimension of length one, so the
Hessian of a function of one parameter came back with shape () instead
of (1, 1). estimate_ml then failed with an IndexError whenever a single
parameter was estimated. Keep the full block shape, as
matrix_to_block_tree already does for first derivatives.

Closes optimagic-dev#752

Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/optimagic/parameters/block_trees.py 85.46% <100.00%> (-0.09%) ⬇️
tests/estimagic/test_estimate_ml.py 99.48% <100.00%> (+0.02%) ⬆️
tests/optimagic/parameters/test_block_trees.py 97.29% <100.00%> (+0.19%) ⬆️

... and 2 files with indirect coverage changes

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

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.

second_derivative drops dimensions of length one, so estimate_ml fails for a single parameter

1 participant