gridsearch()#
- gridsearch(func: Callable, candidates: ndarray, gridsearch_only: bool = False, label: str = 'parameter', verbose: bool = False) float | ndarray[source]#
Minimize a function by first checking a collection of candidates, then following up with a derivative-free optimization routine.
If the candidates are one-dimensional, meaning
func()is a scalar map \(f:\RR_+\to\RR\), the optimization is carried out viascipy.optimize.minimize_scalar()withmethod='brent'.Otherwise (\(f:\RR_+^p\to\RR\) for some \(p > 1\)), the optimization is carried out via
scipy.optimize.minimize()withmethod='Nelder-Mead'.- Parameters:
- funccallable(candidate) -> float
Function to minimize.
- candidateslist of floats or ndarrays
Candidates to check before starting the optimization routine.
- gridsearch_onlybool
If
True, stop after checking allcandidatesand do not follow up with optimization.- labelstr
Label for the types of candidates being checked.
- verbosebool
If
True, print information at each iteration.
- Returns:
- winnerfloat or ndarray
Optimized candidate.
- Raises:
- RuntimeError
If the grid search fails because
func()returnsnp.inffor each candidate.
- Warns:
- OpInfWarning
1) If the
candidatesare one-dimensional and the grid search winner is the smallest or largest candidate, or 2) If the minimization fails or does not result in a solution that improves upon the grid search.
Notes
The optimization minimizes
func(x)by varyingxlogarithmically. Unlessgridsearch_only=True, it is assumed that all entries of the argumentxare positive.If only one scalar candidate is given, the (one-dimensional) optimization sets the bounds to
[candidate / 100, candidate * 100].