TExcelTAD
Home › Optimization › The Nelder-Mead Method

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 n+1n + 1 points through Rn\mathbb{R}^n 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, f(x1)≤⋯≤f(xn+1)f(\mathbf{x}_1) \le \dots \le f(\mathbf{x}_{n+1}), and let xˉ\bar{\mathbf{x}} be the centroid of all but the worst. Then try, in order:

Move Point Accept if
reflect xr=xˉ+(xˉ−xn+1)\mathbf{x}_r = \bar{\mathbf{x}} + (\bar{\mathbf{x}} - \mathbf{x}_{n+1}) better than the second worst, not better than the best
expand xe=xˉ+2(xˉ−xn+1)\mathbf{x}_e = \bar{\mathbf{x}} + 2(\bar{\mathbf{x}} - \mathbf{x}_{n+1}) the reflection was the new best and xe\mathbf{x}_e is even better
contract xc=xˉ+12(xn+1−xˉ)\mathbf{x}_c = \bar{\mathbf{x}} + \frac{1}{2}(\mathbf{x}_{n+1} - \bar{\mathbf{x}}) (inside) or toward xr\mathbf{x}_r (outside) better than the point it replaces
shrink every xi←x1+12(xi−x1)\mathbf{x}_i \leftarrow \mathbf{x}_1 + \frac{1}{2}(\mathbf{x}_i - \mathbf{x}_1) when all else fails

The simplex stretches along valleys, turns corners, and shrinks around a minimum.

python · try it yourself
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 n+1n + 1 points and progress per evaluation drops.
python · try it yourself
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 (f∗=0f^* = 0) 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:

python · try it yourself
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 via adaptive=True for higher dimensions), MATLAB's fminsearch, R's optim default.
  • Common in quick calibration of small models (curve fits with a handful of parameters, simulation tuning).

Exercises

Exercise 1: Reflection

Simplex vertices (0,0)(0, 0), (1,0)(1, 0), (0,1)(0, 1) with the worst at (0,1)(0, 1). Where is the reflected point?

Answer

(1,−1)(1, -1).

Solution

The centroid of the other two is (0.5,0)(0.5, 0); reflect: (0.5,0)+((0.5,0)−(0,1))=(1,−1)(0.5, 0) + ((0.5, 0) - (0, 1)) = (1, -1).

Exercise 2: Simplex size

How many points does the Nelder-Mead simplex have in 50 dimensions?

Answer
Solution

n+1n + 1 affinely independent points are needed to span Rn\mathbb{R}^n.

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
python · try it yourself
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…

  1. only function values
  2. gradients
  3. Hessians
  4. integer variables

In nn dimensions the Nelder-Mead simplex has…

  1. nn points
  2. n+1n + 1 points
  3. 2n2n points
  4. n2n^2 points

Which statement about Nelder-Mead is true?

  1. It is guaranteed to converge to a minimum of any smooth function
  2. It can stall at a non-stationary point, so restarts are common
  3. It scales well to thousands of variables
  4. It is the same as the LP simplex method