Unit 14, lesson 110

Differentiable pricing and automatic differentiation

How automatic differentiation applies the chain rule to a pricing computational graph in forward and reverse mode, when pathwise derivatives may be exchanged with expectation, and why a digital payoff breaks that exchange.

  • Video 110
  • Differentiation, calibration and validation

A Monte Carlo pricer turns a normal draw into a terminal stock price, a call payoff and a discounted value. If the interest rate changes, how does the change travel through those steps, given that the rate enters both the growth of the stock and the discount factor? This lesson writes one simulated path as a computational graph, differentiates it in forward and reverse mode, and then asks when the average of these path derivatives is the derivative of the price. The answer is yes for the delta of a call under an integrable bound, and no for a digital option, whose sensitivity lives entirely at the strike.

Key terms

Computational graph

Definition A computational graph of a program is a directed acyclic graph whose nodes are the inputs, the intermediate values and the outputs of the computation, with an edge from viv_i to vjv_j when vjv_j is computed by an elementary operation that uses viv_i. Each edge carries the local partial derivative ∂vj/∂vi\partial v_j/\partial v_i of that operation, evaluated at the computed values. If every operation is differentiable at those values, the chain rule says that the derivative of an output with respect to an input is the sum, over all directed paths from the input to the output, of the product of the local derivatives along each path. For one simulated terminal value of a stock without dividends in the Black-Scholes model under a pricing measure Q\mathbb{Q}, fix a realization zz of the standard normal variable WTQ/TW^{\mathbb{Q}}_T/\sqrt{T} and compute

a=(r−12σ2)T+σT z,u=ea,ST=S0 u,P=(ST−K)+,D=e−rT,Y=DP,\begin{aligned} a &= \bigl(r - \tfrac12 \sigma^2\bigr) T + \sigma \sqrt{T}\, z, \\[0.4em] u &= e^{a}, \qquad S_T = S_0\, u, \\[0.4em] P &= (S_T - K)^{+}, \\[0.4em] D &= e^{-rT}, \qquad Y = D P, \end{aligned}

with inputs S0S_0, rr and σ\sigma, and with KK, TT and zz held fixed. The rate reaches the discounted payoff YY along two paths, one through the exponent aa and one through the discount factor DD.

In plain words The graph is the recipe of the computation drawn as a network. It records which quantities feed which, so that every route by which an input affects the result can be found and none is forgotten.

Example Take the constructed inputs S0=100S_0 = 100, K=102K = 102, T=1T = 1 year, r=0.03r = 0.03, σ=0.2\sigma = 0.2 and z=0.4z = 0.4. Then a=0.03−0.02+0.08=0.09a = 0.03 - 0.02 + 0.08 = 0.09, ST≈109.4174S_T \approx 109.4174, D≈0.970446D \approx 0.970446, P≈7.4174P \approx 7.4174 and Y≈7.1982Y \approx 7.1982. The path through the exponent contributes the product T⋅u⋅S0⋅1⋅D=DSTT≈106.1837T \cdot u \cdot S_0 \cdot 1 \cdot D = D S_T T \approx 106.1837, and the path through the discount factor contributes (−TD)⋅P=−TY≈−7.1982(-TD) \cdot P = -TY \approx -7.1982. On this path, which ends in the money, the total is

∂Y∂r=DT(ST−P)=DKT≈98.9854.\begin{aligned} \frac{\partial Y}{\partial r} &= D T (S_T - P) \\[0.4em] &= D K T \approx 98.9854 . \end{aligned}

The stock terms cancel, and dropping either route gives a wrong derivative.

Automatic differentiation

Definition Automatic differentiation (AD) evaluates derivatives of a function defined by a program by applying the chain rule to the program’s elementary operations, each of which has a known local derivative. It uses no step size. At an input where every operation is differentiable at its computed arguments and every branch taken stays the same for all nearby inputs, it returns the derivative of the function that the program computes, up to floating-point rounding. In forward mode a seed vector θ˙\dot\theta of input tangents is pushed through the graph in the order of computation,

v˙j=∑i : i→j∂vj∂vi v˙i,\dot v_j = \sum_{i \,:\, i \to j} \frac{\partial v_j}{\partial v_i}\, \dot v_i ,

and one sweep returns Jθ˙\mathcal{J}\dot\theta, the directional derivative of every output, where J\mathcal{J} is the Jacobian of the outputs with respect to the inputs. A sweep costs a small constant multiple of one evaluation, and a full Jacobian with nn inputs takes nn forward sweeps. AD differentiates the computation, not the price it approximates. Time discretization, the Monte Carlo sample, exercise decisions and fitted parameters are all part of what is differentiated. If a model parameter is calibrated to market quotes, a sensitivity to a quote needs the dependence of the fit on the quote, obtained either by differentiating through the calibration or by the implicit function theorem applied to its optimality conditions, when these are continuously differentiable and their Jacobian with respect to the parameters is invertible. Holding the fitted parameter fixed answers a different question.

In plain words Forward mode follows a small change of one input through the computation, step by step, and reads off how much every result moves per unit of change. Unlike a finite difference it never actually changes the input, so there is no step to choose and no truncation error. It differentiates exactly what the code computes, approximations included.

Example On the constructed path, seed r˙=1\dot r = 1 with S˙0=σ˙=0\dot S_0 = \dot\sigma = 0. Then a˙=T=1\dot a = T = 1, u˙=u a˙≈1.094174\dot u = u\,\dot a \approx 1.094174, S˙T=S0 u˙≈109.4174\dot S_T = S_0\, \dot u \approx 109.4174 and P˙=S˙T≈109.4174\dot P = \dot S_T \approx 109.4174 because the path ends in the money, while D˙=−TD≈−0.970446\dot D = -TD \approx -0.970446. The product rule at Y=DPY = DP gives

Y˙=D˙ P+D P˙≈−7.1982+106.1837≈98.9854.\begin{aligned} \dot Y &= \dot D\, P + D\, \dot P \\[0.4em] &\approx -7.1982 + 106.1837 \\[0.4em] &\approx 98.9854 . \end{aligned}

A second sweep seeded with σ˙=1\dot\sigma = 1 starts from a˙=Tz−σT=0.2\dot a = \sqrt{T} z - \sigma T = 0.2 and returns Y˙≈21.2367\dot Y \approx 21.2367. Two inputs of interest take two forward sweeps.

Reverse-mode adjoint

Definition For a scalar output yy, the adjoint of a node viv_i is vˉi=∂y/∂vi\bar v_i = \partial y/\partial v_i, the rate at which yy changes when viv_i changes and the change is passed through every operation that uses viv_i. Seeding yˉ=1\bar y = 1 and visiting the nodes in reverse order of computation,

vˉi=∑j : i→jvˉj ∂vj∂vi,\bar v_i = \sum_{j \,:\, i \to j} \bar v_j\, \frac{\partial v_j}{\partial v_i},

where the sum runs over all edges leaving viv_i. The adjoints of the inputs form the gradient of yy. One reverse sweep costs a small constant multiple of one evaluation whatever the number of inputs, so it suits a price with many inputs, while forward mode suits few inputs and many outputs. Reverse mode needs the intermediate values of the forward computation, which it stores or recomputes, and checkpointing stores some of them and recomputes the rest to save memory. A node used in several places must add the contributions of all its edges, and overwriting one with another drops part of the chain rule. Iterative solvers and branches need their own treatment, for example by differentiating the converged equations instead of every iteration.

In plain words Reverse mode starts from the price and works backward, asking for each intermediate value how much the price would move per unit change in it. Each node collects a contribution from every place its value is used, so one backward pass gives the sensitivity to every input at once.

Example On the constructed path, Y=DPY = DP sends Dˉ=P≈7.417428\bar D = P \approx 7.417428 and Pˉ=D≈0.970446\bar P = D \approx 0.970446. The positive part passes Pˉ\bar P times the in-the-money indicator, so SˉT≈0.970446\bar S_T \approx 0.970446. From ST=S0uS_T = S_0 u come Sˉ0=SˉT u≈1.061837\bar S_0 = \bar S_T\, u \approx 1.061837 and uˉ=SˉT S0≈97.0446\bar u = \bar S_T\, S_0 \approx 97.0446, and the exponential gives aˉ=uˉ u≈106.1837\bar a = \bar u\, u \approx 106.1837. The exponent sends σˉ=aˉ (Tz−σT)≈21.2367\bar\sigma = \bar a\,(\sqrt{T} z - \sigma T) \approx 21.2367 and a contribution aˉ T≈106.1837\bar a\, T \approx 106.1837 to the rate, and the discount factor adds Dˉ⋅(−TD)≈−7.1982\bar D \cdot (-TD) \approx -7.1982, so rˉ≈98.9854\bar r \approx 98.9854. One sweep returns all three sensitivities, and they agree with the forward sweeps.

Pathwise derivative

Definition Let Y(θ)=Y(θ,ω)Y(\theta) = Y(\theta, \omega) be a discounted payoff computed from a parameter θ\theta and random inputs ω\omega, here the normal draw. The pathwise derivative at θ\theta is ∂Y(θ,ω)/∂θ\partial Y(\theta, \omega)/\partial\theta with ω\omega held fixed, wherever it exists. For the call path with σ>0\sigma > 0, T>0T > 0 and ST≠KS_T \ne K,

∂Y∂S0=Du 1{ST>K},∂Y∂σ=DST(Tz−σT)×1{ST>K},∂Y∂r=DKT 1{ST>K},\begin{aligned} \frac{\partial Y}{\partial S_0} &= D u\, \mathbf{1}_{\{S_T > K\}}, \\[0.4em] \frac{\partial Y}{\partial \sigma} &= D S_T \bigl(\sqrt{T} z - \sigma T\bigr) \\[0.4em] &\qquad \times \mathbf{1}_{\{S_T > K\}}, \\[0.4em] \frac{\partial Y}{\partial r} &= D K T\, \mathbf{1}_{\{S_T > K\}}, \end{aligned}

measured in currency per currency unit of spot, currency per unit of decimal volatility and currency per unit of decimal rate. On paths with ST<KS_T < K all three are zero, and at ST=KS_T = K the payoff has a kink and no derivative. The pathwise estimator of a price sensitivity V′(θ)V'(\theta) is the average of the path derivatives over independent draws. It estimates the right quantity only when the derivative and the expectation can be exchanged, the subject of the next card. Central differences computed with the same draws check a path derivative, while fresh draws for each bumped input would mix a change of path into the difference.

In plain words Keep the random numbers fixed and ask how this one simulated payoff responds to an input. It is the derivative of a known function and needs no new simulation.

Example On the constructed path, ∂Y/∂S0=e−0.03+0.09=e0.06≈1.061837\partial Y/\partial S_0 = e^{-0.03 + 0.09} = e^{0.06} \approx 1.061837, ∂Y/∂σ≈21.2367\partial Y/\partial\sigma \approx 21.2367 and ∂Y/∂r≈98.9854\partial Y/\partial r \approx 98.9854. To first order, one volatility point, a change of 0.010.01 in σ\sigma, moves this path’s payoff by about 0.21240.2124, and one basis point of rate moves it by about 0.00990.0099. Central differences with the same zz and step 10−410^{-4} give 1.06183651.0618365, 21.23673121.236731 and 98.9854498.98544. With step 0.010.01 the volatility difference gives about 21.235721.2357, off in the fifth significant digit because of its step error.

Interchange of derivative and expectation

Definition Let V(θ)=EQ[Y(θ)]V(\theta) = \mathbb{E}^{\mathbb{Q}}[Y(\theta)] for θ\theta in an open interval, with each Y(θ)Y(\theta) integrable, and fix θ0\theta_0 in it. Suppose that (i) with probability one, θ↦Y(θ)\theta \mapsto Y(\theta) is differentiable at θ0\theta_0, and (ii) there are ε>0\varepsilon > 0 and an integrable random variable MM such that, for each θ\theta with ∣θ−θ0∣<ε\lvert \theta - \theta_0 \rvert < \varepsilon, the bound ∣Y(θ)−Y(θ0)∣≤M∣θ−θ0∣\lvert Y(\theta) - Y(\theta_0) \rvert \le M \lvert \theta - \theta_0 \rvert holds with probability one. Then VV is differentiable at θ0\theta_0 and

V′(θ0)=EQ[∂Y∂θ(θ0)].V'(\theta_0) = \mathbb{E}^{\mathbb{Q}}\Bigl[\frac{\partial Y}{\partial \theta}(\theta_0)\Bigr].

The proof applies dominated convergence to the difference quotients along any sequence θn→θ0\theta_n \to \theta_0. For the call delta, x↦(xu−K)+x \mapsto (x u - K)^{+} is Lipschitz with constant uu, so condition (ii) holds with M=DuM = D u, and EQ[M]=e−rT EQ[ea]=1\mathbb{E}^{\mathbb{Q}}[M] = e^{-rT}\, \mathbb{E}^{\mathbb{Q}}[e^{a}] = 1. Condition (i) holds because STS_T has a continuous law when σ>0\sigma > 0 and T>0T > 0, so the kink ST=KS_T = K has probability zero. Hence

Δ=EQ[Du 1{ST>K}]=Φ(d1),\begin{aligned} \Delta &= \mathbb{E}^{\mathbb{Q}}\bigl[D u\, \mathbf{1}_{\{S_T > K\}}\bigr] \\[0.4em] &= \Phi(d_1), \end{aligned}

with

d1=1σT[ln⁡S0K+(r+12σ2)T],d2=d1−σT.\begin{aligned} d_1 &= \frac{1}{\sigma\sqrt{T}}\Bigl[\ln\frac{S_0}{K} \\[0.4em] &\qquad + \bigl(r + \tfrac12\sigma^2\bigr)T\Bigr], \\[0.4em] d_2 &= d_1 - \sigma\sqrt{T}. \end{aligned}

For vega and rho similar bounds hold for θ\theta in a small closed interval, because the exponential of a normal variable times a polynomial in it is integrable, and the averages of the path derivatives are S0φ(d1)TS_0 \varphi(d_1)\sqrt{T} and KTe−rTΦ(d2)K T e^{-rT}\Phi(d_2). In other models a kink of probability zero is not enough by itself, and the integrable bound is what carries the argument. If σ=0\sigma = 0, the terminal price S0erTS_0 e^{rT} is deterministic, and the price (S0−Ke−rT)+(S_0 - K e^{-rT})^{+} has no derivative at S0=Ke−rTS_0 = K e^{-rT}, where the kink has probability one.

In plain words The average of the path derivatives is the derivative of the price when a small change in the input moves every path payoff by at most a proportional amount whose average is finite. A kink that paths hit with probability zero passes this test, and a jump does not.

Example For the constructed inputs d1≈0.150987d_1 \approx 0.150987 and d2≈−0.049013d_2 \approx -0.049013, so Δ=Φ(d1)≈0.560007\Delta = \Phi(d_1) \approx 0.560007, the vega is S0φ(d1)≈39.4421S_0 \varphi(d_1) \approx 39.4421 and the rho is KTe−rTΦ(d2)≈47.5580K T e^{-rT}\Phi(d_2) \approx 47.5580. A run of four million draws with a fixed seed gives pathwise averages of about 0.559930.55993, 39.437739.4377 and 47.551847.5518, with standard errors of about 0.00030.0003, 0.0370.037 and 0.0250.025, consistent with these values. A single path can be far from the average. For z=−1z = -1 all three path derivatives are zero, while the option’s delta is about 0.56000.5600.

Discontinuous digital payoff

Definition A cash-or-nothing digital call pays 11 currency unit at TT if ST>KS_T > K. With zz fixed, its discounted path payoff Y=D 1{S0u>K}Y = D\, \mathbf{1}_{\{S_0 u > K\}} is piecewise constant in S0S_0, so ∂Y/∂S0=0\partial Y/\partial S_0 = 0 on every path with ST≠KS_T \ne K, that is with probability one, and AD through the indicator returns a zero delta on every path. The price V=e−rTΦ(d2)V = e^{-rT}\Phi(d_2) has

∂V∂S0=e−rTφ(d2)S0σT>0,\frac{\partial V}{\partial S_0} = \frac{e^{-rT}\varphi(d_2)}{S_0 \sigma \sqrt{T}} > 0,

so the interchange fails. Condition (ii) of the previous card is what breaks. A bump hh of spot changes the payoff by DD on the event that STS_T crosses KK, whose probability is of order ∣h∣\lvert h \rvert, so the difference quotient equals D/∣h∣D/\lvert h \rvert on that event, and no integrable MM bounds these quotients for all small hh. The change in price comes from probability mass that moves across the strike, which the path derivative does not see. Methods that handle it under their own assumptions include the likelihood ratio method, which differentiates the lognormal density instead of the payoff and in this model gives

∂V∂S0=EQ[YzS0σT],\frac{\partial V}{\partial S_0} = \mathbb{E}^{\mathbb{Q}}\Bigl[\frac{Y z}{S_0 \sigma \sqrt{T}}\Bigr],

conditioning on part of the randomness to get a smooth conditional expectation, smoothing the indicator at the cost of a bias, and analytic boundary terms. The same failure affects the second derivatives of a call. The pathwise delta Du 1{ST>K}D u\, \mathbf{1}_{\{S_T > K\}} jumps at the strike, so differentiating it again path by path returns zero on every path, while the gamma φ(d1)/(S0σT)\varphi(d_1)/(S_0 \sigma \sqrt{T}) is positive. A valid first-order pathwise derivative does not make the second-order one valid.

In plain words An indicator jumps, so a small bump of spot changes almost no paths and changes those few by a whole unit of payoff. Path derivatives see only the flat parts, and the entire sensitivity of the price sits in the jumps.

Example For a constructed digital with K=102K = 102 and the other inputs as before, V≈0.466255V \approx 0.466255 and its delta is about 0.0193340.019334 per currency unit of spot. On the path z=0.4z = 0.4 it pays D≈0.970446D \approx 0.970446, and the payoff does not change until spot falls to 102e−0.09≈93.2210102 e^{-0.09} \approx 93.2210, so every small bump gives a zero difference. A run of four million draws gives a likelihood ratio estimate of about 0.0193310.019331 with standard error 0.0000140.000014. For the call with the same inputs the gamma is about 0.0197210.019721, and differentiating the pathwise delta again returns zero.

Check yourself

  1. On the constructed path, change the draw to z=−1z = -1. Find the terminal price, the path payoff and the three path derivatives. Does it follow that the delta of the option is zero?
Show the answer

The exponent is a=0.03−0.02−0.2=−0.19a = 0.03 - 0.02 - 0.2 = -0.19, so ST=100e−0.19≈82.6959S_T = 100 e^{-0.19} \approx 82.6959, below the strike 102102. The payoff is 00 and stays 00 for small changes of S0S_0, σ\sigma and rr, so all three path derivatives are zero. The delta of the option is not zero. By the interchange result it is the expectation of the path derivative over all draws,

Δ=Φ(d1)≈0.5600,\Delta = \Phi(d_1) \approx 0.5600,

and under Q\mathbb{Q} the path ends in the money with probability Φ(d2)≈0.4805\Phi(d_2) \approx 0.4805. A single path derivative is one draw of the pathwise estimator, not the derivative of the price.

  1. In a hand-written reverse sweep for the constructed path, the line for the discount factor sets rˉ=Dˉ⋅(−TD)\bar r = \bar D \cdot (-TD) instead of adding to it, and it runs after the exponent has set rˉ=aˉ T\bar r = \bar a\, T. What rate sensitivity does the code report, and how would a test catch the bug?
Show the answer

The code reports rˉ≈−7.1982\bar r \approx -7.1982, the discount route alone. With the two lines in the other order it would report aˉ T≈106.1837\bar a\, T \approx 106.1837, the exponent route alone. The correct adjoint adds both,

rˉ=aˉ T+Dˉ⋅(−TD)=DKT≈98.9854.\begin{aligned} \bar r &= \bar a\, T + \bar D \cdot (-TD) \\[0.4em] &= DKT \approx 98.9854 . \end{aligned}

A central difference in rr with the same draw z=0.4z = 0.4 and step 10−410^{-4} gives 98.9854498.98544 and exposes the error at once. For many draws, the average of the correct pathwise rho can also be compared with the Black-Scholes rho KTe−rTΦ(d2)≈47.5580K T e^{-rT}\Phi(d_2) \approx 47.5580.

  1. A desk prices the constructed digital by Monte Carlo and gets its delta by AD through the comparison ST>KS_T > K in the code. What delta does it get, which hypothesis fails, and how can the delta be estimated correctly?
Show the answer

Every path contributes a zero derivative, so the desk gets a delta of 00, while the true delta is e−rTφ(d2)/(S0σT)≈0.019334e^{-rT}\varphi(d_2)/(S_0 \sigma \sqrt{T}) \approx 0.019334. Condition (i) holds, since the paths miss the strike with probability one, but condition (ii) fails, because the payoff jumps by DD and no integrable bound controls the difference quotients near the strike. The likelihood ratio estimator averages Yz/(S0σT)=Yz/20Y z/(S_0 \sigma\sqrt{T}) = Y z/20 and gave about 0.0193310.019331 with standard error 0.0000140.000014 from four million draws. A smoothed payoff, for example one that rises linearly from 00 to 11 as STS_T moves across [K−w,K+w][K - w, K + w], is Lipschitz, so the interchange applies to it, but it is a different payoff. Its delta has a bias that vanishes as w→0w \to 0, while the variance of its pathwise estimator grows like 1/w1/w. In this model the analytic formula gives the answer directly.

How this lesson connects