Skip to content

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

Backlog

Description

@janosg

Bug

second_derivative removes every dimension of length one from the Hessian. For a parameter vector with one entry it returns an array of shape () instead of (1, 1). first_derivative keeps these dimensions and returns shape (1,).

estimate_ml calls second_derivative on the internal parameter vector, which is always a 1d array, and then converts the result with matrix_to_block_tree. That conversion fails, so estimate_ml raises an IndexError whenever exactly one parameter is estimated. It does not matter whether params is a float, a dict, a DataFrame or an array.

How to reproduce

import numpy as np

import estimagic as em
import optimagic as om

om.second_derivative(lambda x: x @ x, np.array([1.0])).derivative.shape
# ()   expected: (1, 1)

om.second_derivative(lambda x: x @ x, np.array([1.0, 2.0])).derivative.shape
# (2, 2)

om.first_derivative(lambda x: x @ x, np.array([1.0])).derivative.shape
# (1,)

data = np.random.default_rng(0).normal(loc=0.3, size=100)


def loglike(params):
    return -0.5 * (data - params[0]) ** 2


em.estimate_ml(loglike, np.array([0.5]), {"algorithm": "scipy_lbfgsb"})
# IndexError: tuple index out of range

The traceback ends in matrix_to_block_tree → _check_dimensions_matrix at matrix.shape[0]. With two parameters, or with hessian=False, the same estimate_ml call works.

The same happens for array leaves of length one in pytrees and for vector-valued functions:

f = lambda p: float(np.sum(p["a"] ** 2) + p["b"] ** 2)
om.second_derivative(f, {"a": np.array([1.0]), "b": 2.0}).derivative
# every block has shape (); expected (1, 1) for a/a and (1,) for a/b and b/a

om.second_derivative(lambda x: x**2, np.array([1.0])).derivative.shape
# ()   expected: (1, 1, 1)

Cause

hessian_to_block_tree builds the shape of each block and drops all entries that are 1:

_shape = [k for k in (*s0, *s1, *s2) if k != 1]

_shape = [k for k in (*s0, *s1, *s2) if k != 1]

The line exists since pytree support for derivatives was added (#281, #286).

Possible fix

Keep all dimensions:

_shape = (*s0, *s1, *s2)

With this change, all estimate_ml calls above work, and the tests in tests/optimagic/parameters/test_block_trees.py, tests/optimagic/differentiation and tests/estimagic pass (303 tests). The full test suite was not run with this change. A regression test should compare the shapes for leaves of length one with the shapes that first_derivative (and jax) produce.

Found while reviewing #743. The bug is on main (1d05a14), independent of #743.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions