Automatic Differentiation
A technique for computing exact derivatives of any program by mechanically applying the chain rule to each elementary operation it performs, without symbolic algebra or finite-difference approximation.
Prerequisites: Gradient Descent, Matrix Calculus and Derivatives
Training a risk model or calibrating a pricing model to market quotes both boil down to the same task: adjust a handful of parameters to minimize an error, which means computing the derivative of that error with respect to every parameter. When the error is the output of a program with thousands of lines and millions of parameters — not a clean textbook formula — you can't just do the calculus by hand, and approximating derivatives by nudging each parameter slightly and re-running the whole program (finite differences) is both slow and imprecise. Automatic differentiation solves this by getting the exact derivative for free, alongside the original computation.
An analogy: a factory with a receipt for every step
Imagine a factory assembly line where every single machine — cutting, bending, welding — logs exactly how its output changes if its input changes by a tiny amount, and pins that "local sensitivity" receipt to the part as it moves down the line. To find out how the finished product changes with the very first input, you don't need to re-derive anything: you just multiply the local sensitivities along the line, machine by machine, using the chain rule. Automatic differentiation (AD) does exactly this to a computer program: it breaks the program into elementary operations (add, multiply, sin, exp, ...), each with a known, trivial derivative, and chains those local derivatives together mechanically.
The idea, one piece at a time
Any program that computes a function is really a sequence of elementary operations, , each depending only on earlier ones — this sequence is called a computational graph. AD comes in two flavors. Forward mode starts at the input, carries a derivative alongside each intermediate value as it's computed, and applies the chain rule forward: if , its derivative with respect to the input is built from the already-known derivatives of and . Reverse mode (the mode behind neural-network backpropagation) instead runs the program forward once to get the final output, then walks backward through the graph, propagating "how much does the final output change per unit change in this intermediate value" — each such quantity is called an adjoint. Reverse mode computes the derivative of one output with respect to all inputs in a single backward pass, which is why it's the standard choice when there are many parameters and one loss value, as in model calibration.
Worked example 1: forward mode by hand
Let , evaluated at , and suppose we want . Forward mode carries a value and a derivative-with-respect-to- for every intermediate quantity. Step 1: , derivative (it's the input we're differentiating against). Step 2: , derivative (constant with respect to ). Step 3: , derivative (product rule, applied locally). Step 4: , derivative . Step 5: , derivative . That final derivative, , matches the exact calculus answer — computed without ever writing down the symbolic formula for .
Worked example 2: reverse mode and a calibration payoff
Now suppose is a one-parameter pricing model, , and we're minimizing squared error at . Forward pass: , wait — try : , ; to make it instructive use : , . Reverse pass starts at the output: . Then . Then , so by the chain rule . A calibration routine uses this single number directly to update in the direction that reduces — no finite-difference re-runs, no symbolic differentiation of as a whole.
What this means in practice
Every modern machine-learning framework (and increasingly, pricing libraries that need fast Greeks) is built on reverse-mode AD: it is exact to machine precision, unlike finite differences, and its cost is roughly independent of the number of inputs, unlike computing one derivative per parameter by hand or by bumping. Quants use it to get thousands of risk sensitivities from one backward pass through a pricing function, and to train models where computing gradients any other way would be computationally hopeless.
Automatic differentiation computes exact derivatives of any program by mechanically chaining the known derivatives of its elementary operations — reverse mode gets the derivative of one output with respect to every input in a single backward pass.
Automatic differentiation is routinely confused with both symbolic differentiation and finite-difference approximation. It is neither: symbolic differentiation produces a formula (which can explode in size) and finite differences only approximate the derivative with rounding and step-size error. AD produces the exact numerical derivative value directly, at a cost close to that of the original computation itself — that combination of exactness and speed is precisely why it replaced both older approaches in practice.
Related concepts
Practice in interviews
Further reading
- Baydin et al., Automatic Differentiation in Machine Learning: A Survey
- Griewank & Walther, Evaluating Derivatives