Quant Memo
Advanced

Gradient Descent

Walking downhill in the direction that drops fastest, taking a step, and repeating is almost the entire training algorithm behind modern machine learning — the whole method fits in one line, and every subtlety is in choosing the step size.

Prerequisites: Derivatives and Differentiation Rules, Convex Sets and Convex Functions

Train a neural network with millions of parameters, calibrate a volatility surface to hundreds of option prices, or fit any model where there's no clean formula for the best answer — and almost always, underneath whatever fancy name the optimiser goes by, the actual mechanism is embarrassingly simple: look at which direction makes the error worse fastest, walk the opposite way by a small amount, and repeat. That's gradient descent, and its simplicity is deceptive — the entire practical difficulty of training modern models comes down to one question this basic idea leaves open: how big should each step be?

The analogy: descending a foggy mountain by feel

You're on a mountainside in thick fog, trying to reach the valley floor, with no map and visibility only a few feet. The only information available is what's under your own feet right now: which direction is steepest downhill from exactly where you're standing. A sensible strategy: face the steepest downhill direction, take one step, stop, feel around again, and repeat. Take steps too large and you might stride right over a narrow valley and end up climbing the opposite slope; take steps too small and you'll get down eventually, but agonisingly slowly. Gradient descent is this exact strategy, formalised: it never sees the whole mountain, only the local slope, and its entire performance hinges on step size.

Writing it down

For a function f(x)f(x) you want to minimise, the gradient f(x)\nabla f(x) points in the direction of steepest increase at xx (in one dimension it's just f(x)f'(x); in many dimensions, it's the vector of all partial derivatives). Since you want to go down, you step in the opposite direction:

xk+1=xkηf(xk).x_{k+1} = x_k - \eta\, \nabla f(x_k) .

In words: the next point xk+1x_{k+1} is the current point xkx_k, moved a distance controlled by η\eta (the learning rate, or step size) directly opposite the direction of steepest increase — i.e. toward steepest decrease. Repeat this update over and over, and for well-behaved (convex, convex) functions with a small enough η\eta, the sequence xkx_k provably converges to the true minimum.

The choice of η\eta is where all the practical difficulty lives. Too large, and the update can overshoot the minimum and diverge — each step lands further from the answer than the last, oscillating out of control. Too small, and convergence is correct but glacially slow, needing enormous numbers of steps to make any real progress. For a simple case — a quadratic f(x)=12ax2f(x)=\tfrac12 a x^2 — the exact stability boundary can be written down: the iteration converges if and only if 0<η<2/a0 < \eta < 2/a, and it converges fastest at η=1/a\eta = 1/a, landing exactly on the minimum in a single step. This one clean formula is the seed of every learning-rate heuristic used in practice.

Convergence explorer
true meansamples →
after n = 200estimate -0.025error 0.025std error 0.071

Watch a running quantity settle onto its true value as more steps accumulate — that settling curve is the same shape a well-tuned gradient descent run traces on its own error, step by step getting closer to the minimum, with the rate of settling controlled by the same trade-off between step size and stability.

x0 approaching x* = 2
Each step lands closer to the bottom of the bowl, with the gap to the true minimum shrinking by a fixed factor every time — the same steady, converging pattern worked out numerically below.

Worked example 1: minimising a parabola, three steps by hand

Minimise f(x)=x24x+5f(x) = x^2 - 4x + 5 (true minimum at x=2x^\star=2, found by f(x)=2x4=0f'(x)=2x-4=0, with f(2)=1f(2)=1). Gradient: f(x)=2x4f'(x) = 2x-4. Start at x0=0x_0 = 0, use learning rate η=0.2\eta = 0.2.

Step 1: f(0)=4f'(0) = -4. Update: x1=00.2(4)=0.8x_1 = 0 - 0.2(-4) = 0.8.

Step 2: f(0.8)=2(0.8)4=2.4f'(0.8) = 2(0.8)-4 = -2.4. Update: x2=0.80.2(2.4)=0.8+0.48=1.28x_2 = 0.8 - 0.2(-2.4) = 0.8+0.48=1.28.

Step 3: f(1.28)=2(1.28)4=1.44f'(1.28) = 2(1.28)-4=-1.44. Update: x3=1.280.2(1.44)=1.28+0.288=1.568x_3 = 1.28-0.2(-1.44)=1.28+0.288=1.568.

The sequence is 0,0.8,1.28,1.568,0,\, 0.8,\, 1.28,\, 1.568,\ldots, converging toward x=2x^\star=2. Notice the gap to the true answer — 2,1.2,0.72,0.4322, 1.2, 0.72, 0.432 — shrinks by a factor of exactly 0.60.6 every step. That's not a coincidence: for a quadratic with a=2a=2 and η=0.2\eta=0.2, theory predicts the error shrinks by a factor of 1ηa=10.4=0.6|1-\eta a| = |1-0.4|=0.6 per step, and the hand calculation matches exactly.

Worked example 2: overshoot, and choosing a better rate

Same function, same start x0=0x_0=0, but now η=1.2\eta = 1.2 — larger than the stability boundary 2/a=2/2=12/a=2/2=1 derived above, so divergence is predicted. Check it: f(0)=4f'(0)=-4, x1=01.2(4)=4.8x_1 = 0-1.2(-4)=4.8. f(4.8)=2(4.8)4=5.6f'(4.8)=2(4.8)-4=5.6, x2=4.81.2(5.6)=4.86.72=1.92x_2 = 4.8-1.2(5.6)=4.8-6.72=-1.92. f(1.92)=2(1.92)4=7.84f'(-1.92)=2(-1.92)-4=-7.84, x3=1.921.2(7.84)=1.92+9.408=7.488x_3=-1.92-1.2(-7.84)=-1.92+9.408=7.488. The sequence is 0,4.8,1.92,7.488,0,\,4.8,\,-1.92,\,7.488,\ldots — swinging wider and further from x=2x^\star=2 every step, exactly the overshoot-and-diverge failure mode predicted by η>2/a\eta > 2/a.

Now try the theoretically optimal rate η=1/a=0.5\eta=1/a=0.5: f(0)=4f'(0)=-4, x1=00.5(4)=2x_1 = 0-0.5(-4)=2 — already exactly at the minimum, in a single step, matching the "fastest convergence" prediction exactly. In practice you rarely know aa (the curvature) in advance, which is precisely why real training pipelines tune the learning rate empirically or use adaptive methods that estimate curvature on the fly, rather than compute this ideal rate directly.

What this means in practice

  • It's the literal engine of neural network training. Backpropagation computes f\nabla f efficiently for millions of parameters; gradient descent (almost always its noisy cousin, stochastic gradient descent) is what actually moves the parameters using that gradient.
  • The learning rate is the single most consequential hyperparameter in most ML pipelines. Too high and training visibly diverges or oscillates (loss spikes); too low and training "works" but wastes enormous compute converging too slowly — this exact trade-off, proven analytically for a simple quadratic above, is what learning-rate schedules and warmup heuristics are built to manage on much messier, non-quadratic loss surfaces.
  • It only guarantees a global minimum on convex problems. On the non-convex loss surfaces of deep networks, gradient descent still faithfully walks downhill, but "downhill" can lead to any of many local minima or saddle points — a very different guarantee from the quadratic case above.

Gradient descent repeats one instruction: move opposite the local direction of steepest increase, scaled by a step size η\eta. For a simple quadratic, the whole story is analytically exact — convergence needs η<2/a\eta < 2/a, and the error shrinks by a fixed factor 1ηa|1-\eta a| every step — and that same trade-off between step size and stability governs every real, messier optimisation problem gradient descent is used on.

The classic confusion is treating a smoothly decreasing loss curve as proof the learning rate is well chosen. A loss curve can decrease steadily right up until it doesn't — many real (non-quadratic) loss surfaces have regions of very different curvature, and a learning rate that's stable in a shallow region can suddenly overshoot and diverge the moment the path enters a much steeper one. The worked overshoot example above used a constant, badly-chosen η\eta on a function with constant curvature and still diverged outright; real loss surfaces, with curvature that varies from point to point, can look perfectly well-behaved for many steps and then blow up. This is exactly why production training loops monitor for sudden loss spikes rather than trusting a fixed learning rate to remain safe for the entire run, and why adaptive methods that adjust the effective step size on the fly are the default rather than an optional extra.

Related concepts

Practice in interviews

Further reading

  • Boyd & Vandenberghe, Convex Optimization (ch. 9)
  • Nocedal & Wright, Numerical Optimization (ch. 3)
ShareTwitterLinkedIn