Documentation

A.9 Optimisation: transformed parameters, BFGS and derivatives

The optimiser works on an unconstrained scale

What you will learn

  • Use the transformations that turn bounded THETAs and covariance matrices into unconstrained parameters, and convert a gradient between scales.
  • Read the BFGS update, the line search and the stopping rules.
  • Compute a derivative by forward-mode automatic differentiation and by finite differences, and see where each stops being accurate.
  • Follow a Newton search for a subject's conditional mode on the appendix subject.

Prerequisites: A.3, A.4 and A.8.

Two optimisations, one inside the other

An estimation run has two nested searches.

Search Over Objective Derivatives
Inner (per subject) the subject's \(\eta_i\) \(J_i(\eta)\) exact gradient by automatic differentiation; Hessian by finite differences of gradients
Outer (whole data set) the population elements \(\psi\) \(\mathrm{OFV}(\psi)\), with each \(\hat\eta_i\) re-found central finite differences

The inner search is cheap and parallel across subjects (each subject owns its calculation); the outer search calls it for every subject at every trial point.

Transformations

The estimated elements are optimised on an unconstrained scale \(x\), so the optimiser never proposes an inadmissible value.

Element Transformation \(x\) Inverse
THETA with both bounds \((l,u)\) \(\operatorname{logit}\bigl((\theta-l)/(u-l)\bigr)\) \(\theta=l+(u-l)\,\operatorname{expit}(x)\)
THETA with a lower bound only \(\log(\theta-l)\) \(\theta=l+e^{x}\)
THETA with an upper bound only \(\log(u-\theta)\) \(\theta=u-e^{x}\)
THETA unbounded \(\theta\) \(\theta=x\)
OMEGA or SIGMA block Cholesky factor \(L\), \(\Omega=LL^{\mathsf T}\), diagonal of \(L\) on the log scale \(\Omega=LL^{\mathsf T}\)

Worked illustration. A bioavailability bounded to \((0.1,\ 1)\) with \(\theta=0.55\) has \(x=\operatorname{logit}\bigl(0.45/0.9\bigr)=\operatorname{logit}(0.5)=0\); \(\theta=0.8\) has \(x=\operatorname{logit}(0.7778)=1.2528\). A clearance with lower bound 0 and \(\theta=4\) has \(x=\log4=1.3863\), and \(\theta=0.25\) has \(x=-1.3863\): the transformation turns multiplicative changes into additive ones. A scalar variance \(\omega^2=0.09\) has Cholesky factor \(L=0.3\), stored as \(\log0.3=-1.204\).

Gradients change with the scale. By the chain rule, for \(\theta=e^{x}\) (lower bound 0),

\[ \frac{\partial\,\mathrm{OFV}}{\partial x}=\frac{\partial\,\mathrm{OFV}}{\partial\theta}\cdot\frac{d\theta}{dx}=\theta\,\frac{\partial\,\mathrm{OFV}}{\partial\theta}. \]

So if \(\partial\mathrm{OFV}/\partial\theta=-2.5\) at \(\theta=4\), the optimiser sees \(-10\). The gradient printed in an optimiser message is on the \(x\) scale and the gradient tolerance applies to it: a small gradient on the transformed scale means a small relative change of the estimate would not improve the fit. Standard errors, by contrast, are reported on the natural scale of the estimated element (page A.12).

BFGS

The outer search uses the quasi-Newton method of Broyden, Fletcher, Goldfarb and Shanno. At iteration \(k\), with current point \(x_k\), gradient \(g_k\) and an approximation \(B_k\) of the inverse Hessian:

  1. the search direction is \(d_k=-B_kg_k\);
  2. a step length \(\alpha_k\) is found by backtracking until the Armijo condition holds, \(\mathrm{OFV}(x_k+\alpha d_k)\le\mathrm{OFV}(x_k)+c\,\alpha\,g_k^{\mathsf T}d_k\) (nlmx caps the step at 2 on the transformed scale);
  3. with \(s=x_{k+1}-x_k\), \(q=g_{k+1}-g_k\) and \(\rho=1/(q^{\mathsf T}s)\), the approximation is updated:
\[ B_{k+1}=\bigl(I-\rho\,s\,q^{\mathsf T}\bigr)B_k\bigl(I-\rho\,q\,s^{\mathsf T}\bigr)+\rho\,s\,s^{\mathsf T}. \]

The update keeps \(B_{k+1}\) positive definite as long as \(q^{\mathsf T}s>0\), so every direction is a descent direction. The gradient is obtained by central differences with step \(10^{-4}(1+\lvert x_k\rvert)\); the initial \(B_0\) is diagonal, from second differences of the first gradient.

Stopping. The run stops when \(\max_k\lvert g_k\rvert\) is below the gradient tolerance (\(10^{-3}\) by default), or when the relative change of the OFV stays below \(10^{-9}\) for three iterations, or at the iteration limit (300 by default). The reason it stopped is reported, and is evidence about convergence, not proof of it (page 18.23).

Forward-mode automatic differentiation

Automatic differentiation (AD) computes the derivative of a program exactly, by applying the chain rule to every elementary operation. In forward mode every number carries a derivative along with it: a dual number \(a+b\,\epsilon\) with \(\epsilon^2=0\). Arithmetic on duals reproduces the rules of calculus:

\[ (a+b\epsilon)+(c+d\epsilon)=(a+c)+(b+d)\epsilon,\qquad (a+b\epsilon)(c+d\epsilon)=ac+(ad+bc)\epsilon,\qquad \exp(a+b\epsilon)=e^{a}(1+b\epsilon). \]

Worked illustration. The 1-hour prediction of the appendix subject is \(f(\eta)=5\exp(-0.2\,e^{\eta})\). To differentiate at \(\eta=0\), seed \(\eta=0+1\epsilon\):

Step Dual value
\(\eta\) \(0+1\epsilon\)
\(e^{\eta}\) \(1+1\epsilon\)
\(-0.2\,e^{\eta}\) \(-0.2-0.2\epsilon\)
\(\exp(-0.2-0.2\epsilon)\) \(0.818731\,(1-0.2\epsilon)=0.818731-0.163746\epsilon\)
\(5\times\) that \(4.093654\;\mathbf{-0.818731}\,\epsilon\)

The coefficient of \(\epsilon\) is the exact derivative, \(-0.818731\), obtained with no step size and no error beyond rounding. This is the \(G\) entry used on page A.8. A model with \(p\) ETAs needs \(p\) seeds (or a vector of derivatives carried together).

Finite differences: accuracy against step size

A finite difference needs a step \(h\). Too large and the formula's truncation error dominates; too small and subtraction of nearly equal numbers destroys digits.

Worked illustration: the derivative above, true value \(-0.8187308\):

\(h\) central \(\dfrac{f(h)-f(-h)}{2h}\): error forward \(\dfrac{f(h)-f(0)}{h}\): error
\(10^{-1}\) \(-6\times10^{-4}\) \(-3\times10^{-2}\)
\(10^{-2}\) \(-6\times10^{-6}\) \(-3\times10^{-3}\)
\(10^{-4}\) \(-6\times10^{-10}\) \(-3\times10^{-5}\)
\(10^{-6}\) \(1\times10^{-10}\) \(-3\times10^{-7}\)
\(10^{-8}\) \(3\times10^{-8}\) \(-2\times10^{-8}\)
\(10^{-10}\) \(-1\times10^{-6}\) \(-1\times10^{-6}\)
\(10^{-12}\) \(-2\times10^{-4}\) \(-2\times10^{-4}\)

The central error falls by a factor of 100 for each factor of 10 in \(h\) (it is of order \(h^2\)), the forward error by a factor of 10 (order \(h\)); then both rise again as rounding takes over. The best central step is around \(10^{-4}\) to \(10^{-6}\), which is why the program's steps are of that order, and why a derivative needed to many digits (the inner gradient) comes from AD instead.

Newton's method for the conditional mode of the appendix subject. Starting at the typical value, \(\eta_{k+1}=\eta_k-g_k/H_k\), with \(g=\nabla J\) and \(H=\nabla^2J\) (central differences of \(J\)):

\(k\) \(\eta_k\) \(J(\eta_k)\) \(g_k\) \(H_k\) step
0 0.000000 31.9755 328.30 2583.3 −0.12708
1 −0.127084 5.9220 113.99 1034.9 −0.11014
2 −0.237225 −1.6959 34.36 483.2 −0.07112
3 −0.308344 −3.0928 7.10 298.6 −0.02378
4 −0.332124 −3.1816 0.54 254.6 −0.00211
5 −0.334236 −3.1822 0.004 251.0 \(-2\times10^{-5}\)
6 −0.334251 −3.1822 0.000 251.0 –

Convergence is quadratic at the end (the gradient falls from 0.54 to 0.004 to 0). The mode is \(\hat\eta=-0.33425\) and the curvature there, \(H=251.0\), is what the Laplace objective and the importance-sampling proposal use. nlmx's own mode search is a Gauss–Newton iteration with a symmetric-rank-one correction and a trust region, warm-started from the previous outer iteration; the plain Newton iteration above shows the mathematics of the target, not the program's path to it.

Common mistakes

  • Reading a gradient message as if it were on the natural scale of the parameter.
  • Taking a finite-difference step "as small as possible": below about \(10^{-8}\) the result gets worse.
  • Believing that a run that "converged" (a small gradient) is at a global minimum. It is at a point where the gradient is small; restarts (18.24) and the covariance step (18.25) are the evidence.

Review checklist

  • Can you transform a bounded THETA to the optimiser's scale and back, and convert a gradient between scales?
  • Can you state the BFGS update and say what keeps it positive definite?
  • Can you differentiate a small function with dual numbers?
  • Do you know the usual step size of a central difference, and why it is not smaller?

See the previous page, the next page and the mathematical reference.