Quant Memo
Core

The Conjugate Gradient Method

An iterative method for minimizing quadratic-like functions that picks each new search direction to avoid undoing the progress made by all previous directions, reaching the exact minimum in far fewer steps than plain gradient descent.

Prerequisites: Gradient Descent, Newton's Method for Optimization

Gradient descent on a long, narrow valley zig-zags badly: each step points straight downhill from where you stand, but on an elongated bowl "straight downhill" keeps overcorrecting side to side instead of heading toward the true minimum, so progress is painfully slow. You could fix this with Newton's method, which uses full curvature information, but that requires an expensive Hessian. The conjugate gradient method finds a middle ground: it never computes the Hessian, yet it avoids the zig-zag by being smarter about which direction to search next.

An analogy: not retracing your steps

Imagine hiking down a long, narrow ravine by always choosing the current steepest-downhill direction. You'll swing back and forth across the ravine's width, wasting most of your motion correcting a mistake you just made, rather than progressing along its length. A smarter hiker remembers the direction of her last few moves and deliberately chooses each new direction so it doesn't undo the progress those earlier moves already made along the ravine's length — she never has to "re-fix" the same component of her position twice. That's the core idea of conjugacy: each new search direction is chosen to be independent, in a precise mathematical sense, of the directions already used.

The method, one symbol at a time

For a quadratic function f(x)=12xTAxbTxf(x) = \tfrac{1}{2} x^T A x - b^T x (where AA is a symmetric positive-definite matrix describing curvature), the method builds a sequence of search directions d0,d1,d2,d_0, d_1, d_2, \dots that are AA-conjugate, meaning diTAdj=0d_i^T A \, d_j = 0 for iji \neq j. In plain English, this is a generalized notion of "at right angles" adapted to the shape of the bowl AA describes, rather than plain geometric right angles. At each step the algorithm moves along the current direction dkd_k by the exact optimal amount, then picks the next direction dk+1d_{k+1} by combining the new gradient with the previous direction:

dk+1=f(xk+1)+βkdk,d_{k+1} = -\nabla f(x_{k+1}) + \beta_k \, d_k ,

where βk\beta_k is a scalar chosen (by one of a few standard formulas) to enforce conjugacy with dkd_k. In words: move in the steepest-descent direction, but corrected by a fraction of the previous direction so as not to disturb the progress that direction already made. The remarkable result: for an nn-dimensional quadratic problem, this process finds the exact minimum in at most nn steps — a hard guarantee gradient descent never gives.

Worked example 1: a simple two-dimensional quadratic

Minimize f(x,y)=x2+10y2f(x,y) = x^2 + 10y^2 (a narrow elongated bowl, since yy is penalized ten times more steeply), starting at (x0,y0)=(10,1)(x_0, y_0) = (10, 1). Plain gradient descent zig-zags across the narrow yy-direction for dozens of iterations. Conjugate gradient, in contrast, is guaranteed to find the exact minimum (0,0)(0,0) in at most 2 steps, since this is a 2-dimensional quadratic problem — its first step moves in the steepest direction, and its second step, chosen to be AA-conjugate to the first, lands exactly on the minimum rather than merely getting closer.

Worked example 2: solving a linear system via CG

Conjugate gradient is also the standard way to solve large sparse linear systems Ax=bAx = b, since solving Ax=bAx=b is equivalent to minimizing f(x)=12xTAxbTxf(x) = \tfrac{1}{2}x^TAx - b^Tx. Suppose A=(4113)A = \begin{pmatrix} 4 & 1 \\ 1 & 3 \end{pmatrix} and b=(1,2)b = (1, 2), starting at x0=(0,0)x_0 = (0,0). The initial gradient (residual) is r0=bAx0=(1,2)r_0 = b - Ax_0 = (1, 2), so the first direction is d0=(1,2)d_0 = (1,2). Stepping optimally along d0d_0 and then computing a conjugate second direction d1d_1 takes the solver to the exact solution x=(1/11,7/11)(0.091,0.636)x^\star = (1/11, 7/11) \approx (0.091, 0.636) in exactly 2 steps, matching the theoretical guarantee for a 2×2 system — no matrix inversion was ever performed, only matrix-vector products, which is why CG scales to problems with millions of variables where forming A1A^{-1} directly would be infeasible.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

Try a narrow, elongated bowl and compare the zig-zagging steepest-descent path against a conjugate-direction path: watch how the conjugate path corrects course only once and lands exactly on the minimum instead of oscillating.

zig-zag GD conjugate step: exact in 2
On an elongated quadratic bowl, gradient descent zig-zags across the narrow axis, while conjugate gradient's two carefully chosen directions land exactly on the minimum.

What this means in practice

Conjugate gradient is the workhorse for large-scale quadratic and near-quadratic problems: solving covariance-weighted least-squares in portfolio optimization, large sparse linear systems from PDE-based pricing grids, and as an inner solver inside more complex optimizers. Its appeal is that it needs no Hessian, no matrix inversion, and only a handful of vector operations per step, yet it inherits much of Newton's finite-step guarantee for quadratic problems.

Conjugate gradient chooses each new search direction to be conjugate to (non-interfering with) all previous directions, so it never has to "undo" earlier progress — reaching the exact minimum of an nn-dimensional quadratic problem in at most nn steps, without ever computing a Hessian.

The clean nn-step guarantee holds exactly only for genuinely quadratic objectives; on a non-quadratic function (a real pricing loss, a real likelihood), conjugate gradient is used in its "nonlinear" form, which loses the exact finite-step property and instead just tends to converge much faster than plain gradient descent, especially on narrow, elongated loss surfaces. Applying vanilla linear CG's exact-in-nn-steps claim to a genuinely nonlinear problem is the classic mistake — real problems need periodic restarts of the direction-building process to stay well-behaved.

Related concepts

Practice in interviews

Further reading

  • Shewchuk, An Introduction to the Conjugate Gradient Method Without the Agonizing Pain
  • Nocedal & Wright, Numerical Optimization, ch. 5
ShareTwitterLinkedIn