To minimise a function f(x) of n variables is to find a point x∗ where f is as small as possible. The point x∗ is a minimiser and f(x∗) is the minimum value. As in lesson 2.4.1, an answer has two parts: where, in the units of x, and how much, in the units of f.
The function being minimised is called the objective. Unconstrained means that x may be any point of Rn. Module 4.5 adds conditions, such as portfolio weights that must add up to 1.
A maximum needs no new theory. The highest point of f is the lowest point of −f, so this module speaks only of minima.
The first-order condition
Lesson 4.2.1 showed that the slope of f in a unit direction u is ∇f⋅u, and that f falls fastest against the gradient. So if ∇f(x)=0, a small step against the gradient lowers f, and x is not a minimum.
Take f(x,y)=x2+xy+y2−3x, whose gradient is ∇f=(2x+y−3,x+2y). At (0,1), f=1 and ∇f=(−2,2).
A step of size t against the gradient goes to (0,1)−t(−2,2). The tangent-plane estimate of lesson 4.2.2 says f falls by about t∥∇f∥2=8t.
step t
new point
f there
estimate 1−8t
0.1
(0.2,0.8)
0.24
0.2
0.05
(0.1,0.9)
0.61
0.6
0.01
(0.02,0.98)
0.9204
0.92
Every step lowers f, and the smaller the step, the closer the estimate.
Rule: First-order condition
∇f(x∗)=0
At a minimum of a differentiable function, every partial derivative is zero. A point where this holds is a critical point (lesson 4.3.2). The condition is needed but not enough: a maximum or a saddle passes it too.
The saddle x2−y2 of lesson 4.1.1 has ∇f=(2x,−2y)=0 at the origin. Yet f(0,0.1)=−0.01 is below f(0,0)=0, so the origin is not a minimum.
For f=x2+xy+y2−3x, the condition is two linear equations, 2x+y=3 and x+2y=0. That is a linear system (lesson 3.3.1), and its solution is (2,−1), with f(2,−1)=−3. On a grid around it, every neighbour is higher:
y=−0.5
y=−1
y=−1.5
x=1.5
−2.75
−2.75
−2.25
x=2
−2.75
−3
−2.75
x=2.5
−2.25
−2.75
−2.75
A table only samples a few points. The next section gives a test that settles it.
Try it
grad is ∇f. The loop takes the three steps of the first table and prints f against the estimate 1−8t.
Then np.linalg.solve finds the critical point from the two equations, and the loop prints the grid of f around it. The last line gives the eigenvalues of the Hessian, used in the next section.
⌘/Ctrl + Enter runs the cell
Second-order conditions
Lesson 4.3.2's test reads the Hessian H, the matrix of second partial derivatives, at a critical point. Positive definite means a minimum, negative definite a maximum, indefinite a saddle, and semidefinite leaves it undecided. For minimising, the test splits into a condition that is needed and one that is enough.
At every local minimum the Hessian is positive semidefinite (lesson 3.7.1). A critical point with a positive definite Hessian is a strict local minimum: every nearby point is higher.
For f=x2+xy+y2−3x the Hessian is H=(2112) at every point. Its eigenvalues are 1 and 3 (lesson 3.6.3), both positive, so (2,−1) is a strict local minimum.
The "needed" condition rules points out. If H has a negative eigenvalue, f falls along that eigenvector, so the point is no minimum. At the saddle x2−y2, H has eigenvalues 2 and −2.
The gap between the two conditions is a semidefinite Hessian with a zero eigenvalue. Lesson 4.3.2 showed it with x4+y2 and x3+y2: both have ∇f=0 and H=(0002) at the origin, so both pass the needed condition. Only the first has a minimum there.
Local and global minima
A local minimum is lower than every point near it. The global minimum is the lowest value anywhere, as in lesson 2.4.1. Both conditions above are local: they look only near x∗.
Take f(x,y)=x4−3x2+x+y2. Along y=0 it is the quartic of lesson 2.4.3.
Its gradient is (4x3−6x+1,2y), so a critical point needs y=0 and 4x3−6x+1=0. Lesson 2.4.3 found the three roots.
The Hessian is (12x2−6002), so the sign of 12x2−6 decides each point:
critical point
f
12x2−6
Hessian
kind
(−1.30084,0)
−3.5139
14.3062
positive definite
global minimum
(0.169938,0)
0.0841
−5.6535
indefinite
saddle
(1.130901,0)
−1.0702
9.3472
positive definite
local minimum only
Two valleys and a saddle between them. Both minima pass the second-order test; only the comparison of their values shows which is global. The dashed level curve through the saddle encloses one valley in each loop.
To find the global minimum, compare the critical points. On a closed interval lesson 2.4.1 also checked the ends.
In Rn there are no ends, but there is "far away": here x4 and y2 grow without limit in every direction, with f(3,0)=57 and f(0,3)=9. So f has a lowest point, and it is the lower minimum, −3.5139 at (−1.30084,0).
Without that growth there may be no global minimum. Lesson 4.3.2's cubic x3−3x+y2 has a local minimum at (1,0), where f=−2, but f(−10,0)=−970: it falls without limit.
Rule: Local and global
f grows without limit far away:the lowest critical point is global
If f falls without limit in some direction, it has no global minimum. Lesson 4.5.3 adds convex functions, for which every local minimum is global.
The methods of lessons 4.4.2 and 4.4.3 walk downhill from a starting point and stop at a minimum near it. Which minimum depends on the start, so case study 4.4.5 tries several.
Try it
np.roots finds the three roots of 4x3−6x+1. Each line prints a root, f there and the entry 12x2−6 of the Hessian.
The next line shows the quartic growing far away. The last line is the cubic, which has no lowest point.
⌘/Ctrl + Enter runs the cell
Quadratic functions
The most important example is a quadratic function: a quadratic form (lesson 3.7.1) minus a linear part. For a symmetric n×n matrix A and a vector b in Rn,
f(x)=21x⊤Ax−b⊤x
Its gradient is Ax−b and its Hessian is A at every point. For n=2, lesson 3.7.1 writes the form out as 21x⊤Ax=21a11x12+a12x1x2+21a22x22. Its partial derivative in x1 is a11x1+a12x2, the first entry of Ax.
The 21 is there so that the gradient has no factor 2.
The example above is one. With A=(2112) and b=(3,0), 21x⊤Ax=x2+xy+y2 and b⊤x=3x.
So the first-order condition ∇f=0 reads Ax=b: the minimiser of a quadratic solves a linear system. For a quadratic, the second-order expansion of lesson 4.3.1 is exact, with nothing left over. At a solution of Ax∗=b its gradient term is zero:
Rule: Minimum of a quadratic
Ax∗=bf(x∗+h)=f(x∗)+21h⊤Ahf(x∗)=−21b⊤x∗
If A is positive definite, 21h⊤Ah>0 for every step h=0. So x∗ is the unique global minimum, not just a local one.
For the example, x∗=(2,−1) and −21b⊤x∗=−21(6)=−3, as found above. A step h=(0.5,0.5) raises f by 21h⊤Ah=0.75: f(2.5,−0.5)=−2.25.
Along a unit eigenvector q of A with eigenvalue λ, a step of t raises f by 21λt2. Here one unit along (1,1)/2 adds 1.5, and along (1,−1)/2 only 0.5. The bowl is steeper along one eigenvector than the other, which matters for lesson 4.4.2.
Three variables. Lesson 3.7.1's positive definite matrix
K=4−10−132025
with b=(3,0,−3) gives x∗=(1,1,−1) and a minimum value of −3. Its eigenvalues are 1.4711, 4.1674 and 6.3615.
When A is not positive definite
Ax=b can still have a solution, but it is not a minimum.
A
Ax=b
the quadratic
positive definite
one solution
one global minimum
indefinite
a solution
a saddle: no minimum
semidefinite, singular
none, or a whole line
no minimum, or a line of minima
Indefinite.N=(1221), with eigenvalues −1 and 3, and b=(3,3) give the solution (1,1), where f=−3. Along (1,−1), f(1+t,1−t)=−3−t2, which is −4, −7 and −103 at t=1, 2 and 10.
Semidefinite.J=(1111) is singular, so solve stops with an error. With b=(1,1), f=21(x+y)2−(x+y) is smallest, −0.5, on the whole line x+y=1. With b=(1,−1), f(t,−t)=−2t falls without limit.
Try it
The first lines check that 21x⊤Ax−b⊤x is the example's f at a point, then solve Ax=b and test f(x∗+h)−f(x∗)=21h⊤Ah.
Then the 3×3 example, the saddle of N and the singular J. Always check the eigenvalues before trusting a solution.
⌘/Ctrl + Enter runs the cell
Least squares is a quadratic
Lesson 3.5.3 chose x^ to make ∥Mx−y∥2 as small as possible, for a matrix M and data y. Multiplied out, this is
F(x)=x⊤M⊤Mx−2(M⊤y)⊤x+y⊤y
That is a quadratic function with A=2M⊤M and b=2M⊤y, plus a constant. So ∇F=2M⊤(Mx−y), and ∇F=0 is M⊤Mx=M⊤y: the normal equations of lesson 3.5.3.
The Hessian 2M⊤M is always positive semidefinite, and positive definite when the columns of M are independent (lesson 3.7.1). Then the normal equations give the global minimum.
For lesson 3.5.3's five points, the line y=1+1.2x leaves a sum of squares of 0.8, and the gradient there is (0,0). The line y=1+x leaves 2, with gradient (−4,−12): both entries negative, so raising the intercept or the slope lowers F. The Hessian has eigenvalues 2.9844 and 67.0156.
When a model is not linear in its unknowns, ∇F=0 is no longer a linear system. Lessons 4.4.2 and 4.4.3 find such minima by walking downhill, and case study 4.4.5 fits a yield curve that way. Module 5.9 maximises a likelihood with the same conditions.
Try it
M has a column of ones and the five x values; F is the sum of squared misses and grad its gradient 2M⊤(Mx−y).
The gradient is zero at the least-squares answer from the normal equations, and not at (1,1).
⌘/Ctrl + Enter runs the cell
Common mistakes
∇f=0 is not enough. Maxima and saddles pass it too; check the Hessian.
A semidefinite Hessian proves nothing.x4+y2 and x3+y2 have the same Hessian at the origin; only the first has a minimum there (lesson 4.3.2).
Both conditions are local. The minimum at (1.130901,0) passes the test but lies above the one at (−1.30084,0).
A local minimum may have no global one below it.x3−3x+y2 falls without limit as x goes to −∞.
Mind the 21. The gradient of 21x⊤Ax is Ax; the gradient of x⊤Ax is 2Ax.
Check A before you solve. An indefinite A still gives a solution of Ax=b, and it is a saddle.
For a maximum, minimise −f. At a maximum the Hessian of f is negative definite.
Optimality conditions
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
To minimise f(x) over Rn is to find the minimiser x∗ and the value f(x∗). A maximum of f is a minimum of −f.
First-order condition: ∇f(x∗)=0. Needed at every minimum of a differentiable f, but maxima and saddles pass it too.
Second-order conditions: at a local minimum H is positive semidefinite; ∇f=0 with H positive definite is enough for a strict local minimum. A zero eigenvalue leaves it undecided.
Local is not global. If f grows without limit in every direction, compare the critical points; if it falls without limit in some direction, there is no global minimum.
f(x)=21x⊤Ax−b⊤x has gradient Ax−b and Hessian A. If A is positive definite, the solution of Ax∗=b is the global minimum, with value −21b⊤x∗.
Least squares ∥Mx−y∥2 is a quadratic with Hessian 2M⊤M; its first-order condition is the normal equations.