A function of two variables has four second partial derivatives (lesson 4.1.2). fxx differentiates twice in x with y held fixed, fyy twice in y, and fxy and fyx once in each. The Hessian puts them in a matrix.
Rule: The Hessian
H=(fxxfyxfxyfyy)
For n variables x1,…,xn, H is n×n, and its entry in row i, column j is ∂xi∂xj∂2f. Since fxy=fyx (lesson 4.1.2), H is symmetric, so the tools of lessons 3.6.3 and 3.7.1 apply to it.
Take f(x,y)=x2y. Its first partials are fx=2xy and fy=x2. Differentiating again gives fxx=2y, fxy=fyx=2x and fyy=0. At the point a=(1,2):
f(a)=2,∇f(a)=(41)H=(4220)
Each entry has a meaning. fxx=4 is the bend of the slice y=2, which is 2x2 (lessons 4.1.1 and 2.4.2). fyy=0 because the slice x=1 is the straight line f=y. And fxy=2 says how the slope in x changes as y moves: fx=2xy rises by 2 per unit of y at x=1.
The units follow. If f is in dollars, x in dollars and y in years, then fxy is in dollars per dollar per year. Each term of the expansion below ends up in the units of f.
H moves the gradient
Row 1 of H holds the partials of fx, and row 2 those of fy. So the total differential of lesson 4.2.2, applied to fx and fy in turn, says how the whole gradient changes after a small step h=(h1,h2).
Rule: H moves the gradient
∇f(a+h)≈∇f(a)+Hh
H is to the gradient what the gradient is to f: the change after a small step. Lesson 4.4.3 builds Newton's method on it.
For x2y, step from (1,2) to (1.1,2), so h=(0.1,0) and Hh=(0.4,0.2). The rule predicts ∇f=(4.4,1.2). The true gradient is (2×1.1×2,1.12)=(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 H at (1,2).
The last line compares ∇f(a)+Hh with the true gradient at (1.1,2).
⌘/Ctrl + Enter runs the cell
The second-order Taylor expansion
Lesson 2.7.1 copied a function of one variable near a with a parabola, using f′′. The same copy works in several variables, with H in place of f′′.
Walk from a along the step h: g(t)=f(a+th), a function of one variable t. Its slope at t=0 is ∇f(a)⋅h, the chain rule of lesson 4.2.2. Applying the chain rule once more gives its second derivative, h⊤Hh; a cell below checks this on an example.
The quadratic copy of g about 0, evaluated at t=1, is g(0)+g′(0)+21g′′(0). Since g(1)=f(a+h), this is the rule.
Rule: Second-order Taylor expansion
f(a+h)≈f(a)+∇f(a)⋅h+21h⊤Hh
The quadratic copy of f near a. The step is h; ∇f and H are taken at 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:
Here f, fx and the rest are their values at a. The cross term has no 21: the off-diagonal entry appears twice in h⊤Hh, as 2fxyh1h2, and the 21 cancels the 2.
Example 1: x2y at (1,2)
With f=2, ∇f=(4,1) and H as above, the copy is
2+4h1+h2+2h12+2h1h2
step h
(0.1,0.1)
(0.1,−0.2)
(−0.2,0.1)
(0.2,0.2)
(0.5,0.5)
f(a+h)
2.541
2.178
1.344
3.168
5.625
tangent plane
2.5
2.2
1.3
3
4.5
quadratic copy
2.54
2.18
1.34
3.16
5.5
error of the copy
0.001
−0.002
0.004
0.008
0.125
Each error is f minus the copy. The bend terms take the tangent plane's miss of 0.041 at (0.1,0.1) down to 0.001.
Algebra shows why. Expand the function at the step:
The first five terms are the copy. The error is exactly h12h2, a term of degree 3: 0.12×0.1=0.001, and 0.12×(−0.2)=−0.002, where the copy is too high.
Try it
The loop evaluates f, the tangent plane f(a)+∇f⋅h and the copy at each step of the table.
The last two columns are the error of the copy and h12h2: they agree.
⌘/Ctrl + Enter runs the cell
Example 2: xey at (2,0)
Here fx=ey and fy=xey, then fxx=0, fxy=ey and fyy=xey (lesson 2.3.4 for ey). At (2,0), e0=1, so
At the step (0.1,0.1) the copy gives 2.32, against 2.1e0.1=2.320859. The tangent plane gives 2.3.
Level curves of xey (solid), with those of its tangent plane and of its quadratic copy about (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), and shrink t. For xey at (2,0):
t
0.4
0.2
0.1
shrinks by
error of the tangent plane
0.38
0.0871
0.0209
about 4
error of the quadratic copy
0.0604
0.00709
0.000859
about 8
The tangent plane's error falls like t2, as lesson 4.2.2 found. The copy's falls like t3, the first power it leaves out, as in lesson 2.7.1.
Rule: Error of the quadratic copy
f(a+tu)−copy≈Ct3
For small steps tu in a fixed direction u. The constant C depends on f, on 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 xey at (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 h12h2, which takes the sign of h2.
Along the direction (−1,1) in example 2, the bend term is h1h2+h22=−t2+t2=0. There the copy is just the tangent plane. Its error still falls like t3: −0.0131, −0.00148 and −0.000175 at t=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 t, the error of the tangent plane and of the copy along (1,1). The second prints the ratios as t halves.
The last loop runs along (−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, the slice g(t)=f(a+tu) has second derivative u⊤Hu at t=0. It measures how the surface bends as you walk through a along u.
Rule: Second derivative along u
dt2d2f(a+tu)t=0=u⊤Hu
Along the axes it gives fxx and fyy. In other directions fxy joins in: u⊤Hu=fxxu12+2fxyu1u2+fyyu22.
For xey at (2,0), with H=(0112):
direction u
(1,0)
(0,1)
21(1,1)
21(1,−1)
u⊤Hu
0
2
2
0
Along x the slice is xe0=x, a straight line, so the bend is 0. Along y it is 2ey, whose second derivative at 0 is 2. Lesson 4.3.2 reads the sign of u⊤Hu in every direction to tell a minimum from a saddle.
Try it
The loop prints u⊤Hu for the four directions of the table.
The last line takes the second central difference (lesson 2.4.2) of the slice along 21(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−3x of lesson 4.2.1, the Hessian is (2112) at every point, lesson 3.7.1's S. The expansion has no terms of degree 3, so the copy is exact for any step, as x3 about 2 was its own cubic copy in lesson 2.7.1.
About (0,0), with f=0 and ∇f=(−3,0), the step (2,−1) gives −6+3=−3. That is f(2,−1) exactly: the bottom of the bowl, found in lesson 4.2.1.
Far from the point the copy drifts. For xey the step (1,1) gives 2+1+2+1+1=7, against 3e=8.1548.
Small next to what? A step is small when it is small next to the distance over which f 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 has no gradient at the origin (lesson 4.2.1), as ∣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), gradient and H. For the tilted bowl about (0,0) it matches f at two far steps: the copy of a quadratic is exact.
The last line is xey about (2,0) at the step (1,1), where the copy is 7 and f is 3e.
⌘/Ctrl + Enter runs the cell
Computing H numerically
Lesson 4.1.2 estimated second partials by central differences: fxx from three points along one axis, and fxy from the four corners of a square around the point, 4δ2f++−f+−−f−++f−−. Here δ is the bump, called h there; in this lesson h is the step. Applied to every pair of variables, the corner formula fills the whole of H.
As in lesson 2.8.1, a bump near 10−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 xey and on xyz.
For f(x,y,z)=xyz at (1,2,3), ∇f=(yz,xz,xy)=(6,3,2), and each entry of H is the third variable: fxy=z=3, fxz=y=2, fyz=x=1. The diagonal is 0. At the step (0.1,0.1,0.1) the copy gives 7.16, against 1.1×2.1×3.1=7.161.
Try it
hessian fills H entry by entry. On the diagonal the corner formula with ei=ej reduces to the second difference with bump 2δ, so it covers both cases.
The printed matrices match (0112) and the 3×3 matrix above. The last line is the copy of xyz at the step.
⌘/Ctrl + Enter runs the cell
Common mistakes
The cross term has no 21.21h⊤Hh=21fxxh12+fxyh1h2+21fyyh22. Writing 21fxyh1h2 halves it; writing 2fxyh1h2 doubles it.
Keep the 21 in front. Without it the copy's second derivative along every direction is twice that of f, as in lesson 2.7.1.
Take ∇f and H at a, not at a+h. They are numbers.
Use the step h=x−a, not the new point x.
H must come out symmetric. If your fxy and fyx differ, one of them is wrong.
The cube rule holds for small steps. Far from 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.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
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 H holds the second partials: entry (i,j) is ∂xi∂xj∂2f. It is symmetric because fxy=fyx.
H moves the gradient: ∇f(a+h)≈∇f(a)+Hh.
Second-order Taylor expansion: f(a+h)≈f(a)+∇f(a)⋅h+21h⊤Hh. In two variables the bend terms are 21fxxh12+fxyh1h2+21fyyh22.
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 is the second derivative of f along the unit vector u.
Lesson 4.1.2's central differences, applied to every pair of variables, give H numerically for any number of variables.