Documentation

18.18 Automatic differentiation and finite differences

nlmx uses forward-mode automatic differentiation (AD) for derivatives of the subject conditional objective with respect to ETA. AD propagates derivative values through model operations, avoiding subtraction error from tiny perturbations. This supports the intensive inner-mode searches.

The exact Laplace ETA Hessian is obtained by central differences of those AD gradients. Outer BFGS gradients are central differences of the total OFV, because population parameters affect full simulation and nested modes. The covariance step likewise uses finite differences on natural estimated-element scales.

Finite differences require a step large enough to overcome numerical noise and small enough to represent local behavior. nlmx outer gradients use (10^{-4}(1+|x_k|)) on transformed coordinates. Covariance steps use \(10^{-3}\max(|\psi_k|,10^{-3})\). ODE tolerances and inner convergence must be tighter than the signal these steps measure.

Practical interpretation

Perform a step-halving study on a representative parameter: compute the derivative with steps (h, h/2, h/4). Stable leading digits indicate a useful range; erratic values suggest solver or mode-search noise, while systematic drift suggests truncation error. Apply this check before interpreting a non-positive Hessian as purely scientific nonidentifiability.

Derivative tests should include analytic ADVAN and ODE cases, parameters near bounds, and covariance elements. Record both absolute and relative discrepancies because a derivative near zero needs an absolute criterion.

Automate these checks in release validation.

Common mistakes

Do not call every derivative “exact” because AD appears somewhere. AD differentiates implemented numerical operations; inaccurate ODE solutions still contaminate results. Avoid changing steps without a convergence study.

Checklist

  • Identify AD and finite-difference derivative layers.
  • Compare derivative steps with solver noise.
  • Test symmetry and stability of Hessians.
  • Repeat with modest step changes for fragile fits.
  • Keep transformed/natural scales distinct.

Further reading

See Chapter 8 on AD, Chapter 9 on floating point, and BFGS.