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 1 of 4

ConceptLesson 4.2.1 in Level 4, Multivariable calculus and optimisation. About 30 minutes.

The gradient and directional derivatives

The gradient

Lesson 4.1.2 measured the slope of a surface z=f(x,y)z = f(x, y) along the two axes. The partial derivative fxf_x is the slope of the slice with yy held fixed, and fyf_y the slope with xx held fixed. The gradient collects both into one vector.

Rule: Gradient

∇f=(fxfy)\nabla f = \begin{pmatrix} f_x \\ f_y \end{pmatrix}

Read ∇f\nabla f as "grad f". It is a vector at each point (x,y)(x, y), written as a column like the vectors of Level 3. Each component is in units of ff per unit of its own variable.

Take f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x, a tilted bowl. Its partials are fx=2x+y−3f_x = 2x + y - 3 and fy=x+2yf_y = x + 2y. At the point (0,1)(0, 1) the height is f=1f = 1, and

∇f(0,1)=(0+1−30+2)=(−22)\nabla f(0, 1) = \begin{pmatrix} 0 + 1 - 3 \\ 0 + 2 \end{pmatrix} = \begin{pmatrix} -2 \\ 2 \end{pmatrix}

So, heading along the xx-axis from (0,1)(0, 1), the surface falls 2 units per unit step. Heading along the yy-axis it rises 2 per unit step.

Three or more variables

A function of three variables has three partials, so its gradient has three components. For g(x,y,z)=x2+yzg(x, y, z) = x^2 + yz at (1,2,3)(1, 2, 3), the partials are gx=2x=2g_x = 2x = 2, gy=z=3g_y = z = 3 and gz=y=2g_z = y = 2. So ∇g(1,2,3)=(2,3,2)\nabla g(1, 2, 3) = (2, 3, 2).

With nn inputs, the gradient is a vector in Rn\mathbb{R}^n.

Try it

gradient estimates each partial by a central difference (lesson 4.1.2): it bumps one input at a time by hh, both ways. It works for any number of inputs.

np.asarray(p, dtype=float) matters. In an array of whole numbers, a bump of 0.00001 would be rounded away.

⌘/Ctrl + Enter runs the cell

The slope in any direction

From a point p\mathbf{p} you can walk off in any direction, not only along the axes. Give the direction as a unit vector u\mathbf{u} (lesson 3.1.2). The points p+hu\mathbf{p} + h\mathbf{u} then form a straight line through p\mathbf{p}, each at distance ∣h∣|h| from it.

The height along that line is a function of one number, hh. Its slope at h=0h = 0 is the directional derivative of ff at p\mathbf{p} in the direction u\mathbf{u}, written Duf(p)D_{\mathbf{u}} f(\mathbf{p}).

For the tilted bowl at p=(0,1)\mathbf{p} = (0, 1), take u=(0.6,0.8)\mathbf{u} = (0.6, 0.8), a vector of length 1. Here are chord slopes (lesson 2.2.1) from both sides:

step hh0.10.010.001
ahead, to p+hu\mathbf{p} + h\mathbf{u}0.5480.41480.40148
behind, to p−hu\mathbf{p} - h\mathbf{u}0.2520.38520.39852

Both rows settle on 0.4. Algebra agrees: expanding gives f(0.6h, 1+0.8h)−f(0,1)=0.4h+1.48h2f(0.6h,\, 1 + 0.8h) - f(0, 1) = 0.4h + 1.48h^2. So the chord slope is 0.4+1.48h0.4 + 1.48h, which tends to 0.4.

The dot product gives the same number in one line: ∇f⋅u=(−2)(0.6)+(2)(0.8)=0.4\nabla f \cdot \mathbf{u} = (-2)(0.6) + (2)(0.8) = 0.4.

Rule: Directional derivative

Duf=∇f⋅u=fxu1+fyu2\begin{gathered} D_{\mathbf{u}} f = \nabla f \cdot \mathbf{u} \\[4pt] = f_x u_1 + f_y u_2 \end{gathered}

The slope of ff per unit of distance in the direction of the unit vector u\mathbf{u}. A step hh along u\mathbf{u} moves xx by hu1hu_1 and yy by hu2hu_2. Each move changes ff at the rate of its own partial, and the two changes add.

The partials are special cases. The direction e1=(1,0)\mathbf{e}_1 = (1, 0) gives ∇f⋅e1=fx\nabla f \cdot \mathbf{e}_1 = f_x, and e2=(0,1)\mathbf{e}_2 = (0, 1) gives fyf_y. In three variables the dot product has three terms: for gg above, along 13(1,2,2)\frac{1}{3}(1, 2, 2) the slope is 2+6+43=4\frac{2 + 6 + 4}{3} = 4.

The direction must be a unit vector

A direction often comes as an angle or as an arrow of any length. For an angle α\alpha (alpha) from the positive xx-axis, use u=(cos⁡α,sin⁡α)\mathbf{u} = (\cos\alpha, \sin\alpha), a point on the unit circle. For an arrow such as (3,4)(3, 4), normalise it first (lesson 3.1.2): 15(3,4)=(0.6,0.8)\frac{1}{5}(3, 4) = (0.6, 0.8).

Skipping that step is costly. ∇f⋅(3,4)=−6+8=2\nabla f \cdot (3, 4) = -6 + 8 = 2, five times the slope, because a step of hh along (3,4)(3, 4) covers a distance of 5h5h.

The slope is in units of ff per unit of distance. Distance in the plane only makes sense when xx and yy are measured in the same units.

Try it

The loop prints the chord slopes of the table, ahead and behind. The next line is ∇f⋅u\nabla f \cdot \mathbf{u}, from the gradient (−2,2)(-2, 2).

The last line shows the mistake: dotting with (3,4)(3, 4) instead of its unit vector gives 2, not 0.4.

⌘/Ctrl + Enter runs the cell

Steepest ascent and steepest descent

Since u\mathbf{u} has length 1, the angle rule of lesson 3.1.2 gives ∇f⋅u=∥∇f∥cos⁡θ\nabla f \cdot \mathbf{u} = \lVert \nabla f \rVert \cos\theta. Here θ\theta (theta) is the angle between u\mathbf{u} and ∇f\nabla f. So the slope depends only on how far u\mathbf{u} turns away from the gradient.

At (0,1)(0, 1) the gradient (−2,2)(-2, 2) points at 135° from the xx-axis. Its length is 22≈2.82842\sqrt{2} \approx 2.8284. Turning u\mathbf{u} round the circle gives these slopes:

direction α\alpha0°45°90°135°180°225°270°315°
slope ∇f⋅u\nabla f \cdot \mathbf{u}−2-2022.828420−2-2−2.8284-2.8284

The largest slope is at 135°, along the gradient. The most negative is at 315°, straight against it. Halfway between, at 45° and 225°, the slope is 0.

Rule: Steepest slopes

−∥∇f∥≤∇f⋅u≤∥∇f∥largest along ∇fsmallest along −∇f\begin{gathered} -\lVert \nabla f \rVert \le \nabla f \cdot \mathbf{u} \le \lVert \nabla f \rVert \\[4pt] \text{largest along } \nabla f \\[2pt] \text{smallest along } -\nabla f \end{gathered}

The gradient points straight uphill, and its length is the steepest slope. Against it is steepest descent, with slope −∥∇f∥-\lVert \nabla f \rVert, and at right angles the slope is 0. The bounds are the Cauchy–Schwarz inequality of lesson 3.1.2.

The slope at (0,1)(0, 1) in every direction. It peaks at 2.83, the length of ∇f\nabla f, in the direction of ∇f\nabla f, and is 0 along the level curve, at right angles to it.

Directions the same angle either side of the gradient have the same slope. 90° and 180° both give 2, since each is 45° from 135°.

Lesson 4.4.2 uses steepest descent to find minima: take a small step against the gradient, then repeat.

The gradient crosses level curves at right angles

A level curve (lesson 4.1.1) joins points of equal height. Walking along it, ff does not change, so the slope along the curve is 0. By the rule, the curve's direction is at right angles to ∇f\nabla f.

At (0,1)(0, 1) the level curve f=1f = 1 leaves at 45° and 225°, the two zeros in the table. It is a closed curve round the bottom of the bowl, and passes through (2,1)(2, 1) too, where ff is also 1.

A second example. Take the bowl f(x,y)=x2+2y2f(x, y) = x^2 + 2y^2 at (2,1)(2, 1). There f=6f = 6 and ∇f=(2x,4y)=(4,4)\nabla f = (2x, 4y) = (4, 4). The level curve x2+2y2=6x^2 + 2y^2 = 6 is an ellipse, and at (2,1)(2, 1) it heads in the direction (1,−1)(1, -1): the nearby point (2.1,0.891628)(2.1, 0.891628) is still at height 6.

The dot product (4,4)⋅(1,−1)=0(4, 4) \cdot (1, -1) = 0 confirms the right angle. The steepest slope is ∥(4,4)∥=42≈5.6569\lVert (4, 4) \rVert = 4\sqrt{2} \approx 5.6569, in the direction (0.7071,0.7071)(0.7071, 0.7071).

Uphill does not have to point straight away from the bottom. The bottom of this bowl is (0,0)(0, 0), so straight away from it is the direction (2,1)(2, 1); the gradient points along (1,1)(1, 1).

Gradient arrows on a grid of points of the tilted bowl, all drawn at the same scale. Each points uphill at right angles to the level curves around it. The arrows are longest where the curves crowd together, and vanish at the lowest point (2,−1)(2, -1).

Try it below. The dark arrow is the gradient and the dashed arrow your direction u\mathbf{u}.

Turn the direction until the slope peaks, and check that the peak is the gradient's length. Then turn it along the highlighted level curve, where the slope is 0, and move the point to try again.

Explore

The arrow is the gradient: it points uphill, at right angles to the level curve. Turn the direction and watch the slope in that direction: largest along the gradient, zero along the curve.

−2024−4−202
Point
(0, 1), f = 1
Gradient ∇f (dark arrow)
(−2, 2), length 2.828
Slope at 0°
∇f · u = −2: downhill

Try it

The first line steps along the ellipse from (2,1)(2, 1): each point keeps height 6. Then the dot product of the gradient with the curve's direction, and the steepest slope with its direction.

np.linalg.norm is the length of lesson 3.1.2.

⌘/Ctrl + Enter runs the cell

Where the gradient is zero, and where there is none

A zero gradient. For the tilted bowl, 2x+y−3=02x + y - 3 = 0 and x+2y=0x + 2y = 0 both hold at (2,−1)(2, -1), so ∇f(2,−1)=(0,0)\nabla f(2, -1) = (0, 0). Then ∇f⋅u=0\nabla f \cdot \mathbf{u} = 0 in every direction: the surface is flat there, with no uphill direction at all.

That point is the bottom of the bowl, where f(2,−1)=−3f(2, -1) = -3. A flat point can also be a top or a saddle; lesson 4.3.2 tells them apart.

No gradient. The cone f(x,y)=x2+y2f(x, y) = \sqrt{x^2 + y^2} gives each point's distance from the origin. Its slice along y=0y = 0 is ∣x∣|x|, which has a corner at 0 (lesson 2.2.2). So fxf_x does not exist at the origin, and neither does the gradient.

Walking out of the origin, the cone rises with slope 1 in every direction. No vector can do that through a dot product: if ∇f⋅u=1\nabla f \cdot \mathbf{u} = 1, then ∇f⋅(−u)=−1\nabla f \cdot (-\mathbf{u}) = -1.

Away from the origin there is no corner. By the chain rule (lesson 2.3.3), fx=xx2+y2f_x = \frac{x}{\sqrt{x^2 + y^2}} and fy=yx2+y2f_y = \frac{y}{\sqrt{x^2 + y^2}}. At (3,4)(3, 4) that gives ∇f=(0.6,0.8)\nabla f = (0.6, 0.8), of length 1: the steepest slope of a cone is 1.

Try it

The first line is the gradient of the tilted bowl at (2,−1)(2, -1), by central differences: zero, after rounding.

Then the cone: from the origin, the slope out along four directions is 1 each time. The last line is the slice ∣x∣|x|: slope 1 to the right of 0 and −1-1 to the left.

⌘/Ctrl + Enter runs the cell

The gradient appears again in the rest of this module. Lesson 4.2.2 follows ff along a curved path with it. Case study 4.2.4 uses the same first-order slopes to explain a day's change in an option's value.

Common mistakes

  1. Normalise the direction. Along (3,4)(3, 4), the tilted bowl at (0,1)(0, 1) has slope 0.4, not ∇f⋅(3,4)=2\nabla f \cdot (3, 4) = 2.
  2. The steepest slope is the gradient's length, not the sum of its components. At (0,1)(0, 1) it is 222\sqrt{2}, not −2+2=0-2 + 2 = 0.
  3. Put the point into the partials. (2x+y−3,  x+2y)(2x + y - 3,\; x + 2y) is the gradient everywhere; at (0,1)(0, 1) it is the vector (−2,2)(-2, 2).
  4. Steepest descent is along −∇f-\nabla f, with slope −∥∇f∥-\lVert \nabla f \rVert: the opposite direction, not a different one.
  5. The gradient crosses level curves; it never runs along them. Along a level curve the slope is 0.
  6. Uphill need not point away from the bottom. On x2+2y2x^2 + 2y^2 at (2,1)(2, 1) the gradient is along (1,1)(1, 1), not (2,1)(2, 1).
  7. No gradient at a corner. The cone rises with slope 1 out of the origin in every direction, but has no gradient there.

The gradient and directional derivatives

0 of 11 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
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

  • The gradient ∇f=(fx,fy)\nabla f = (f_x, f_y) stacks the partial derivatives into a vector at each point; in nn variables it has nn components.
  • The slope in the direction of a unit vector u\mathbf{u} is Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}. Normalise an arrow first; for an angle α\alpha, u=(cos⁡α,sin⁡α)\mathbf{u} = (\cos\alpha, \sin\alpha).
  • ∇f⋅u=∥∇f∥cos⁡θ\nabla f \cdot \mathbf{u} = \lVert \nabla f \rVert \cos\theta: steepest ascent along ∇f\nabla f with slope ∥∇f∥\lVert \nabla f \rVert, steepest descent against it with slope −∥∇f∥-\lVert \nabla f \rVert, and slope 0 at right angles.
  • Along a level curve the slope is 0, so ∇f\nabla f crosses level curves at right angles.
  • Where ∇f=0\nabla f = \mathbf{0} the surface is flat in every direction. At a corner, such as the cone at its tip, there is no gradient.

Free further reading

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