SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.2.1 The gradient and directional derivativesConcept · 30 min
      2. 4.2.2 The multivariable chain rule and the total differentialConcept · 35 min
      3. 4.2.3 Practice set: gradients and the chain rulePractice set · 20 min
      4. 4.2.4 Case study: attributing P&L to risk factorsCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.2 Gradient and chain rule Lesson 2 of 4

ConceptLesson 4.2.2 in Level 4, Multivariable calculus and optimisation. About 35 minutes.

The multivariable chain rule and the total differential

A path through the plane

When both inputs of f(x,y)f(x, y) depend on a third variable tt, the output depends on tt too. Think of tt as time and of (x(t),y(t))(x(t), y(t)) as a point moving across the plane; z=f(x(t),y(t))z = f(x(t), y(t)) is the height of the surface above it.

Take a rectangle whose width is x(t)=3+2tx(t) = 3 + 2t cm and height y(t)=2−ty(t) = 2 - t cm, tt seconds from now. Its area is A=xyA = xy. At t=0t = 0 it is 3 by 2, its width grows at 2 cm per second, and its height shrinks at 1 cm per second.

Take the two changes one at a time, with the partial derivatives of lesson 4.1.2. With the height held at 2, the growing width adds area at Ax⋅x′=y⋅x′=2×2=4A_x \cdot x' = y \cdot x' = 2 \times 2 = 4 cm² per second. With the width held at 3, the shrinking height changes it at Ay⋅y′=x⋅y′=3×(−1)=−3A_y \cdot y' = x \cdot y' = 3 \times (-1) = -3, so together they give 1 cm² per second.

Expanding confirms it. A(t)=(3+2t)(2−t)=6+t−2t2A(t) = (3 + 2t)(2 - t) = 6 + t - 2t^2 has slope 1−4t1 - 4t, which is 1 at t=0t = 0. Chord slopes from t=0t = 0 give 0.8, 0.98 and 0.998 for steps 0.1, 0.01 and 0.001.

Rule: Chain rule along a path

dzdt=fxdxdt+fydydt\frac{dz}{dt} = f_x \frac{dx}{dt} + f_y \frac{dy}{dt}

One term for each input: its partial derivative times its rate of change. The partials are taken at the moving point (x(t),y(t))(x(t), y(t)). Each term is in units of zz per unit of tt; with one input, this is the chain rule of lesson 2.3.3.

The rates (x′(t),y′(t))(x'(t), y'(t)) form the velocity r′(t)\mathbf{r}'(t) of the moving point, so the rule is a dot product: dzdt=∇f⋅r′(t)\frac{dz}{dt} = \nabla f \cdot \mathbf{r}'(t). At speed 1 this is the directional derivative of lesson 4.2.1; at speed 2 the height changes twice as fast.

A second example

Follow the tilted bowl f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x of lesson 4.2.1 along the path x=t2x = t^2, y=1−ty = 1 - t. At t=1t = 1 the point is (1,0)(1, 0), where fx=2x+y−3=−1f_x = 2x + y - 3 = -1 and fy=x+2y=1f_y = x + 2y = 1. The rates are x′=2t=2x' = 2t = 2 and y′=−1y' = -1:

dzdt=(−1)(2)+(1)(−1)=−3\frac{dz}{dt} = (-1)(2) + (1)(-1) = -3

Substituting first gives the same answer. z(t)=t4−t3−t2−2t+1z(t) = t^4 - t^3 - t^2 - 2t + 1, so z′(t)=4t3−3t2−2t−2z'(t) = 4t^3 - 3t^2 - 2t - 2, and z′(1)=4−3−2−2=−3z'(1) = 4 - 3 - 2 - 2 = -3. Central differences of z(t)z(t) at 1 agree:

step hh0.10.010.001
central difference−2.97-2.97−2.9997-2.9997−2.999997-2.999997

The tilted bowl seen along the path (t2,1−t)(t^2, 1 - t). At t=1t = 1 the velocity points mostly downhill, against ∇f\nabla f, so zz falls at rate −3-3. Where the path runs along a level curve, at t=1.38t = 1.38, zz stops falling: there ∇f⋅r′=0\nabla f \cdot \mathbf{r}' = 0.

Try it

SymPy substitutes the path into ff, then differentiates in tt. The loop prints central differences of z(t)z(t) at 1.

The last line is the chain rule itself: ∇f\nabla f at (1,0)(1, 0) dotted with the velocity (2,−1)(2, -1).

⌘/Ctrl + Enter runs the cell

Pulling together, pulling apart, or not at all

Each term carries its own sign, so the inputs can add to the change or work against each other.

  • Pulling apart. In the rectangle at t=0t = 0, the width adds 4 and the height takes away 3. By t=1t = 1 the rectangle is 5 by 1, so the terms are 1×2=21 \times 2 = 2 and 5×(−1)=−55 \times (-1) = -5: the area now shrinks at 3 cm² per second.
  • Along a level curve. On the circle x=cos⁡tx = \cos t, y=sin⁡ty = \sin t, the function f=x2+y2f = x^2 + y^2 is always 1. The two terms, 2x⋅(−sin⁡t)2x \cdot (-\sin t) and 2y⋅cos⁡t2y \cdot \cos t, cancel exactly: at t=π6t = \frac{\pi}{6} they are −0.866025-0.866025 and 0.8660250.866025.
  • At a flat point. Where ∇f=0\nabla f = \mathbf{0}, dzdt=0\frac{dz}{dt} = 0 whatever the velocity.

The circle is one case of a general fact. A path along a level curve has its velocity at right angles to ∇f\nabla f (lesson 4.2.1), so ∇f⋅r′=0\nabla f \cdot \mathbf{r}' = 0. In the figure, that happens at t=1.38t = 1.38.

When time is an input too

Sometimes tt is one of the inputs itself: z=f(t,y(t))z = f(t, y(t)). Then the rule has a term for tt directly and a term for tt acting through yy.

Rule: Partial and total derivative

dzdt=∂f∂t+∂f∂y dydt\frac{dz}{dt} = \frac{\partial f}{\partial t} + \frac{\partial f}{\partial y}\,\frac{dy}{dt}

The partial ∂f∂t\frac{\partial f}{\partial t} holds yy fixed. The total derivative dzdt\frac{dz}{dt} lets yy move with tt as well.

Take f(t,y)=tyf(t, y) = ty with y=t2y = t^2, so z=t3z = t^3 and dzdt=3t2=3\frac{dz}{dt} = 3t^2 = 3 at t=1t = 1. The partial ft=y=1f_t = y = 1 is only part of it. The rest, fy⋅y′=t⋅2t=2f_y \cdot y' = t \cdot 2t = 2, is the effect of yy moving.

Case study 4.2.4 meets both kinds: an option's value changes with time directly, and through the stock price and volatility, which move as time passes.

Try it

The first line is the rectangle at t=1t = 1, from the chain rule and from A(t)A(t). Then the two cancelling terms on the circle at π6\frac{\pi}{6}.

SymPy checks the total derivative of z=tyz = ty with y=t2y = t^2, against its partial ft=yf_t = y.

⌘/Ctrl + Enter runs the cell

The total differential

Multiply the chain rule by a short time dtdt. The point moves by small steps dxdx and dydy, and the height by about fx dx+fy dyf_x\,dx + f_y\,dy. The same estimate holds for any small steps from a point, whatever path they come from.

Rule: Total differential

Δf≈df=fx dx+fy dy=∇f⋅(dxdy)\begin{gathered} \Delta f \approx df = f_x\,dx + f_y\,dy \\[4pt] = \nabla f \cdot \begin{pmatrix} dx \\ dy \end{pmatrix} \end{gathered}

dxdx and dydy are small steps in xx and yy, in their own units. Δf\Delta f is the true change and dfdf its first-order estimate. With more inputs there is one term per input.

In words: f(a+dx, b+dy)≈f(a,b)+fx dx+fy dyf(a + dx,\, b + dy) \approx f(a, b) + f_x\,dx + f_y\,dy, with the partials taken at (a,b)(a, b). The right side is the tangent plane: the flat surface that touches ff at (a,b)(a, b) with the same slopes. It is the degree-1 copy of lesson 2.7.1, in two variables.

Example 1: the rectangle. Grow the 3 by 2 rectangle by dx=0.1dx = 0.1 and dy=0.05dy = 0.05. Then dA=y dx+x dy=0.2+0.15=0.35dA = y\,dx + x\,dy = 0.2 + 0.15 = 0.35, while the true change is 3.1×2.05−6=0.3553.1 \times 2.05 - 6 = 0.355. The estimate misses only the small corner, dx⋅dy=0.005dx \cdot dy = 0.005.

A rectangle of area xyxy grown by dxdx and dydy, steps drawn large. The total differential y dx+x dyy\,dx + x\,dy counts the two strips; it misses only the corner dx dydx\,dy, whose area falls like the square of the steps.

Halve both steps and the corner, which is the error, falls to a quarter: 0.00125. Halve them again and it is 0.0003125.

Example 2: a distance. Take f(x,y)=x2+y2f(x, y) = \sqrt{x^2 + y^2} at (3,4)(3, 4), where f=5f = 5 and ∇f=(0.6,0.8)\nabla f = (0.6, 0.8) (lesson 4.2.1). For steps dx=0.1dx = 0.1 and dy=−0.2dy = -0.2, df=0.06−0.16=−0.1df = 0.06 - 0.16 = -0.1. So 3.12+3.82≈4.9\sqrt{3.1^2 + 3.8^2} \approx 4.9; the true value is 4.90408.

steps (dx,dy)(dx, dy)(0.1,−0.2)(0.1, -0.2)(0.05,−0.1)(0.05, -0.1)(0.025,−0.05)(0.025, -0.05)(0.0125,−0.025)(0.0125, -0.025)
true change Δf\Delta f−0.09592-0.09592−0.04899-0.04899−0.024749-0.024749−0.012437-0.012437
estimate dfdf−0.1-0.1−0.05-0.05−0.025-0.025−0.0125-0.0125
error Δf−df\Delta f - df0.004080.001010.0002510.0000627

Each halving divides the error by about 4: the ratios are 4.04, 4.02 and 4.01.

Rule: How good the estimate is

Δf−df≈C×(step)2\Delta f - df \approx C \times (\text{step})^2

Near the point the error behaves like the square of the steps, as for the tangent line of lesson 2.7.1. Make every step kk times smaller and the error falls about k2k^2 times. Lesson 4.3.1 finds the constant CC from the second partial derivatives.

Three variables, and where it fails

With three inputs the estimate has three terms. A box 2 by 3 by 4 cm has volume V=xyz=24V = xyz = 24 cm³. Lengthen each side by 0.1 cm: dV=yz dx+xz dy+xy dz=12(0.1)+8(0.1)+6(0.1)=2.6dV = yz\,dx + xz\,dy + xy\,dz = 12(0.1) + 8(0.1) + 6(0.1) = 2.6, against a true change of 2.691.

The estimate fails in two ways. Large steps: from (3,4)(3, 4), the steps (2,−4)(2, -4) reach (5,0)(5, 0), again at distance 5, so the true change is 0; the estimate says −2-2. Corners: the cone at the origin has no partial derivatives (lesson 4.2.1), so it has no tangent plane and no dfdf there.

Try it

The first loop prints the rectangle's true change, its estimate, the error and dx⋅dydx \cdot dy, for the steps above and two halvings. The second loop prints the distance table.

Then the box, and the large step on the distance. 6.27e-05 is Python's way of writing 0.0000627.

⌘/Ctrl + Enter runs the cell

Two inner variables

Now let xx and yy each depend on two variables, ss and tt. Then zz has two partial derivatives, and each follows the chain rule with one of ss, tt moving and the other held fixed.

Rule: Chain rule, two inner variables

∂z∂s=fx xs+fy ys∂z∂t=fx xt+fy yt\begin{gathered} \frac{\partial z}{\partial s} = f_x\,x_s + f_y\,y_s \\[4pt] \frac{\partial z}{\partial t} = f_x\,x_t + f_y\,y_t \end{gathered}

xsx_s is the partial of xx in ss, and so on. As before, one term per input of ff, and the partials of ff are taken at the point (x,y)(x, y).

Example: polar coordinates. A point at distance rr from the origin, at angle θ\theta, has x=rcos⁡θx = r\cos\theta and y=rsin⁡θy = r\sin\theta, as in the polar form of lesson 1.7.3. Take f=xyf = xy, so fx=yf_x = y and fy=xf_y = x. The inner partials are xr=cos⁡θx_r = \cos\theta, yr=sin⁡θy_r = \sin\theta, xθ=−rsin⁡θx_\theta = -r\sin\theta and yθ=rcos⁡θy_\theta = r\cos\theta.

At r=2r = 2, θ=π6\theta = \frac{\pi}{6}, the point is (3,1)≈(1.732051,1)(\sqrt{3}, 1) \approx (1.732051, 1), with cos⁡θ≈0.866025\cos\theta \approx 0.866025 and sin⁡θ=0.5\sin\theta = 0.5. Then

zr=ycos⁡θ+xsin⁡θ=3zθ=−yrsin⁡θ+xrcos⁡θ=2\begin{aligned} z_r &= y\cos\theta + x\sin\theta = \sqrt{3} \\ z_\theta &= -yr\sin\theta + xr\cos\theta = 2 \end{aligned}

In numbers, zr=0.866025+0.866025≈1.732051z_r = 0.866025 + 0.866025 \approx 1.732051 and zθ=−1+3=2z_\theta = -1 + 3 = 2.

Substituting first agrees: z=r2cos⁡θsin⁡θz = r^2\cos\theta\sin\theta, so zr=2rcos⁡θsin⁡θ=3z_r = 2r\cos\theta\sin\theta = \sqrt{3} and zθ=r2(cos⁡2θ−sin⁡2θ)=4×0.5=2z_\theta = r^2(\cos^2\theta - \sin^2\theta) = 4 \times 0.5 = 2.

In matrix form, the row of answers is the gradient, written as a row, times the matrix of inner partials (lesson 3.2.1):

(zrzθ)=(fxfy)(xrxθyryθ)\begin{pmatrix} z_r & z_\theta \end{pmatrix} = \begin{pmatrix} f_x & f_y \end{pmatrix} \begin{pmatrix} x_r & x_\theta \\ y_r & y_\theta \end{pmatrix}

That matrix is called the Jacobian matrix of the map from (r,θ)(r, \theta) to (x,y)(x, y); lesson 4.7.2 uses it to change variables in integrals.

Try it

The gradient (y,x)(y, x) at (3,1)(\sqrt{3}, 1) times the Jacobian matrix gives (zr,zθ)(z_r, z_\theta). Central differences in rr and in θ\theta agree.

SymPy differentiates z=r2cos⁡θsin⁡θz = r^2\cos\theta\sin\theta directly.

⌘/Ctrl + Enter runs the cell

Lesson 4.3.1 adds second-order terms to the total differential. Case study 4.2.4 uses the first-order estimate to split a day's change in an option's value into its causes.

Common mistakes

  1. One term per input. For the rectangle at t=0t = 0, dAdt=4−3=1\frac{dA}{dt} = 4 - 3 = 1; leaving out the height gives 4.
  2. Take the partials at the moving point. At t=1t = 1 the rectangle is 5 by 1, so the terms are 1×21 \times 2 and 5×(−1)5 \times (-1), not the values at t=0t = 0.
  3. ∂\partial and dd are different. For f=tyf = ty with y=t2y = t^2, the partial ftf_t is 1 but dzdt\frac{dz}{dt} is 3.
  4. The total differential is for small steps. Its error grows like the square of the steps: the step (2,−4)(2, -4) on the distance gives −2-2 for a true change of 0.
  5. Steps in the inputs' own units. dxdx must be in the units fxf_x is measured per; mixing centimetres and metres, or percent and decimals, gives nonsense.
  6. No tangent plane at a corner. Where a partial does not exist, neither does dfdf.

The multivariable chain rule and the total differential

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • Along a path, dzdt=fxdxdt+fydydt=∇f⋅r′(t)\frac{dz}{dt} = f_x\frac{dx}{dt} + f_y\frac{dy}{dt} = \nabla f \cdot \mathbf{r}'(t): one term per input, with the partials taken at the moving point.
  • The terms can add or cancel. Along a level curve the velocity is at right angles to ∇f\nabla f, so dzdt=0\frac{dz}{dt} = 0.
  • When tt is an input itself, dzdt=∂f∂t+∂f∂ydydt\frac{dz}{dt} = \frac{\partial f}{\partial t} + \frac{\partial f}{\partial y}\frac{dy}{dt}: the partial holds the other inputs fixed, the total derivative does not.
  • Total differential: Δf≈df=fx dx+fy dy\Delta f \approx df = f_x\,dx + f_y\,dy, the tangent-plane estimate. Its error falls like the square of the steps, and it fails for large steps and at corners.
  • With two inner variables, zs=fxxs+fyysz_s = f_x x_s + f_y y_s and zt=fxxt+fyytz_t = f_x x_t + f_y y_t: the gradient, as a row, times the Jacobian matrix.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.