Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Two classic algorithms for solving f(x)=0f(x)=0, either as an equation or system of equations, or as FOC of an optimization problem. These will be referred to in this course a countless number of times.

Bisection methodยถ

Consider first the one-dimensional equations of the form

f(x)=0,xโˆˆ[a,b]โŠ‚R,โ€…โ€Šf(a)f(b)<0f(x) = 0, \quad x \in [a,b] \subset \mathbb{R}, \; f(a)f(b)<0

The latter condition requires that the function f(x)f(x) takes different signs at the endpoints aa and bb.

The algorithm is similar to binary search, but in continuous space.

Input: function f(x)
       brackets [a,b] such that f(a)f(b)<0
       convergence tolerance epsilon
       maximum number of iterations max_iter

Algorithm:
  step 0: ensure all conditions are satisfied
  step 1: compute the sign of the function at (a+b)/2
  step 2: replace a with (a+b)/2 if f(a)f((a+b)/2)>0, otherwise replace b with (a+b)/2
  step 3: repeat steps 1-2 until |a-b| < epsilon, or max_iter number of iterations is reached
  step 4: return (a+b)/2
Source
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
Converged in 22 steps

Bisection is slow but bulletproof: given a valid bracket it always converges, at a linear rate, halving the interval each step.

Remember this when we get to solving complex models โ€” a robust but slow method makes an excellent fallback in the complex problems.

Newtonโ€“Raphson methodยถ

The second of the two classic solution methods; this one is gradient based.

General form

f(x)=0f(x)=0

Derivation of Newton stepยถ

Remember the Taylor series expansion

f(x)=โˆ‘k=0โˆžf(k)(x0)k!(xโˆ’x0)kf(x) = \sum_{k=0}^{\infty} \frac{f^{(k)}(x_0)}{k!} (x-x_0)^k

Take the first two terms around the solution f(x)=0f(x)=0, and let x0=xix_0=x_i and x=xi+1x=x_{i+1} (step towards the solution)

0=f(x)=f(xi)+fโ€ฒ(xi)(xi+1โˆ’xi)โ‡’xi+1=xiโˆ’f(xi)fโ€ฒ(xi)0 = f(x) = f(x_i) + f'(x_i) (x_{i+1}-x_i) \quad \Rightarrow \quad x_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)}

The main idea of the Newtonโ€“Raphson method is to iterate on this equation starting from some x0x_0

xi+1=xiโˆ’f(xi)fโ€ฒ(xi),โ€…โ€Ši=1,2,โ€ฆx_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)}, \; i=1,2,\ldots

It is applicable to systems of equations, in which case xโˆˆRnx\in\mathbb{R}^n and f:Rnโ†’Rnf: \mathbb{R}^n \to \mathbb{R}^n, with the derivative replaced by the Jacobian.

Input: function f(x)
       gradient function f'(x)
Algorithm:
1. Start with some good initial value
2. Update x using the Newton step above
3. Iterate until convergence
Source
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
Converged in 7 steps

Rates of convergence and complexityยถ

  • Which of the two algorithms is intuitively faster?

  • Fundamentally why?

Continue the example above with a different callback function to print the errors at each iteration.

Source
Bisection on [-3,-0.5]
  0:  x = -1.75000000000000   |x-x*| = 7.500e-01   reported err = 2.500e+00
  1:  x = -1.12500000000000   |x-x*| = 1.250e-01   reported err = 1.250e+00
  2:  x = -0.81250000000000   |x-x*| = 1.875e-01   reported err = 6.250e-01
  3:  x = -0.96875000000000   |x-x*| = 3.125e-02   reported err = 3.125e-01
  4:  x = -1.04687500000000   |x-x*| = 4.688e-02   reported err = 1.562e-01
  5:  x = -1.00781250000000   |x-x*| = 7.812e-03   reported err = 7.812e-02
  6:  x = -0.98828125000000   |x-x*| = 1.172e-02   reported err = 3.906e-02
  7:  x = -0.99804687500000   |x-x*| = 1.953e-03   reported err = 1.953e-02
  8:  x = -1.00292968750000   |x-x*| = 2.930e-03   reported err = 9.766e-03
  9:  x = -1.00048828125000   |x-x*| = 4.883e-04   reported err = 4.883e-03
 10:  x = -0.99926757812500   |x-x*| = 7.324e-04   reported err = 2.441e-03
 11:  x = -0.99987792968750   |x-x*| = 1.221e-04   reported err = 1.221e-03
 12:  x = -1.00018310546875   |x-x*| = 1.831e-04   reported err = 6.104e-04
 13:  x = -1.00003051757812   |x-x*| = 3.052e-05   reported err = 3.052e-04
 14:  x = -0.99995422363281   |x-x*| = 4.578e-05   reported err = 1.526e-04
 15:  x = -0.99999237060547   |x-x*| = 7.629e-06   reported err = 7.629e-05
 16:  x = -1.00001144409180   |x-x*| = 1.144e-05   reported err = 3.815e-05
 17:  x = -1.00000190734863   |x-x*| = 1.907e-06   reported err = 1.907e-05
 18:  x = -0.99999713897705   |x-x*| = 2.861e-06   reported err = 9.537e-06
 19:  x = -0.99999952316284   |x-x*| = 4.768e-07   reported err = 4.768e-06
 20:  x = -1.00000071525574   |x-x*| = 7.153e-07   reported err = 2.384e-06
 21:  x = -1.00000011920929   |x-x*| = 1.192e-07   reported err = 1.192e-06

Newtonโ€“Raphson from x0=-2.5
  0:  x = -1.77142857142857   |x-x*| = 7.714e-01   reported err = 7.286e-01
  1:  x = -1.33114944950557   |x-x*| = 3.311e-01   reported err = 4.403e-01
  2:  x = -1.09877514383983   |x-x*| = 9.878e-02   reported err = 2.324e-01
  3:  x = -1.01315263007170   |x-x*| = 1.315e-02   reported err = 8.562e-02
  4:  x = -1.00028616796687   |x-x*| = 2.862e-04   reported err = 1.287e-02
  5:  x = -1.00000014027560   |x-x*| = 1.403e-07   reported err = 2.860e-04
  6:  x = -1.00000000000003   |x-x*| = 3.375e-14   reported err = 1.403e-07
-1.000000070137816

Two things stand out:

The same callback mechanism, now storing and plotting the errors instead of printing them, gives the picture:

Source
<Figure size 900x600 with 1 Axes>

On a log scale linear convergence is a straight line โ€” the error is multiplied by the same factor every iteration.

Quadratic convergence is a cliff: the number of correct digits doubles, so the curve bends down without limit until it hits machine precision.

  • Bisection took 22 iterations and Newton 7. Is that the right way to compare them?

  • The Newton curve stops at 10-14 and cannot go lower. Why?

Algorithmic complexityยถ

Letโ€™s take a loot at the algorithmic complexity of the two methods now.

  • What is the size of the problem nn for the solvers?

Given the importance of the the desired precision of the solution, the rate of convergence is therefore part of the computational complexity of the algorithm, and the two questions to ask of each method are how many iterations it needs to reach a given accuracy, and what one iteration costs.

Suppose we want to find a root xโ‹†x^\star of f(xโ‹†)=0f(x^\star) = 0 to accuracy ฮต\varepsilon.

Bisection: one binary digit per iterationยถ

Starting from an interval of width L=bโˆ’aL=b-a, each iteration halves the interval, so after kk steps the error is bounded by

ekโ‰คL2ke_k \leq \frac{L}{2^k}

To achieve ekโ‰คฮตe_k \leq \varepsilon we need L/2kโ‰คฮตL/2^k \leq \varepsilon, that is

kโ‰ฅlogโก2(Lฮต)โŸนkbisection=O(logโก1ฮต)k \geq \log_2\left(\frac{L}{\varepsilon}\right) \quad \Longrightarrow \quad k_{\text{bisection}} = O\left(\log\frac{1}{\varepsilon}\right)

Each iteration gains approximately one binary digit of accuracy, whatever the function looks like. That is the flip side of being bulletproof: the bound holds always, and it is never beaten.

Newtonโ€“Raphson: doubling the number of digitsยถ

Under certain regularity conditions the error of the Newton iteration satisfies

ek+1โ‰ˆCek2e_{k+1} \approx C e_k^2

where constant CC depends on the f(x)f(x) and fโ€ฒ(x)f'(x).

The number of correct digits approximately doubles with each iteration.

Schematically ekโˆผCโ€ฒe02ke_k \sim C' e_0^{2^k}, and to obtain ekโ‰คฮตe_k \leq \varepsilon we need 2k=O(logโก(1/ฮต))2^k = O\big(\log(1/\varepsilon)\big), hence

kNewton=O(logโกlogโก1ฮต)k_{\text{Newton}} = O\left(\log\log\frac{1}{\varepsilon}\right)

Counted in digits rather than in ฮต\varepsilon, the same statement reads: calculating a root of f(x)f(x) with pp-digit precision, provided that a good initial approximation is known, costs O(logโก(p)F(p))O\big(\log(p) F(p)\big), where F(p)F(p) is the cost of calculating f(x)/fโ€ฒ(x)f(x)/f'(x) with pp-digit precision. Both the number of iterations and the price of a single iteration grow with the precision demanded.

Note the words locally and provided that a good initial approximation is known. Everything in this subsection is conditional on being close enough to the root!

Multivariate Newton: the cost of one iterationยถ

In nn dimensions, for f:Rnโ†’Rf:\mathbb{R}^n\rightarrow\mathbb{R}, the Newton step is no longer a division but the solution of a linear system

H(xk)โ€‰ฮ”x=โˆ’โˆ‡f(xk)H(x_k)\,\Delta x = -\nabla f(x_k)

For a dense nร—nn \times n Hessian that costs O(n3)O(n^3) per iteration, so once the iterations are in the region of quadratic convergence the overall cost is roughly

O(n3logโกlogโก1ฮต)O\left(n^3\log\log\frac{1}{\varepsilon}\right)

The iteration count barely depends on the accuracy we ask for; the dimension of the problem is what hurts. This is why the structure of the Jacobian matters so much in practice โ€” sparsity, block structure, or an analytical inverse turn the n3n^3 into something affordable.

MethodConvergenceIterations for accuracy ฮต\varepsilonCost per iteration
BisectionlinearO(logโก(1/ฮต))O\big(\log(1/\varepsilon)\big)one evaluation of ff
Newtonโ€“Raphsonquadratic, locallyO(logโกlogโก(1/ฮต))O\big(\log\log(1/\varepsilon)\big)one evaluation of ff and fโ€ฒf', up to O(n3)O\big(n^3)

The key distinction between the two methods is therefore:

  • Can we get the best of the both approaches? How?

When does Newtonโ€“Raphson fail?ยถ

Newtonโ€“Raphson is fast but fragile: quadratic convergence near the root, but what happens away from the root?

Five things can go wrong, in rough order of how often they bite in economic applications:

  1. Multiple solutions โ€” the method converges, but to a root you did not want

  2. Divergence โ€” the iterations run away, or converge to a root far outside the region of interest: the domain of attraction problem

  3. Cycles โ€” the iterations enter a loop and never converge

  4. Function domain and differentiability โ€” a Newton step leaves the region where f(x)f(x) or fโ€ฒ(x)f'(x) is defined

  5. Reduced performance โ€” quadratic convergence is lost in special cases

Multiple solutionsยถ

The function of the first example has three roots.

Which one Newton returns is decided by the starting value alone, and the mapping from starting values to roots is neither monotone nor otherwise well behaved.

Starting Newton-Raphson from x0โˆˆ{โˆ’0.565,โˆ’0.580,โˆ’0.595}x_0 \in \{-0.565,-0.580,-0.595\}

Source
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>

Be aware is all that can be said here: multiple roots are a property of the problem, not of the algorithm.

In structural work this is the situation where the same solver, started from different points, produces different estimates.

Divergence and the domain of attractionยถ

arctanโก(x)=0\arctan(x)=0 has a single root at x=0x=0, and Newton still fails from a starting point that is only slightly too far out: the tangent overshoots, and each step overshoots by more than the last.

Starting values x0โˆˆ{1.25,1.50}x_0 \in \{1.25, 1.50\}

Source
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>

Cyclesยถ

The iterations can also enter a cycle, returning to the same point every second step.

In the example below I had to compute the appropriate starting value numerically (using Newton method to the equation โ€œtwo Newton steps return me where I startedโ€), but this can happen in practice due to analytical properties of the function being solved. Important to be aware of such possibility.

Source
To cycle start with x0 = -0.5689842546416416
<Figure size 900x600 with 1 Axes>

Function domain and differentiabilityยถ

Nothing in the Newton step knows where f(x)f(x) is defined. Solving logโก(x)=0\log(x)=0 from x0=2.9x_0=2.9, the very first step lands on a negative number and every value after that is nan.

Source
/tmp/ipykernel_3879/2475852609.py:1: RuntimeWarning: invalid value encountered in log
  f = lambda x: np.log(x)
<Figure size 900x600 with 1 Axes>

This is the failure mode to expect in many structural models.

The usual remedies are reparametrization (solve in expโกx\exp x rather than xx) or specialized algorithms.

Suboptimal performanceยถ

Quadratic convergence requires a simple root, fโ€ฒ(xโ‹†)โ‰ 0f'(x^\star) \ne 0. At a root of multiplicity mm the method still converges, but only linearly, at rate 1โˆ’1/m1 - 1/m. For f(x)=x9f(x)=x^9 that is 8/98/9: a hundred iterations to reach the tolerance that the first example reached in seven.

Source
<Figure size 900x600 with 1 Axes>
   0:  x =  0.88888888888889  err = 1.111111e-01
   5:  x =  0.49327018427257  err = 6.165877e-02
  10:  x =  0.27372990902883  err = 3.421624e-02
  15:  x =  0.15190065300101  err = 1.898758e-02
  20:  x =  0.08429407098405  err = 1.053676e-02
  25:  x =  0.04677722091831  err = 5.847153e-03
  30:  x =  0.02595803442990  err = 3.244754e-03
  35:  x =  0.01440486497991  err = 1.800608e-03
  40:  x =  0.00799367670345  err = 9.992096e-04
  45:  x =  0.00443592267809  err = 5.544903e-04
  50:  x =  0.00246162194645  err = 3.077027e-04
  55:  x =  0.00136602530002  err = 1.707532e-04
  60:  x =  0.00075804699540  err = 9.475587e-05
  65:  x =  0.00042066222875  err = 5.258278e-05
  70:  x =  0.00023343765198  err = 2.917971e-05
  75:  x =  0.00012954131281  err = 1.619266e-05
  80:  x =  0.00007188622564  err = 8.985778e-06
  85:  x =  0.00003989174824  err = 4.986469e-06
  90:  x =  0.00002213708626  err = 2.767136e-06
  95:  x =  0.00001228451019  err = 1.535564e-06
8.148481689595136e-06

What this means for economicsยถ

Multivariate Newton and optimizationยถ

Everything above is scalar, and everything above is about solving an equation.

But the natural multivariate problem in economics is not a system of equations โ€” it is the first order condition of an optimization problem.

Consider F(x)โ†’maxโกF(x) \rightarrow \max with xโˆˆRnx \in \mathbb{R}^n. The first order conditions for the interior optimum are

โˆ‡F(x)=0\nabla F(x) = 0

which is a system of nn equations in nn unknowns. Letโ€™s use Newton-Raphson to solve it.

Derivation from the two-dimensional Taylor expansionยถ

The scalar Newton step came from a first order Taylor expansion of ff around xix_i. In the optimization problems the step comes from a second order expansion of the objective โ€” one order higher, because we are now looking for the gradient of the derivative in the FOC.

Take n=2n=2 and write the step as (h,k)(h,k). Around the current point (xi,yi)(x_i,y_i),

F(xi+h,โ€‰yi+k)โ‰ˆF+Fxh+Fyk+12(Fxxh2+2Fxyhk+Fyyk2)F(x_i+h,\,y_i+k) \approx F + F_x h + F_y k + \tfrac{1}{2}\big(F_{xx}h^2 + 2F_{xy}hk + F_{yy}k^2\big)

where every derivative is evaluated at (xi,yi)(x_i,y_i). The right hand side is a quadratic in (h,k)(h,k) โ€” a paraboloid โ€” and we can find its stationary point exactly. Its two first order conditions are

โˆ‚โˆ‚h:โ€…โ€ŠFx+Fxxh+Fxyk=0,โˆ‚โˆ‚k:โ€…โ€ŠFy+Fxyh+Fyyk=0\frac{\partial}{\partial h}:\; F_x + F_{xx}h + F_{xy}k = 0, \qquad \frac{\partial}{\partial k}:\; F_y + F_{xy}h + F_{yy}k = 0

which is a 2ร—22\times 2 linear system for the step:

(FxxFxyFxyFyy)(hk)=โˆ’(FxFy)\begin{pmatrix} F_{xx} & F_{xy} \\ F_{xy} & F_{yy} \end{pmatrix} \begin{pmatrix} h \\ k \end{pmatrix} = -\begin{pmatrix} F_{x} \\ F_{y} \end{pmatrix}

The matrix on the left is the Hessian โˆ‡2F\nabla^2 F, the vector on the right the gradient โˆ‡F\nabla F. Solving the system by hand,

(hk)=โˆ’1FxxFyyโˆ’Fxy2(Fyyโˆ’Fxyโˆ’FxyFxx)(FxFy)\begin{pmatrix} h \\ k \end{pmatrix} = -\frac{1}{F_{xx}F_{yy}-F_{xy}^2} \begin{pmatrix} F_{yy} & -F_{xy} \\ -F_{xy} & F_{xx} \end{pmatrix} \begin{pmatrix} F_{x} \\ F_{y} \end{pmatrix}

and the iteration xi+1=xi+(h,k)x_{i+1} = x_i + (h,k) is the multivariate Newton method

xi+1=xiโˆ’(โˆ‡2F(xi))โˆ’1โˆ‡F(xi)x_{i+1} = x_{i} - \big( \nabla^2 F(x_i) \big)^{-1} \nabla F(x_i)

Three observations before any code.

For a general system G(x)=0G(x)=0 with G:Rnโ†’RnG: \mathbb{R}^n \to \mathbb{R}^n the same argument on a first order expansion of GG gives

xi+1=xiโˆ’(โˆ‡G(xi))โˆ’1G(xi)x_{i+1} = x_{i} - \big( \nabla G(x_i) \big)^{-1} G(x_i)

where โˆ‡G\nabla G is the Jacobian, the nร—nn\times n matrix of partial derivatives. Optimization is the special case G=โˆ‡FG = \nabla F, whose Jacobian is the Hessian โˆ‡G=โˆ‡2F\nabla G = \nabla^2 F.

An objective function with many critical pointsยถ

Consider the following objective function which is the sum of four Gaussian bumps, one of which is a banana-shaped ridge:

F(x,y)=โˆ‘i=14aiexpโก(โˆ’qi(x,y)),(a1,a2,a3,a4)=(1.00,โ€‰0.94,โ€‰0.78,โ€‰1.20)F(x,y)=\sum_{i=1}^{4} a_i \exp\big(-q_i(x,y)\big), \qquad (a_1,a_2,a_3,a_4) = (1.00,\,0.94,\,0.78,\,1.20)

with

q1=(xโˆ’0.22)20.018+(yโˆ’0.27)20.030,q2=(xโˆ’0.73)20.080+(yโˆ’0.25)20.012,q3=(xโˆ’0.55)20.055+[yโˆ’0.63+2.5(xโˆ’0.55)2]20.0025,q4=(xโˆ’0.55)20.005+(yโˆ’0.48)20.010\begin{aligned} q_1 &= \frac{(x-0.22)^2}{0.018}+\frac{(y-0.27)^2}{0.030}, & q_2 &= \frac{(x-0.73)^2}{0.080}+\frac{(y-0.25)^2}{0.012}, \\ q_3 &= \frac{(x-0.55)^2}{0.055}+\frac{\big[y-0.63+2.5(x-0.55)^2\big]^2}{0.0025}, & q_4 &= \frac{(x-0.55)^2}{0.005}+\frac{(y-0.48)^2}{0.010} \end{aligned}
Source
<Figure size 900x600 with 1 Axes>

Three peaks, a ridge running over the top, and long flat plains towards the corners where all four exponentials have died out. A grid search over the square finds eleven critical points: four maxima, five saddle points and two minima.

The global maximum F=1.2084F=1.2084 sits at (0.5500,0.4787)(0.5500,0.4787), on the central peak.

Gradient and Hessianยถ

Writing each term as gi=aiexpโก(โˆ’qi)g_i = a_i \exp(-q_i) saves us from differentiating four different functions. The chain rule gives, for each term,

โˆ‡gi=โˆ’giโˆ‡qi,โˆ‡2gi=gi[(โˆ‡qi)(โˆ‡qi)โŠคโˆ’โˆ‡2qi]\nabla g_i = -g_i \nabla q_i, \qquad \nabla^2 g_i = g_i \big[ (\nabla q_i)(\nabla q_i)^{\top} - \nabla^2 q_i \big]

so that

โˆ‡F=โˆ’โˆ‘i=14giโˆ‡qi,โˆ‡2F=โˆ‘i=14gi[(โˆ‡qi)(โˆ‡qi)โŠคโˆ’โˆ‡2qi]\nabla F = -\sum_{i=1}^4 g_i \nabla q_i, \qquad \nabla^2 F = \sum_{i=1}^4 g_i \big[ (\nabla q_i)(\nabla q_i)^{\top} - \nabla^2 q_i \big]

Only the derivatives of the exponents qiq_i are left to compute. For a plain Gaussian term with q=(xโˆ’c)2A+(yโˆ’d)2Bq=\frac{(x-c)^2}{A}+\frac{(y-d)^2}{B},

โˆ‡q=(2(xโˆ’c)/A2(yโˆ’d)/B),โˆ‡2q=(2/A002/B)\nabla q = \begin{pmatrix} 2(x-c)/A \\ 2(y-d)/B \end{pmatrix}, \qquad \nabla^2 q = \begin{pmatrix} 2/A & 0 \\ 0 & 2/B \end{pmatrix}

and for the ridge, writing u=xโˆ’0.55u = x-0.55 and v=yโˆ’0.63+2.5u2v = y-0.63+2.5u^2,

โˆ‡q3=(2u/0.055+4000โ€‰uv800โ€‰v),โˆ‡2q3=(2/0.055+4000โ€‰v+20000โ€‰u24000โ€‰u4000โ€‰u800)\nabla q_3 = \begin{pmatrix} 2u/0.055 + 4000\,uv \\ 800\,v \end{pmatrix}, \qquad \nabla^2 q_3 = \begin{pmatrix} 2/0.055 + 4000\,v + 20000\,u^2 & 4000\,u \\ 4000\,u & 800 \end{pmatrix}

The bumps() function above already returns qiq_i, โˆ‡qi\nabla q_i and โˆ‡2qi\nabla^2 q_i for all four terms, so the gradient and the Hessian of FF are two short loops:

Source
at (0.30,0.70):  max |G-Gnum| = 5.77e-12   max |H-Hnum| = 1.25e-10
at (0.55,0.50):  max |G-Gnum| = 5.39e-10   max |H-Hnum| = 7.92e-08
at (0.80,0.20):  max |G-Gnum| = 4.54e-10   max |H-Hnum| = 1.65e-09

The two components of the gradient, and the four elements of the Hessian, are themselves functions of (x,y)(x,y) โ€” the Newton step is a local linear approximation built from them at the current point. Note how much sharper their variation is than that of FF itself, and that H12H_{12} and H21H_{21} are the same picture, as they must be.

Source
<Figure size 1100x500 with 2 Axes>
<Figure size 1100x1000 with 2 Axes>
<Figure size 1100x1000 with 2 Axes>

Multivariate Newton solverยถ

The scalar solver carries over with two changes: the division becomes a linear solve, and the error is measured with a vector norm.

Source

Started just below the highest peak, the quadratic approximation is a good model of the objective and the solver walks straight up to it in four steps:

<Figure size 900x600 with 1 Axes>

Now move the starting point down by 0.05, still on the flank of the same peak. The first step jumps 0.2 downward into the low ground between the two lower peaks, the second lands where FF is a fifteenth of its value at the start, and from there the method wanders back up and settles on a saddle point โ€” not on the peak it started from:

<Figure size 900x600 with 1 Axes>

To see how little it takes, draw 10 Newton paths from random starting points in a small area in the center. Stars mark the paths that converged and crosses the point where a run gave up โ€” for the plain solver most of those are far outside the picture, after maxiter=100 steps.

Source
<Figure size 900x600 with 1 Axes>

The starting points are withing a square of (0.6,0.43)(0.6,0.43) with side 0.05! Only a few paths converge, to different critical points; many leave the picture, several crossing directly over a peak on the way out without stopping at it.

Newton finds critical points, not maximaยถ

The solver was given the first order condition โˆ‡F(x)=0\nabla F(x)=0, and that is all it knows. Any point satisfying it โ€” a maximum, a minimum or a saddle point โ€” is a solution, and which one is reached depends entirely on the starting value. The classification is ours to make afterwards, from the eigenvalues of the Hessian.

Source
from (0.55,0.45) --> (0.5500,0.4787)  F = 1.208  maximum  in  4 steps
from (0.55,0.40) --> (0.4236,0.2526)  F = 0.390  saddle   in  8 steps
from (0.20,0.30) --> (0.2046,0.2902)  F = 1.044  maximum  in  5 steps
from (0.75,0.25) --> (0.7300,0.2500)  F = 0.940  maximum  in  3 steps
from (0.70,0.50) --> (0.7085,0.4660)  F = 0.035  minimum  in  5 steps
from (0.55,0.65) --> (0.5500,0.6227)  F = 0.920  maximum  in  6 steps
from (0.50,0.50) --> Failed to converge in 100 iterations
from (0.10,0.80) --> Failed to converge in 100 iterations
  • Two of the starting points above are 0.05 apart and end up at different critical points, one of them not even a maximum. What does that say about the domain of attraction of each solution?

  • How would you check, in code, that the point returned is the maximum you wanted?

  • What does a nearly singular Hessian do to the length of the step?

The lesson is the one from the scalar case, only sharper in higher dimensions: the failure modes do not go away, the domains of attraction are harder to picture, and the poly-algorithm remedy โ€” a slow robust method first, Newton for the last digits โ€” is correspondingly more valuable.

Look again at the fan of paths above. What goes wrong is rarely the direction of the Newton step โ€” near the current point the local quadratic approximation is a reasonable model, and it points the right way. What goes wrong is the length. The Newton step jumps all the way to the stationary point of the quadratic approximation, and when it is a poor description of FF further out, it lands somewhere unrelated to the problem.

The remedy is to keep the direction and shorten the step. Write the full Newton step as

ฮ”i=(โˆ‡2F(xi))โˆ’1โˆ‡F(xi)\Delta_i = \big(\nabla^2 F(x_i)\big)^{-1} \nabla F(x_i)

and take

xi+1=xiโˆ’ฮปโ€‰ฮ”i,ฮปโˆˆ(0,1]x_{i+1} = x_i - \lambda\,\Delta_i, \qquad \lambda \in (0,1]

where ฮป\lambda is the step size. With ฮป=1\lambda=1 this is the method we already have; with smaller ฮป\lambda it is a damped Newton step.

Choosing ฮป\lambda by step halvingยถ

The step size is chosen by trial: take the full step, check whether it improved matters, and if not, halve it.

  1. Start with the full Newton step, ฮป=1\lambda=1

  2. If the step improves on the current point, accept it and go on to the next iteration

  3. If it does not, replace ฮป\lambda by ฮป/2\lambda/2

  4. Repeat steps 2โ€“3 until an improving step is found

The step sizes tried are therefore ฮป=1,12,14,18,โ€ฆ\lambda=1,\tfrac12,\tfrac14,\tfrac18,\ldots, so this is called a step-halving line search. It is computationally cheap: the gradient and the Hessian are computed once per iteration and reused for every trial ฮป\lambda โ€” only the cheap part, the function value, is recomputed.

We are maximizing, so what counts as an improvement is the criterion function itself: a trial step is accepted when FF goes up. This turns the solver into a genuine maximizer โ€” it can no longer converge to a minimum โ€” but it only works if โˆ’ฮ”i-\Delta_i is an ascent direction, which requires the Hessian to be negative definite. Far from the optimum it need not be, and then no ฮป\lambda improves FF and the line search fails. Forcing the curvature matrix to be definite is exactly what the quasi-Newton methods listed at the end of these notes achieve.

Exactly the picture from above โ€” the same ten starting points in the same small square, the same colors โ€” but solved with the line search component.

Also a wider choice of starting points, line search can handle a few further out.

Source
<Figure size 900x600 with 1 Axes>
<Figure size 900x600 with 1 Axes>

A completely different picture!

Nothing runs away: every path either climbs to a peak or stops, at a cross, where step halving could not find an uphill step.

The line search never returns a minimum or a saddle point โ€” it cannot, because every accepted step increases FF and neither is approached from below.

The line search is also the only one of the two that reaches the global maximum F=1.2084F=1.2084 from this cluster of starting points, and it does so in fewer iterations, having halved the step on just three of them.

The price is multiple failures. They are not failures to converge: the solver stops because no ฮป\lambda improved FF, which happens whenever the Newton direction is not an ascent direction.

That is a property of the direction, not of the step length, and shortening the step cannot cure it. The โˆ’(โˆ‡2F)โˆ’1โˆ‡F-\big(\nabla^2F\big)^{-1}\nabla F points uphill only when the Hessian is negative definite, which far from a peak it need not be.

  • Plain Newton converges more often, but half of what it returns is not a maximum. Is a method that more often gives up (fails to find suitable step size) better or worse?

  • Would step halving have rescued any of the failures of the one-dimensional Newton?

  • The full step is tried first at every iteration, and is only halved when Newton is going off. What implications does this have for the convergence properties of the method?

BFGS, BHHH and other quasi-Newton methodsยถ

The line search of the previous section can only work if the direction it is handed points uphill, and โˆ’(โˆ‡2F)โˆ’1โˆ‡F-\big(\nabla^2 F\big)^{-1}\nabla F does so only where the Hessian is negative definite. Quasi-Newton methods remove that condition by never using the true Hessian at all: they carry an approximation to it, built from the gradients already computed, and update that approximation in a way that preserves its definiteness at every iteration. The direction is then an improving one by construction, and the line search always has a step to find.

BFGS (Broydenโ€“Fletcherโ€“Goldfarbโ€“Shanno) builds up an approximation to the inverse Hessian iteratively, ensuring correct definiteness (negative for maximization and positive for minimization problems). Other approaches to approximating the Hessian, such as the DFP (Davidonโ€“Fletcherโ€“Powell) method, were developed for general optimization problems.

BHHH (Berndt-Hall-Hall-Hausman) gets the same guarantee out of statistics rather than out of the update formula.

Maximizing a log-likelihood with score contributions si=โˆ‡ฮธlnโกLi(ฮธ)s_i = \nabla_\theta \ln L_i(\theta), it uses the outer product B=โˆ‘isisiโ€ฒB = \sum_i s_i s_i' in place of โˆ’โˆ‡2lnโกL-\nabla^2 \ln L. That matrix is positive semi-definite by construction, and by the information identity it estimates โˆ’E[โˆ‡2lnโกL]-\mathbb{E}\big[\nabla^2 \ln L\big], so the step

ฮธ(g+1)=ฮธ(g)+ฮปโ€‰Bโˆ’1โˆ‘isi(ฮธ(g))\theta^{(g+1)} = \theta^{(g)} + \lambda\, B^{-1} \sum_i s_i(\theta^{(g)})

always climbs. BHHH, being based on the statistical properties of the scores, is more specific to econometric M-estimation problems โ€” and for that reason it is often not found in standard optimization packages.

Regions of attraction of the four-hills functionยถ

The solution of homework delta_newton_fractals: every starting point of a grid covering the picture is iterated at once, in the same vectorized style as the cubic example in the task notebook.

Source
CPU times: user 5.81 s, sys: 248 ms, total: 6.06 s
Wall time: 6.07 s
CPU times: user 4.81 s, sys: 118 ms, total: 4.93 s
Wall time: 5.02 s
11 critical points found

type            x        y       F   plain Newton    line search
maximum    0.5500   0.4787  1.2085           0.7%           1.1%
maximum    0.2046   0.2902  1.0443           1.7%           2.5%
maximum    0.7300   0.2500  0.9400           3.0%           4.0%
maximum    0.5500   0.6227  0.9202           1.2%           2.0%
saddle     0.5500   0.5722  0.7181           0.6%           0.1%
saddle     0.5667   0.3456  0.5019           1.3%           0.0%
saddle     0.3285   0.5019  0.4032           1.0%           0.1%
saddle     0.4236   0.2526  0.3898           2.9%           0.2%
saddle     0.8223   0.4350  0.2440           0.8%           0.1%
minimum    0.4064   0.4749  0.0663           3.1%           0.0%
minimum    0.7085   0.4660  0.0351           2.8%           0.0%
singular Hessian                           1.2%           0.0%
maxiter reached                           79.7%           0.0%
no ascent direction                        0.0%          89.9%
<Figure size 1200x1600 with 2 Axes>

Same regions at higher resolution (5000 points on x-axis)ยถ

Referencesยถ
  1. Rust, J. (1987). Optimal Replacement of GMC Bus Engines: An Empirical Model of Harold Zurcher. Econometrica, 55(5), 999. 10.2307/1911259
  2. Judd, K. L. (1998). Numerical Methods in Economics (p. 633). MIT Press. https://mitpress.mit.edu/9780262100717/numerical-methods-in-economics/
  3. Adda, J., & Cooper, R. W. (2023). Dynamic Economics: Quantitative Methods and Applications. MIT Press. https://mitpress.mit.edu/9780262547888/dynamic-economics
  4. Berndt, E. R., Hall, B. H., Hall, R. E., & Hausman, J. A. (1974). Estimation and Inference in Nonlinear Structural Models. In Annals of Economic and Social Measurement, Volume 3, Number 4 (Vol. 3, pp. 653โ€“665). National Bureau of Economic Research. https://www.nber.org/chapters/c10206