Documentation

8.15 Predictions, residual error and likelihood records

For compartment models, F is the selected compartment amount divided by its scale. $ERROR receives F, amounts, data, parameters and persistent locals, and must assign Y. With $ERROR (OBS ONLY), non-observation records skip error evaluation; otherwise $ERROR can run on other record types while only observations contribute outputs.

Record-to-objective likelihood path

Figure 8.6. State propagation, $ERROR, residual variance and F_FLAG are separate checkpoints. Compare them record by record before attributing a total-objective difference to the optimizer.

Residual variance

At estimation, $ERROR is evaluated at EPS=0. nlmx obtains \(h=\partial Y/\partial EPS\) using symmetric finite differences, then calculates

\[ V=h^T\Sigma h. \]

This is exact for ordinary additive, proportional and combined models that are linear in EPS. Nonlinear EPS transformations are approximated at zero and require step-sensitivity checks. The EPS step is \(10^{-3}\max(\sqrt{|\sigma_{kk}|},10^{-6})\).

For Gaussian records, the objective contribution is

\[ \log V + (DV-Y)^2/V. \]

Nonpositive or non-finite residual variance fails with a hint, commonly exposing a proportional-only model at zero prediction.

F_FLAG

F_FLAG=0 uses the Gaussian contribution. F_FLAG=1 treats positive Y as a likelihood and contributes -2 log(Y), as in M3 censoring. F_FLAG=2 treats Y as a direct objective contribution. Other values are unsupported. Likelihood records require the Laplace approximation in the fitting workflow.

Conditional objectives

The subject objective adds the ETA prior \(\eta^T\Omega^{-1}\eta\). FO, FOCE, FOCE-I and Laplace differ in linearisation point, residual-variance evaluation and curvature adjustment. The model engine supplies predictions and derivatives; Chapter 18 will cover estimation theory in depth.

Common mistakes

  • Calling EPS derivatives automatic differentiation; they are symmetric differences.
  • Using a proportional-only error at an exact zero prediction.
  • Simulating an F_FLAG likelihood model as though Y were an observation.

Checklist

  • Derive F, Y, EPS derivatives and variance separately.
  • Verify F_FLAG on every special record.
  • Match likelihood approximation across software.

Further reading

See automatic differentiation, simulation limits and Chapter 18's planned likelihood theory.