SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.3.1 The Hessian and second-order Taylor expansionConcept · 35 min
      2. 4.3.2 Minima, maxima and saddle pointsConcept · 30 min
      3. 4.3.3 Practice set: second-order methodsPractice set · 20 min
      4. 4.3.4 Case study: delta–gamma–vega P&LCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.3 Second order Lesson 1 of 4

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

The Hessian and second-order Taylor expansion

The Hessian

A function of two variables has four second partial derivatives (lesson 4.1.2). fxxf_{xx} differentiates twice in xx with yy held fixed, fyyf_{yy} twice in yy, and fxyf_{xy} and fyxf_{yx} once in each. The Hessian puts them in a matrix.

Rule: The Hessian

H=(fxxfxyfyxfyy)H = \begin{pmatrix} f_{xx} & f_{xy} \\ f_{yx} & f_{yy} \end{pmatrix}

For nn variables x1,…,xnx_1, \ldots, x_n, HH is n×nn \times n, and its entry in row ii, column jj is ∂2f∂xi ∂xj\frac{\partial^2 f}{\partial x_i\,\partial x_j}. Since fxy=fyxf_{xy} = f_{yx} (lesson 4.1.2), HH is symmetric, so the tools of lessons 3.6.3 and 3.7.1 apply to it.

Take f(x,y)=x2yf(x, y) = x^2y. Its first partials are fx=2xyf_x = 2xy and fy=x2f_y = x^2. Differentiating again gives fxx=2yf_{xx} = 2y, fxy=fyx=2xf_{xy} = f_{yx} = 2x and fyy=0f_{yy} = 0. At the point a=(1,2)\mathbf{a} = (1, 2):

f(a)=2,∇f(a)=(41)H=(4220)\begin{gathered} f(\mathbf{a}) = 2, \quad \nabla f(\mathbf{a}) = \begin{pmatrix} 4 \\ 1 \end{pmatrix} \\[6pt] H = \begin{pmatrix} 4 & 2 \\ 2 & 0 \end{pmatrix} \end{gathered}

Each entry has a meaning. fxx=4f_{xx} = 4 is the bend of the slice y=2y = 2, which is 2x22x^2 (lessons 4.1.1 and 2.4.2). fyy=0f_{yy} = 0 because the slice x=1x = 1 is the straight line f=yf = y. And fxy=2f_{xy} = 2 says how the slope in xx changes as yy moves: fx=2xyf_x = 2xy rises by 2 per unit of yy at x=1x = 1.

The units follow. If ff is in dollars, xx in dollars and yy in years, then fxyf_{xy} is in dollars per dollar per year. Each term of the expansion below ends up in the units of ff.

H moves the gradient

Row 1 of HH holds the partials of fxf_x, and row 2 those of fyf_y. So the total differential of lesson 4.2.2, applied to fxf_x and fyf_y in turn, says how the whole gradient changes after a small step h=(h1,h2)\mathbf{h} = (h_1, h_2).

Rule: H moves the gradient

∇f(a+h)≈∇f(a)+Hh\nabla f(\mathbf{a} + \mathbf{h}) \approx \nabla f(\mathbf{a}) + H\mathbf{h}

HH is to the gradient what the gradient is to ff: the change after a small step. Lesson 4.4.3 builds Newton's method on it.

For x2yx^2y, step from (1,2)(1, 2) to (1.1,2)(1.1, 2), so h=(0.1,0)\mathbf{h} = (0.1, 0) and Hh=(0.4,0.2)H\mathbf{h} = (0.4, 0.2). The rule predicts ∇f=(4.4,1.2)\nabla f = (4.4, 1.2). The true gradient is (2×1.1×2, 1.12)=(4.4,1.21)(2 \times 1.1 \times 2,\ 1.1^2) = (4.4, 1.21).

Try it

sp.hessian builds the matrix of second partials from the function and its variables. The next line evaluates the gradient and HH at (1,2)(1, 2).

The last line compares ∇f(a)+Hh\nabla f(\mathbf{a}) + H\mathbf{h} with the true gradient at (1.1,2)(1.1, 2).

⌘/Ctrl + Enter runs the cell

The second-order Taylor expansion

Lesson 2.7.1 copied a function of one variable near aa with a parabola, using f′′f''. The same copy works in several variables, with HH in place of f′′f''.

Walk from a\mathbf{a} along the step h\mathbf{h}: g(t)=f(a+th)g(t) = f(\mathbf{a} + t\mathbf{h}), a function of one variable tt. Its slope at t=0t = 0 is ∇f(a)⋅h\nabla f(\mathbf{a}) \cdot \mathbf{h}, the chain rule of lesson 4.2.2. Applying the chain rule once more gives its second derivative, h⊤Hh\mathbf{h}^\top H\mathbf{h}; a cell below checks this on an example.

The quadratic copy of gg about 0, evaluated at t=1t = 1, is g(0)+g′(0)+12g′′(0)g(0) + g'(0) + \frac{1}{2}g''(0). Since g(1)=f(a+h)g(1) = f(\mathbf{a} + \mathbf{h}), this is the rule.

Rule: Second-order Taylor expansion

f(a+h)≈f(a)+∇f(a)⋅h+12 h⊤H h\begin{gathered} f(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a}) \cdot \mathbf{h} \\ + \tfrac{1}{2}\,\mathbf{h}^\top H\,\mathbf{h} \end{gathered}

The quadratic copy of ff near a\mathbf{a}. The step is h\mathbf{h}; ∇f\nabla f and HH are taken at a\mathbf{a}, so they are numbers. The first two terms are the tangent plane of lesson 4.2.2, and the last is a quadratic form (lesson 3.7.1).

Written out for two variables, with the quadratic form of lesson 3.7.1:

f(a+h)≈f+fxh1+fyh2+12fxxh12+fxyh1h2+12fyyh22\begin{aligned} f(\mathbf{a} + \mathbf{h}) \approx{}& f + f_x h_1 + f_y h_2 \\ &+ \tfrac{1}{2}f_{xx}h_1^2 + f_{xy}h_1h_2 \\ &+ \tfrac{1}{2}f_{yy}h_2^2 \end{aligned}

Here ff, fxf_x and the rest are their values at a\mathbf{a}. The cross term has no 12\frac{1}{2}: the off-diagonal entry appears twice in h⊤Hh\mathbf{h}^\top H\mathbf{h}, as 2fxyh1h22f_{xy}h_1h_2, and the 12\frac{1}{2} cancels the 2.

Example 1: x2yx^2y at (1,2)(1, 2)

With f=2f = 2, ∇f=(4,1)\nabla f = (4, 1) and HH as above, the copy is

2+4h1+h2+2h12+2h1h22 + 4h_1 + h_2 + 2h_1^2 + 2h_1h_2
step h\mathbf{h}(0.1,0.1)(0.1, 0.1)(0.1,−0.2)(0.1, -0.2)(−0.2,0.1)(-0.2, 0.1)(0.2,0.2)(0.2, 0.2)(0.5,0.5)(0.5, 0.5)
f(a+h)f(\mathbf{a} + \mathbf{h})2.5412.1781.3443.1685.625
tangent plane2.52.21.334.5
quadratic copy2.542.181.343.165.5
error of the copy0.001−0.002-0.0020.0040.0080.125

Each error is ff minus the copy. The bend terms take the tangent plane's miss of 0.041 at (0.1,0.1)(0.1, 0.1) down to 0.001.

Algebra shows why. Expand the function at the step:

(1+h1)2(2+h2)=2+4h1+h2+2h12+2h1h2+h12h2\begin{aligned} (1 + h_1)^2(2 + h_2) ={}& 2 + 4h_1 + h_2 \\ &+ 2h_1^2 + 2h_1h_2 \\ &+ h_1^2h_2 \end{aligned}

The first five terms are the copy. The error is exactly h12h2h_1^2h_2, a term of degree 3: 0.12×0.1=0.0010.1^2 \times 0.1 = 0.001, and 0.12×(−0.2)=−0.0020.1^2 \times (-0.2) = -0.002, where the copy is too high.

Try it

The loop evaluates ff, the tangent plane f(a)+∇f⋅hf(\mathbf{a}) + \nabla f \cdot \mathbf{h} and the copy at each step of the table.

The last two columns are the error of the copy and h12h2h_1^2h_2: they agree.

⌘/Ctrl + Enter runs the cell

Example 2: xeyxe^y at (2,0)(2, 0)

Here fx=eyf_x = e^y and fy=xeyf_y = xe^y, then fxx=0f_{xx} = 0, fxy=eyf_{xy} = e^y and fyy=xeyf_{yy} = xe^y (lesson 2.3.4 for eye^y). At (2,0)(2, 0), e0=1e^0 = 1, so

f=2,∇f=(12),H=(0112)copy: 2+h1+2h2+h1h2+h22\begin{gathered} f = 2, \quad \nabla f = \begin{pmatrix} 1 \\ 2 \end{pmatrix}, \quad H = \begin{pmatrix} 0 & 1 \\ 1 & 2 \end{pmatrix} \\[6pt] \text{copy: } 2 + h_1 + 2h_2 + h_1h_2 + h_2^2 \end{gathered}

At the step (0.1,0.1)(0.1, 0.1) the copy gives 2.32, against 2.1e0.1=2.3208592.1e^{0.1} = 2.320859. The tangent plane gives 2.3.

Level curves of xeyxe^y (solid), with those of its tangent plane and of its quadratic copy about (2,0)(2, 0) (dashed). The copy follows the bend of the curves near the point; further out they part.

How fast the error falls

Take steps in one direction, h=t (1,1)\mathbf{h} = t\,(1, 1), and shrink tt. For xeyxe^y at (2,0)(2, 0):

tt0.40.20.1shrinks by
error of the tangent plane0.380.08710.0209about 4
error of the quadratic copy0.06040.007090.000859about 8

The tangent plane's error falls like t2t^2, as lesson 4.2.2 found. The copy's falls like t3t^3, the first power it leaves out, as in lesson 2.7.1.

Rule: Error of the quadratic copy

f(a+tu)−copy≈C t3f(\mathbf{a} + t\mathbf{u}) - \text{copy} \approx C\,t^3

For small steps tut\mathbf{u} in a fixed direction u\mathbf{u}. The constant CC depends on ff, on a\mathbf{a} and on the direction. Halve the step and the error shrinks about 8 times.

The error of the tangent plane and of the quadratic copy of xeyxe^y at (2,0)(2, 0), on log scales. The slopes are 2 and 3: the bend terms add one to the power.

Both sides, and a direction with no bend

The error can have either sign. In example 1 it is h12h2h_1^2h_2, which takes the sign of h2h_2.

Along the direction (−1,1)(-1, 1) in example 2, the bend term is h1h2+h22=−t2+t2=0h_1h_2 + h_2^2 = -t^2 + t^2 = 0. There the copy is just the tangent plane. Its error still falls like t3t^3: −0.0131-0.0131, −0.00148-0.00148 and −0.000175-0.000175 at t=0.4t = 0.4, 0.2 and 0.1, as when a Taylor coefficient is 0 in lesson 2.7.1.

Try it

The first loop prints, for each tt, the error of the tangent plane and of the copy along (1,1)(1, 1). The second prints the ratios as tt halves.

The last loop runs along (−1,1)(-1, 1), where the two errors are equal.

⌘/Ctrl + Enter runs the cell

The second derivative along a direction

The middle step of the derivation is worth keeping. For a unit vector u\mathbf{u}, the slice g(t)=f(a+tu)g(t) = f(\mathbf{a} + t\mathbf{u}) has second derivative u⊤Hu\mathbf{u}^\top H\mathbf{u} at t=0t = 0. It measures how the surface bends as you walk through a\mathbf{a} along u\mathbf{u}.

Rule: Second derivative along u

d2dt2f(a+tu)∣t=0=u⊤H u\frac{d^2}{dt^2} f(\mathbf{a} + t\mathbf{u}) \Big|_{t = 0} = \mathbf{u}^\top H\,\mathbf{u}

Along the axes it gives fxxf_{xx} and fyyf_{yy}. In other directions fxyf_{xy} joins in: u⊤Hu=fxxu12+2fxyu1u2+fyyu22\mathbf{u}^\top H\mathbf{u} = f_{xx}u_1^2 + 2f_{xy}u_1u_2 + f_{yy}u_2^2.

For xeyxe^y at (2,0)(2, 0), with H=(0112)H = \begin{pmatrix} 0 & 1 \\ 1 & 2 \end{pmatrix}:

direction u\mathbf{u}(1,0)(1, 0)(0,1)(0, 1)12(1,1)\frac{1}{\sqrt{2}}(1, 1)12(1,−1)\frac{1}{\sqrt{2}}(1, -1)
u⊤Hu\mathbf{u}^\top H\mathbf{u}0220

Along xx the slice is xe0=xxe^0 = x, a straight line, so the bend is 0. Along yy it is 2ey2e^y, whose second derivative at 0 is 2. Lesson 4.3.2 reads the sign of u⊤Hu\mathbf{u}^\top H\mathbf{u} in every direction to tell a minimum from a saddle.

Try it

The loop prints u⊤Hu\mathbf{u}^\top H\mathbf{u} for the four directions of the table.

The last line takes the second central difference (lesson 2.4.2) of the slice along 12(1,1)\frac{1}{\sqrt{2}}(1, 1), with a step of 0.001: it agrees with 2.

⌘/Ctrl + Enter runs the cell

Where the copy is exact, and where it fails

A quadratic is its own copy. For the tilted bowl f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x of lesson 4.2.1, the Hessian is (2112)\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} at every point, lesson 3.7.1's SS. The expansion has no terms of degree 3, so the copy is exact for any step, as x3x^3 about 2 was its own cubic copy in lesson 2.7.1.

About (0,0)(0, 0), with f=0f = 0 and ∇f=(−3,0)\nabla f = (-3, 0), the step (2,−1)(2, -1) gives −6+3=−3-6 + 3 = -3. That is f(2,−1)f(2, -1) exactly: the bottom of the bowl, found in lesson 4.2.1.

Far from the point the copy drifts. For xeyxe^y the step (1,1)(1, 1) gives 2+1+2+1+1=72 + 1 + 2 + 1 + 1 = 7, against 3e=8.15483e = 8.1548.

Small next to what? A step is small when it is small next to the distance over which ff bends, in each variable. A move of 1 may be tiny for one input and huge for another; case study 4.3.4 meets this with a stock price in dollars and a volatility in decimals.

No second derivatives, no copy. The cone x2+y2\sqrt{x^2 + y^2} has no gradient at the origin (lesson 4.2.1), as ∣x∣|x| has no slope at 0. At such a point there is no tangent plane and no quadratic copy.

Try it

copy is the second-order expansion for any f(a)f(\mathbf{a}), gradient and HH. For the tilted bowl about (0,0)(0, 0) it matches ff at two far steps: the copy of a quadratic is exact.

The last line is xeyxe^y about (2,0)(2, 0) at the step (1,1)(1, 1), where the copy is 7 and ff is 3e3e.

⌘/Ctrl + Enter runs the cell

Computing H numerically

Lesson 4.1.2 estimated second partials by central differences: fxxf_{xx} from three points along one axis, and fxyf_{xy} from the four corners of a square around the point, f++−f+−−f−++f−−4δ2\frac{f_{++} - f_{+-} - f_{-+} + f_{--}}{4\delta^2}. Here δ\delta is the bump, called hh there; in this lesson h\mathbf{h} is the step. Applied to every pair of variables, the corner formula fills the whole of HH.

As in lesson 2.8.1, a bump near 10−410^{-4} times the size of the inputs balances truncation and rounding for a second difference. The function below works for any number of variables, and is checked on xeyxe^y and on xyzxyz.

For f(x,y,z)=xyzf(x, y, z) = xyz at (1,2,3)(1, 2, 3), ∇f=(yz,xz,xy)=(6,3,2)\nabla f = (yz, xz, xy) = (6, 3, 2), and each entry of HH is the third variable: fxy=z=3f_{xy} = z = 3, fxz=y=2f_{xz} = y = 2, fyz=x=1f_{yz} = x = 1. The diagonal is 0. At the step (0.1,0.1,0.1)(0.1, 0.1, 0.1) the copy gives 7.16, against 1.1×2.1×3.1=7.1611.1 \times 2.1 \times 3.1 = 7.161.

Try it

hessian fills HH entry by entry. On the diagonal the corner formula with ei=ej\mathbf{e}_i = \mathbf{e}_j reduces to the second difference with bump 2δ2\delta, so it covers both cases.

The printed matrices match (0112)\begin{pmatrix} 0 & 1 \\ 1 & 2 \end{pmatrix} and the 3×33 \times 3 matrix above. The last line is the copy of xyzxyz at the step.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The cross term has no 12\frac{1}{2}. 12h⊤Hh=12fxxh12+fxyh1h2+12fyyh22\frac{1}{2}\mathbf{h}^\top H\mathbf{h} = \frac{1}{2}f_{xx}h_1^2 + f_{xy}h_1h_2 + \frac{1}{2}f_{yy}h_2^2. Writing 12fxyh1h2\frac{1}{2}f_{xy}h_1h_2 halves it; writing 2fxyh1h22f_{xy}h_1h_2 doubles it.
  2. Keep the 12\frac{1}{2} in front. Without it the copy's second derivative along every direction is twice that of ff, as in lesson 2.7.1.
  3. Take ∇f\nabla f and HH at a\mathbf{a}, not at a+h\mathbf{a} + \mathbf{h}. They are numbers.
  4. Use the step h=x−a\mathbf{h} = \mathbf{x} - \mathbf{a}, not the new point x\mathbf{x}.
  5. HH must come out symmetric. If your fxyf_{xy} and fyxf_{yx} differ, one of them is wrong.
  6. The cube rule holds for small steps. Far from a\mathbf{a}, or where the second derivatives do not exist, the copy can miss badly.

The Hessian and second-order Taylor expansion

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

  • The Hessian HH holds the second partials: entry (i,j)(i, j) is ∂2f∂xi ∂xj\frac{\partial^2 f}{\partial x_i\,\partial x_j}. It is symmetric because fxy=fyxf_{xy} = f_{yx}.
  • HH moves the gradient: ∇f(a+h)≈∇f(a)+Hh\nabla f(\mathbf{a} + \mathbf{h}) \approx \nabla f(\mathbf{a}) + H\mathbf{h}.
  • Second-order Taylor expansion: f(a+h)≈f(a)+∇f(a)⋅h+12h⊤Hhf(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a}) \cdot \mathbf{h} + \frac{1}{2}\mathbf{h}^\top H\mathbf{h}. In two variables the bend terms are 12fxxh12+fxyh1h2+12fyyh22\frac{1}{2}f_{xx}h_1^2 + f_{xy}h_1h_2 + \frac{1}{2}f_{yy}h_2^2.
  • Its error falls like the cube of the step in any fixed direction; the tangent plane's falls like the square. A quadratic function is its own copy.
  • u⊤Hu\mathbf{u}^\top H\mathbf{u} is the second derivative of ff along the unit vector u\mathbf{u}.
  • Lesson 4.1.2's central differences, applied to every pair of variables, give HH numerically for any number of variables.

Free further reading

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