Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ This release adds experimental support for **SciPy-style callbacks**, replaces p
`convergence_gtol_rel` and `convergence_gtol_scaled`. Reaching `stopping_maxiter` is
now reported as unsuccessful with the message "maxits reached" instead of as
successful with "user defined" ({ghuser}`janosg`).
- {gh}`728` Rewrites the `bhhh` optimizer. It now stops on the scale invariant decrement
`g'(J'J)^-1 g`, supports bounds, uses a line search with step doubling, rejects trial
points at which the objective or its derivative fails, and warns about weakly
identified parameters. The options of `bhhh` change ({ghuser}`janosg`).
- {gh}`727` Raises an `InvalidFunctionError` if the objective function or a user
provided derivative is not finite at the start params. The error message lists the
paths of the non-finite entries ({ghuser}`janosg`).
Expand Down
43 changes: 19 additions & 24 deletions docs/source/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,46 +777,41 @@ We implement a few algorithms from scratch. They are currently considered experi

.. code-block:: python

import numpy as np
import optimagic as om
from optimagic.optimizers.bhhh import LineSearch


@om.mark.likelihood
def fun(x):
return x**2


om.minimize(
fun=fun,
params=np.array([1.0, 2.0, 3.0]),
algorithm=om.algos.bhhh(stopping_maxiter=1_000),
om.maximize(
fun=loglike_contributions,
params=start_params,
algorithm=om.algos.bhhh(
convergence_decrement_tol=1e-8,
globalization=LineSearch(expansion="none"),
),
)

or using the string interface:

.. code-block:: python

import numpy as np
import optimagic as om


@om.mark.likelihood
def fun(x):
return x**2


om.minimize(
fun=fun,
params=np.array([1.0, 2.0, 3.0]),
om.maximize(
fun=loglike_contributions,
params=start_params,
algorithm="bhhh",
algo_options={"stopping_maxiter": 1_000},
algo_options={"convergence_decrement_tol": 1e-8},
)

``loglike_contributions`` must be decorated with ``om.mark.likelihood`` and return
one log-likelihood contribution per independent unit.

**Description and available options:**

.. autoclass:: optimagic.optimizers.bhhh.BHHH
:members:
:inherited-members: Algorithm, object

**Options of the line search:**

.. autoclass:: optimagic.optimizers.bhhh.LineSearch
```

```{eval-rst}
Expand Down
9 changes: 5 additions & 4 deletions docs/source/explanation/numdiff_background.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Numerical differentiation: methods

In this section we explain the mathematical background of forward, backward and central
differences. The main ideas in this chapter are taken from {cite}`Dennis1996`. x is used
for the pandas DataFrame with parameters. We index the entries of x as a n-dimensional
vector, where n is the number of variables in params_sr. The forward difference for the
gradient is given by:
differences. The main ideas in this chapter are taken from {cite}`nd-Dennis1996`. x is
used for the pandas DataFrame with parameters. We index the entries of x as a
n-dimensional vector, where n is the number of variables in params_sr. The forward
difference for the gradient is given by:

$$
\nabla f(x) = \begin{pmatrix}\frac{f(x + e_0 * h_0) - f(x)}{h_0}\\
Expand Down Expand Up @@ -71,5 +71,6 @@ central differences.

```{eval-rst}
.. bibliography:: ../refs.bib
:keyprefix: nd-
:filter: docname in docnames
```
31 changes: 31 additions & 0 deletions docs/source/refs.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,37 @@ @article{Ni2013
year = {2013}
}

@book{Train2009,
author = {Train, Kenneth E.},
title = {Discrete Choice Methods with Simulation},
edition = {2},
publisher = {Cambridge University Press},
address = {Cambridge},
year = {2009},
}

@article{Dennis1981,
author = {Dennis, John E., Jr. and Gay, David M. and Welsch, Roy E.},
title = {An Adaptive Nonlinear Least-Squares Algorithm},
journal = {ACM Transactions on Mathematical Software},
volume = {7},
number = {3},
pages = {348--368},
year = {1981},
doi = {10.1145/355958.355965},
}

@article{Bunch1988,
author = {Bunch, David S.},
title = {A Comparison of Algorithms for Maximum Likelihood Estimation of Choice Models},
journal = {Journal of Econometrics},
volume = {38},
number = {1--2},
pages = {145--167},
year = {1988},
doi = {10.1016/0304-4076(88)90031-0},
}

@Article{Froehlich2022,
author = {Fabian Fr{\"o}hlich and Peter K. Sorger},
journal = {PLOS Computational Biology},
Expand Down
68 changes: 68 additions & 0 deletions src/optimagic/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ class BoundedGradientBasedLeastSquaresLocalAlgorithms(AlgoSelection):
scipy_ls_trf: Type[ScipyLSTRF] = ScipyLSTRF


class BoundedGradientBasedLikelihoodLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH


class GradientBasedLocalNonlinearConstrainedScalarAlgorithms(AlgoSelection):
ipopt: Type[Ipopt] = Ipopt
nlopt_mma: Type[NloptMMA] = NloptMMA
Expand Down Expand Up @@ -963,6 +967,7 @@ def NonlinearConstrained(


class BoundedGradientBasedLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH
fides: Type[Fides] = Fides
iminuit_migrad: Type[IminuitMigrad] = IminuitMigrad
ipopt: Type[Ipopt] = Ipopt
Expand All @@ -983,6 +988,10 @@ class BoundedGradientBasedLocalAlgorithms(AlgoSelection):
def LeastSquares(self) -> BoundedGradientBasedLeastSquaresLocalAlgorithms:
return BoundedGradientBasedLeastSquaresLocalAlgorithms()

@property
def Likelihood(self) -> BoundedGradientBasedLikelihoodLocalAlgorithms:
return BoundedGradientBasedLikelihoodLocalAlgorithms()

@property
def NonlinearConstrained(
self,
Expand Down Expand Up @@ -1052,6 +1061,10 @@ def Bounded(self) -> BoundedGradientBasedLeastSquaresLocalAlgorithms:
class GradientBasedLikelihoodLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def Bounded(self) -> BoundedGradientBasedLikelihoodLocalAlgorithms:
return BoundedGradientBasedLikelihoodLocalAlgorithms()


class BoundedGradientBasedNonlinearConstrainedAlgorithms(AlgoSelection):
ipopt: Type[Ipopt] = Ipopt
Expand Down Expand Up @@ -1116,6 +1129,14 @@ def Local(self) -> BoundedGradientBasedLeastSquaresLocalAlgorithms:
return BoundedGradientBasedLeastSquaresLocalAlgorithms()


class BoundedGradientBasedLikelihoodAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def Local(self) -> BoundedGradientBasedLikelihoodLocalAlgorithms:
return BoundedGradientBasedLikelihoodLocalAlgorithms()


class GradientBasedNonlinearConstrainedScalarAlgorithms(AlgoSelection):
ipopt: Type[Ipopt] = Ipopt
nlopt_mma: Type[NloptMMA] = NloptMMA
Expand Down Expand Up @@ -2052,6 +2073,14 @@ def Parallel(self) -> BoundedLeastSquaresLocalParallelAlgorithms:
return BoundedLeastSquaresLocalParallelAlgorithms()


class BoundedLikelihoodLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def GradientBased(self) -> BoundedGradientBasedLikelihoodLocalAlgorithms:
return BoundedGradientBasedLikelihoodLocalAlgorithms()


class BoundedLocalParallelAlgorithms(AlgoSelection):
pounders: Type[Pounders] = Pounders
tranquilo: Type[Tranquilo] = Tranquilo
Expand Down Expand Up @@ -2308,6 +2337,7 @@ def Scalar(self) -> GradientBasedLocalScalarAlgorithms:


class BoundedGradientBasedAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH
fides: Type[Fides] = Fides
iminuit_migrad: Type[IminuitMigrad] = IminuitMigrad
ipopt: Type[Ipopt] = Ipopt
Expand Down Expand Up @@ -2335,6 +2365,10 @@ def Global(self) -> BoundedGlobalGradientBasedAlgorithms:
def LeastSquares(self) -> BoundedGradientBasedLeastSquaresAlgorithms:
return BoundedGradientBasedLeastSquaresAlgorithms()

@property
def Likelihood(self) -> BoundedGradientBasedLikelihoodAlgorithms:
return BoundedGradientBasedLikelihoodAlgorithms()

@property
def Local(self) -> BoundedGradientBasedLocalAlgorithms:
return BoundedGradientBasedLocalAlgorithms()
Expand Down Expand Up @@ -2430,6 +2464,10 @@ def Local(self) -> GradientBasedLeastSquaresLocalAlgorithms:
class GradientBasedLikelihoodAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def Bounded(self) -> BoundedGradientBasedLikelihoodAlgorithms:
return BoundedGradientBasedLikelihoodAlgorithms()

@property
def Local(self) -> GradientBasedLikelihoodLocalAlgorithms:
return GradientBasedLikelihoodLocalAlgorithms()
Expand Down Expand Up @@ -3091,6 +3129,7 @@ def Scalar(self) -> GlobalParallelScalarAlgorithms:


class BoundedLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH
fides: Type[Fides] = Fides
iminuit_migrad: Type[IminuitMigrad] = IminuitMigrad
ipopt: Type[Ipopt] = Ipopt
Expand Down Expand Up @@ -3133,6 +3172,10 @@ def GradientFree(self) -> BoundedGradientFreeLocalAlgorithms:
def LeastSquares(self) -> BoundedLeastSquaresLocalAlgorithms:
return BoundedLeastSquaresLocalAlgorithms()

@property
def Likelihood(self) -> BoundedLikelihoodLocalAlgorithms:
return BoundedLikelihoodLocalAlgorithms()

@property
def NonlinearConstrained(self) -> BoundedLocalNonlinearConstrainedAlgorithms:
return BoundedLocalNonlinearConstrainedAlgorithms()
Expand Down Expand Up @@ -3252,6 +3295,10 @@ def Parallel(self) -> LeastSquaresLocalParallelAlgorithms:
class LikelihoodLocalAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def Bounded(self) -> BoundedLikelihoodLocalAlgorithms:
return BoundedLikelihoodLocalAlgorithms()

@property
def GradientBased(self) -> GradientBasedLikelihoodLocalAlgorithms:
return GradientBasedLikelihoodLocalAlgorithms()
Expand Down Expand Up @@ -3455,6 +3502,18 @@ def Parallel(self) -> BoundedLeastSquaresParallelAlgorithms:
return BoundedLeastSquaresParallelAlgorithms()


class BoundedLikelihoodAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def GradientBased(self) -> BoundedGradientBasedLikelihoodAlgorithms:
return BoundedGradientBasedLikelihoodAlgorithms()

@property
def Local(self) -> BoundedLikelihoodLocalAlgorithms:
return BoundedLikelihoodLocalAlgorithms()


class BoundedParallelAlgorithms(AlgoSelection):
nevergrad_bo: Type[NevergradBayesOptim] = NevergradBayesOptim
nevergrad_cga: Type[NevergradCGA] = NevergradCGA
Expand Down Expand Up @@ -3956,6 +4015,7 @@ def Scalar(self) -> LocalScalarAlgorithms:

class BoundedAlgorithms(AlgoSelection):
bayes_opt: Type[BayesOpt] = BayesOpt
bhhh: Type[BHHH] = BHHH
fides: Type[Fides] = Fides
gfo_differential_evolution: Type[GFODifferentialEvolution] = (
GFODifferentialEvolution
Expand Down Expand Up @@ -4063,6 +4123,10 @@ def GradientFree(self) -> BoundedGradientFreeAlgorithms:
def LeastSquares(self) -> BoundedLeastSquaresAlgorithms:
return BoundedLeastSquaresAlgorithms()

@property
def Likelihood(self) -> BoundedLikelihoodAlgorithms:
return BoundedLikelihoodAlgorithms()

@property
def Local(self) -> BoundedLocalAlgorithms:
return BoundedLocalAlgorithms()
Expand Down Expand Up @@ -4277,6 +4341,10 @@ def Parallel(self) -> LeastSquaresParallelAlgorithms:
class LikelihoodAlgorithms(AlgoSelection):
bhhh: Type[BHHH] = BHHH

@property
def Bounded(self) -> BoundedLikelihoodAlgorithms:
return BoundedLikelihoodAlgorithms()

@property
def GradientBased(self) -> GradientBasedLikelihoodAlgorithms:
return GradientBasedLikelihoodAlgorithms()
Expand Down
Empty file.
Loading
Loading