minimize
Builtin index · Signal processing
result = minimize(f, x0, [method="bfgs"], [grad=], [max_iter=200], [tol=1e-8])
Input: f, x0, method, grad, max_iter, tol · Output: result
What it is
f: a string naming a user-defined function f(params) (a length-P vector) returning a scalar; x0: a length-P vector, the starting point; method (optional string, *added in v0.4.14*): "bfgs" (the default, unchanged: the L-BFGS quasi-Newton minimizer; "lbfgs" is an alias) or "nelder_mead" (derivative-free simplex with the adaptive coefficients of Gao and Han, which hold up as the dimension grows; the initial simplex perturbs each coordinate of x0 by 5%, or by 0.00025 where it is zero; use it for noisy or non-smooth objectives; converged is true only when the simplex has shrunk below tol in every coordinate *and* the function values agree to tol; max_iter then defaults to 200*P; grad= is refused with it; a NaN objective value is treated as +Inf and the simplex steps away from it; f(x0) must be finite); grad (optional string naming a gradient function, else the gradient is numerical/central-differences; method="bfgs" only); max_iter/tol (optional numbers).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = minimize(f, x0, [method="bfgs"], [grad=], [max_iter=200], [tol=1e-8]) | f string, vector, or scalarf: a string naming a user-defined function f(params) (a length-P vector) returning a scalar.x0 vectorx0: a length-P vector, the starting point.method string, optional (default "bfgs")method (optional string, *added in v0.4.14*): "bfgs" (the default, unchanged: the L-BFGS quasi-Newton minimizer.grad optionalgrad= is refused with it.max_iter optional (default 200)max_iter then defaults to 200*P.tol number, optional (default 1e-8)max_iter/tol (optional numbers). | Model —kind "minimize"params vectorlength-P vectorvalue numbera numberconverged booleana boolean | Returns a Model (kind "minimize") with fields params (length-P vector), value (a number), converged (a boolean). An unknown method is an error naming the two choices. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
bowl(p) := (p[0]-3)^2 + (p[1]+2)^2
m = minimize("bowl", [0, 0])
print("minimum at {m.params}, value {m.value}")
minimum at [3, -2], value 2.029121e-19
from numerics-dsp
bowl_grad(p) := [2*(p[0]-3), 2*(p[1]+2)]
m2 = minimize("bowl", [0, 0], grad="bowl_grad") # same minimum, fewer
# evaluations per step
from numerics-dsp
wavy(p) := -0.5*exp(-(p[0]+2)^2) - 2*exp(-(p[0]-3)^2/2) # shallow local
# min at -2,
# deeper global
# min at 3
stuck = minimize("wavy", [-2]) # stays at the shallow one
found = basin_hopping("wavy", [-2], n_iter=60, step_size=3) # finds the deep one
from numerics-dsp
How it works
minimize(f, x0, [grad=], [max_iter=200], [tol=1e-8]) — L-BFGS, a quasi-Newton ("pseudo-Newton") multivariate minimizer: f takes a single params vector and returns a scalar. By default the gradient is computed numerically (central differences); passing grad="gradfn" (a function params -> gradient vector, same shape as f) uses that exact analytical gradient instead — cheaper (one call instead of 2n) and not subject to finite-difference error. Model kind "minimize", fields params, value, converged.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.