The Newton step
Gradient descent uses only the slope, so it cannot tell a narrow valley from a round bowl. Newton's method uses the curvature too. Near the current point , lesson 4.3.1 copies by a quadratic in the step :
with and taken at . This is lesson 4.4.1's quadratic in , with and .
If is positive definite, the copy's minimum is where . Newton's method jumps to that minimum, then builds a new copy there.
Rule: Newton's method
It is often written . Compute it with np.linalg.solve (lesson 3.3.1): forming costs more and adds rounding. The curvature sets the length of the step, so there is no to choose.
In one variable, is the number and is , so the step is . That is lesson 2.4.3's Newton's method for a critical point.
A quadratic takes one step
For the copy is exact, so one step lands on the minimum from any start. Take lesson 4.4.1's , with . At the gradient is , so the step solves
which gives and , the minimum. From the step is , and from it is : both land on . Gradient descent needed 22 steps at its best step size (lesson 4.4.2).
Lesson 2.4.3's quartic, in two variables
Take of lesson 4.4.1, with and . This is diagonal, so the Newton step splits into two one-variable steps.
For the step is : lands on 0 at once, because is a quadratic. For it is lesson 2.4.3's step .
From the values are 1.166667, 1.132616, 1.130905 and 1.130901, exactly those of lesson 2.4.3. The end is the local minimum .
Try it
newton_step solves with np.linalg.solve. The first loop takes one step on the quadratic from three starts; each lands on .
The second loop takes four steps on the quartic from .
How fast: the digits double
Lesson 4.4.2's curved valley, , has its minimum at and a condition number of about 133 there. Its gradient and Hessian are
At , and , with eigenvalues 2.9052 and 24.0948. Solving gives . The copy promised there; the true value is 0.1296.
| step | error | correct digits | ||
|---|---|---|---|---|
| 0 | 0.5625 | 1.118 | ||
| 1 | 0.1296 | 0.704 | 0.2 | |
| 2 | 0.04236 | 0.214 | 0.7 | |
| 3 | 0.000808 | 0.0636 | 1.2 | |
| 4 | 0.0014 | 2.9 | ||
| 5 | 5.3 | |||
| 6 | 11.0 |
The correct digits, of the error, grow slowly at first and then about double: 1.2, 2.9, 5.3, 11.0. This is the quadratic convergence of lesson 2.4.3. Near a minimum where is positive definite, each error is roughly a constant times the square of the last.
From the same start, gradient descent with needs 2186 steps to bring below ; Newton needs 6. The valley's narrowness does not slow Newton down: measures the curvature in every direction and sizes the step for each. That is the rescaling lesson 4.4.2 asked for.
Each Newton step jumps to the minimum of the quadratic copy, whose level curves are the dashed ellipses. Where the Hessian is indefinite, plain Newton heads for the nearest critical point, here a saddle; a safeguarded step goes downhill instead.
Try it
The loop takes six Newton steps on the curved valley from and prints the point, , the error and the correct digits.
The last lines run gradient descent from the same start and count its steps.
When H is not positive definite
Newton's method solves , so it heads for the nearest critical point of any kind. Where is not positive definite, the copy has no minimum, and two things go wrong.
It can land on a saddle or a maximum. For the quartic at , is indefinite. The steps are lesson 2.4.3's from 0: 0.166667, 0.169935, 0.169938. They reach the saddle of lesson 4.4.1, which is no minimum.
Its step can go uphill. When is positive definite, , so the step points downhill (lesson 4.2.1). Otherwise it may not. On the curved valley at , has determinant and eigenvalues and 97.3083.
There the Newton step goes to , where , up from 6.5625. Plain Newton does reach after 8 steps from this start, but it passes on the way. That was luck.
Lesson 4.4.2's explorer has a Newton's method switch. Turn it on at the valley's start and set 1 step, then 8. The first step leaves the picture uphill; by step 8 the path is back at .
Rule: A safeguarded Newton step
Test with np.linalg.cholesky, which fails unless is positive definite (lesson 3.7.2). Every step now lowers , so the method heads for a minimum. Near one, is positive definite and the full Newton steps take over.
With the safeguard, the quartic from takes one gradient step, to . Then the Newton steps give , , , : lesson 2.4.3's steps from , ending at the global minimum . The curved valley from takes 12 steps, all downhill.
Try it
The first lines take the plain Newton step on the valley at : has a negative eigenvalue, , and rises.
safe_newton follows the rule above and returns the point and the number of steps. It runs on the valley and on the quartic.
The cost of a step
Newton takes few steps, but each one costs more. A gradient-descent step needs the gradient, numbers.
A Newton step also needs the Hessian, which has different entries because it is symmetric. It then solves an system, about multiplications by elimination (lesson 3.3.1).
| variables | gradient entries | Hessian entries | solving, about |
|---|---|---|---|
| 10 | 10 | 55 | 333 |
| 100 | 100 | 5,050 | |
| 1,000 | 1,000 | 500,500 | |
| 1,000,000 |
With up to a few thousand variables, Newton's handful of steps wins. With millions, as in a large machine-learning model, the Hessian cannot even be stored, and gradient methods are the only choice.
Between the two sit quasi-Newton methods. The best known, BFGS, builds an estimate of from the gradients of earlier steps, so it needs only gradients and still converges fast.
scipy.optimize.minimize
minimize(f, x0) from scipy.optimize returns a result with the minimiser .x, the value .fun, the number of iterations .nit, and .success with a .message. Without bounds or constraints it uses BFGS. Given no gradient, it estimates one by finite differences, as in lesson 2.8.1; jac=grad passes yours, which saves evaluations of .
method="Newton-CG" with hess=hess takes Newton steps. It solves by an iterative method instead of elimination, which suits a large .
On the curved valley from , BFGS takes 17 iterations: 66 evaluations of without a gradient, and 22 with one. Newton-CG takes 23. The counts can differ slightly with the SciPy version.
minimize finds a local minimum near its start. On the quartic from it returns , with , the minimum that is not global (lesson 4.4.1).
From it returns the global one, . Case study 4.4.5 uses minimize on a yield curve.
Try it
The first three results are the curved valley: BFGS with an estimated gradient, BFGS with jac, and Newton-CG with the Hessian too. Each line prints success, the answer and the counts.
The last loop runs BFGS on the quartic from two starts.
Common mistakes
- Do not invert . Solve with
np.linalg.solve. - Mind the sign. The step solves ; with it goes the wrong way.
- Newton finds critical points, not minima. From on the quartic it lands on a saddle; check at the answer (lesson 4.4.1).
- Away from the minimum, may be indefinite and the step may go uphill. Test , fall back to a gradient step, and halve until falls.
- A wrong Hessian spoils the speed. Check it against central differences of the gradient (lesson 4.1.2).
- Few steps is not the same as cheap. Each step forms and solves a system; with many variables, use BFGS or gradient methods.
successis not a proof.minimizereports a local minimum near its start; try other starts.
Newton's method in several variables
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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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
- Newton's method jumps to the minimum of the quadratic copy: solve , then . In one variable it is lesson 2.4.3's .
- A quadratic with positive definite is solved in one step. Near a minimum with positive definite, the correct digits about double each step, however narrow the valley.
- Newton finds critical points of any kind. Where is not positive definite it can land on a saddle or step uphill. The safeguard: a gradient step instead, and halving until falls.
- A Newton step costs a Hessian, entries, and a solve, about multiplications. Gradient descent takes many cheap steps; BFGS sits between, building from gradients.
scipy.optimize.minimize(f, x0, jac=grad)uses BFGS by default;method="Newton-CG"withhesstakes Newton steps. It returns a local minimum near the start.