# The code variable in the nlm() function

In R there is a function nlm() which carries out a minimization of a function f using the Newton-Raphson algorithm. In particular, that function outputs the value of the variable code defined as follows:

code an integer indicating why the optimization process terminated.

1: relative gradient is close to zero, current iterate is probably solution.

2: successive iterates within tolerance, current iterate is probably solution.

3: last global step failed to locate a point lower than estimate. Either estimate is an approximate local minimum of the function or steptol is too small.

4: iteration limit exceeded.

5: maximum step size stepmax exceeded five consecutive times. Either the function is unbounded below, becomes asymptotic to a finite value from above in some direction or stepmax is too small.

Can someone explain me (maybe using a simple illustration with a function of only one variable) to what correspond situations 1-5?

For example, situation 1 might correspond to the following picture:

-

These situations are understood more clearly when having in mind what minimisation or maximisation really is and how optimisation works.

Suppose we have function $f$ which has local minimum at $x_0$. Optimisation methods try to construct the sequence $x_i$ which converges to $x_0$. It is always shown that in theory the sequence constructed converges to the point of local minimum for some class of functions $f$.

To obtain next candidate in iteration $i$ can be a lengthy process, so it is usual that all algorithms limit the number of iterations. This corresponds to situation 4.

Then for each $x$ close to $x_0$ we have that $f(x)>f(x_0)$. So if $f(x_i)>f(x_{i-1})$ this is an indication that we reached the minimum. This corresponds to situation 3

Now if function $f$ has a derivative at $x_0$ then necessarily $\nabla f(x_0)=0$. Newton-Raphson method calculates gradient at each step, so if $\nabla f(x_i)\approx 0$, $x_i$ is probably a solution, which corresponds to situation 1.

Each convergent sequence of real vectors is Cauchy sequence and vice versa, roughly meaning that if $x_i$ is close to $x_0$, then $x_i$ is close to $x_{i+1}$ and vice versa, where $i$ is the iteration number. So if $|x_i-x_{i-1}|<\varepsilon$, and we know that in theory $x_i$ converges to $x_0$, then we should be close to the minimum point. This corresponds to situation 2.

Converging sequences have the property that they contract, i.e. if we are close to convergence all the remaining elements of the sequence are contained in small area. So if the sequence which in theory should converge starts to take large steps this is an indication that there is no convergence probably. This corresponds to situation 5

Note Strict mathematical definitions were left out intentionally.

-
Thank you very much for such a nice answer! –  ocram Feb 8 '11 at 18:38
@Marco, you're welcome. –  mpiktas Feb 8 '11 at 19:19