The Nelder-Mead Method
The Nelder-Mead method (1965), also called the downhill simplex method (not to be confused with the simplex method for LPs), moves a simplex of points through by reflecting, expanding and shrinking it according to which vertex is worst. It needs no derivatives, handles mildly noisy and non-smooth functions, and is the default derivative-free method in many tools (fminsearch, optim).
The moves
Order the vertices by function value, , and let be the centroid of all but the worst. Then try, in order:
| Move | Point | Accept if |
|---|---|---|
| reflect | better than the second worst, not better than the best | |
| expand | the reflection was the new best and is even better | |
| contract | (inside) or toward (outside) | better than the point it replaces |
| shrink | every | when all else fails |
The simplex stretches along valleys, turns corners, and shrinks around a minimum.
import numpy as np
def nelder_mead(f, x0, step=0.5, tol=1e-10, max_iter=2000):
n = len(x0)
S = np.vstack([x0] + [x0 + step * e for e in np.eye(n)])
F = np.array([f(p) for p in S]); evals = n + 1
for it in range(max_iter):
order = np.argsort(F); S, F = S[order], F[order]
if F[-1] - F[0] < tol:
break
cen = S[:-1].mean(axis=0)
xr = cen + (cen - S[-1]); fr = f(xr); evals += 1
if fr < F[0]:
xe = cen + 2 * (cen - S[-1]); fe = f(xe); evals += 1
S[-1], F[-1] = (xe, fe) if fe < fr else (xr, fr)
elif fr < F[-2]:
S[-1], F[-1] = xr, fr
else:
xc = cen + 0.5 * ((xr if fr < F[-1] else S[-1]) - cen); fc = f(xc); evals += 1
if fc < min(fr, F[-1]):
S[-1], F[-1] = xc, fc
else:
S[1:] = S[0] + 0.5 * (S[1:] - S[0])
F[1:] = [f(p) for p in S[1:]]; evals += n
return S[0], F[0], it, evals
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
x, fx, iters, evals = nelder_mead(f, np.array([-1.2, 1.0]))
print("x =", np.round(x, 5), " f below 1e-10:", bool(fx < 1e-10), " iterations:", iters, " evaluations:", evals)x = [1. 1.] f below 1e-10: True iterations: 88 evaluations: 166
Strengths and weaknesses
- Robust on non-smooth and mildly noisy functions in a few dimensions (up to about 10).
- No convergence guarantee in general: McKinnon's counterexample is a smooth, strictly convex function in 2D on which Nelder-Mead converges to a non-stationary point. In practice, restart from the best point with a fresh simplex.
- Scales badly with dimension: the simplex has points and progress per evaluation drops.
import numpy as np
from scipy.optimize import minimize
rng = np.random.default_rng(0)
for n in (2, 5, 10, 20):
f = lambda x: np.sum((x - 1)**2 * np.arange(1, n + 1))
r = minimize(f, np.zeros(n), method="Nelder-Mead", options={"maxfev": 100000, "xatol": 1e-6, "fatol": 1e-10})
print(f"n = {n:2d}: {r.nfev:6d} evaluations, f =", f"{r.fun:.1e}" if r.fun > 1e-8 else "below 1e-8 (solved)")n = 2: 154 evaluations, f = below 1e-8 (solved)
n = 5: 1014 evaluations, f = below 1e-8 (solved)
n = 10: 4985 evaluations, f = 7.9e+00
n = 20: 14592 evaluations, f = 9.1e+01
In 10 and 20 dimensions the method gives up far from the minimum () even after thousands of evaluations, although the function is a simple separable quadratic.
A non-smooth objective
Nelder-Mead only compares values, so kinks do not bother it:
import numpy as np
from scipy.optimize import minimize
pts = np.array([[0.0, 0.0], [4.0, 0.0], [1.0, 3.0], [5.0, 4.0]])
f = lambda p: np.sum(np.linalg.norm(pts - p, axis=1)) # total distance (Fermat-Weber point)
r = minimize(f, pts.mean(axis=0), method="Nelder-Mead", options={"xatol": 1e-8, "fatol": 1e-10})
print("facility location:", np.round(r.x, 4), " total distance:", round(r.fun, 4))facility location: [2.2222 1.7778] total distance: 10.6458
In software
scipy.optimize.minimize(method="Nelder-Mead")(with adaptive parameters viaadaptive=Truefor higher dimensions), MATLAB'sfminsearch, R'soptimdefault.- Common in quick calibration of small models (curve fits with a handful of parameters, simulation tuning).
Exercises
Exercise 1: Reflection
Simplex vertices , , with the worst at . Where is the reflected point?
Answer
.
Solution
The centroid of the other two is ; reflect: .
Exercise 2: Simplex size
How many points does the Nelder-Mead simplex have in 50 dimensions?
Answer
Solution
affinely independent points are needed to span .
Exercise 3: Restart (code)
Run SciPy's Nelder-Mead on Rosenbrock with a loose tolerance (xatol=1e-2), then restart from the result with a tight tolerance. Does the restart improve the answer?
Answer
Yes: the restart reaches the minimum much more precisely.
Solution
import numpy as np
from scipy.optimize import minimize
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
r1 = minimize(f, [-1.2, 1.0], method="Nelder-Mead", options={"xatol": 1e-2, "fatol": 1e-2})
r2 = minimize(f, r1.x, method="Nelder-Mead", options={"xatol": 1e-10, "fatol": 1e-12})
print(np.round(r1.x, 4), f"{r1.fun:.1e}", "->", np.round(r2.x, 6), "f below 1e-15:", bool(r2.fun < 1e-15))[1.001 1.0018] 8.5e-06 -> [1. 1.] f below 1e-15: True
Test yourself
Nelder-Mead uses…
- only function values
- gradients
- Hessians
- integer variables
In dimensions the Nelder-Mead simplex has…
- points
- points
- points
- points
Which statement about Nelder-Mead is true?
- It is guaranteed to converge to a minimum of any smooth function
- It can stall at a non-stationary point, so restarts are common
- It scales well to thousands of variables
- It is the same as the LP simplex method