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 (where is a symmetric positive-definite matrix describing curvature), the method builds a sequence of search directions that are -conjugate, meaning for . In plain English, this is a generalized notion of "at right angles" adapted to the shape of the bowl describes, rather than plain geometric right angles. At each step the algorithm moves along the current direction by the exact optimal amount, then picks the next direction by combining the new gradient with the previous direction:
where is a scalar chosen (by one of a few standard formulas) to enforce conjugacy with . 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 -dimensional quadratic problem, this process finds the exact minimum in at most steps — a hard guarantee gradient descent never gives.
Worked example 1: a simple two-dimensional quadratic
Minimize (a narrow elongated bowl, since is penalized ten times more steeply), starting at . Plain gradient descent zig-zags across the narrow -direction for dozens of iterations. Conjugate gradient, in contrast, is guaranteed to find the exact minimum 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 -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 , since solving is equivalent to minimizing . Suppose and , starting at . The initial gradient (residual) is , so the first direction is . Stepping optimally along and then computing a conjugate second direction takes the solver to the exact solution 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 directly would be infeasible.
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.
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 -dimensional quadratic problem in at most steps, without ever computing a Hessian.
The clean -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--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.
Practice in interviews
Further reading
- Shewchuk, An Introduction to the Conjugate Gradient Method Without the Agonizing Pain
- Nocedal & Wright, Numerical Optimization, ch. 5