Documentation

42.10 Numerical-accuracy case studies

Two accuracy checks, drawn from computed examples

What you will learn

  • Check an objective function against an exact marginal likelihood, and see the Laplace approximation converge as the random effect shrinks.
  • Understand why a log-trapezoid fails on a nearly flat profile in naive arithmetic, with measured errors.
  • Verify that three ways of solving the same model agree.
  • Reproduce standard errors and the OFV of a linear model from the normal equations.
  • Recompute individual predictions from the closed-form solution.

Prerequisites: 42.6 Tolerance policy, Chapter 9 numerical core and Chapter 18 estimation theory.

The five case studies below are the package's High-risk numerical tests. For each, the reference is independent of the program, and the numbers shown were recomputed for this page.

Case 1 — The objective function against an exact marginal likelihood (OQ-FIT-002)

Hazard (RK-01). A wrong objective is plausible-looking and silent.

Reference. For a model with a single random effect η, the marginal likelihood of one individual is an integral over η:

L_i = ∫ p(y_i | η) φ(η; 0, ω²) dη.

That one-dimensional integral can be computed to high accuracy by the trapezoid rule on a fine grid (4001 points to ±8 standard deviations, in the package's test), independently of any approximation. The package stores the result for eight subjects and 56 observations: −2 log L = −144.2408 (the n·ln 2π constant omitted, as in NONMEM and in the program). The sum of the eight per-subject contributions reproduces the total to the last printed digit (−144.24078830288852), which is itself a consistency check on the reference.

Acceptance. FOCE-I and Laplace within 0.5 of the exact value at ω² = 0.04.

Why the Laplace approximation converges. The Laplace approximation replaces the integrand by a Gaussian at its mode. It is exact if the integrand is Gaussian, and the integrand becomes ever more Gaussian as the random effect shrinks, because the model is then essentially linear in η over its support. You can see this in an independent recomputation: a fresh data set of eight subjects was simulated at each variance level from a one-compartment oral model with a random effect on clearance (CL 4 L/h, V 40 L, K_A 1.2 /h, 10 samples to 24 h, proportional error variance 0.0225), and both the exact integral and the Laplace formula were evaluated.

ω² Exact −2LL Laplace −2LL Laplace − exact
0.16 −16.9806 −16.6760 +0.3046
0.04 −27.4427 −27.2274 +0.2153
0.0025 −52.3283 −52.3421 −0.0138
0.0001 −61.9641 −61.9645 −0.0004

(Each row is a different simulated data set; the comparison within a row is what matters.) The difference falls by three orders of magnitude as ω² falls by three. This is the justification for the package's two tolerances: 0.5 where the approximation is merely good (ω² = 0.04), and 0.005 at ω² = 1e-4 where it must be essentially exact (the package's expected value at that variance is −118.5748). A tolerance chosen from the method's behaviour, not from the observed run.

Case 2 — A nearly flat profile (OQ-NCA-007)

Hazard (RK-35). The linear-up/log-down rule integrates a declining segment from C₁ to C₂ over Δt as

AUC = (C₁ − C₂) · Δt / ln(C₁ / C₂).

When C₁ ≈ C₂, both the numerator and the logarithm are tiny differences of nearly equal numbers; in double precision most of their digits cancel. Take a segment of 24 h that falls from 100 to 100(1 − ε). The table compares the textbook formula with a numerically stable one (which uses the logarithm of 1 − δ in a cancellation-free form), each against the same integral in 80-digit decimal arithmetic:

ε (fractional drop) Naive relative error Stable relative error
1e-3 7.6e-14 1.8e-16
1e-6 2.8e-11 1.1e-16
1e-9 8.1e-8 4.7e-17
1e-12 7.1e-5 2.4e-17
1e-15 1.2e-1 7.1e-17

At ε = 1e-15 the naive formula is wrong by 12 %; the stable formula is correct to rounding. The error of the naive form grows by about three orders of magnitude for every three of ε. A test that only used a typical profile (ε of 0.1 or more) would never see it.

The test. Three subjects, nearly flat and declining slowly, one at normal scale (last concentration 0.25 at 24 h, AUC to the last sample 12.156), one with large values (3·10⁶ early on, last concentration 7.5·10⁵ at t = 1023) and one tiny (last concentration 2.5·10⁻¹⁰), are compared with the integral computed in 80-digit decimal arithmetic from the closed-form integral of the log-linear interpolation. The expected values include, for subject 2, an AUC to the last sample of 1 534 968 510.733 and, for subject 3, 1.2156 × 10⁻⁸: a ratio of 1.3 × 10¹⁷ between two subjects processed by the same code path. The package requires all six areas within 1e-9 relative. The extreme scales also catch scaling mistakes (a unit conversion applied twice, an absolute tolerance that is meaningless at 10⁻⁹).

Case 3 — Three solvers, one answer (OQ-FIT-006)

Hazard (RK-03). The analytic solution, the matrix-exponential solver for linear $DES models and the adaptive ODE solver are different code paths. Writing the same one-compartment oral model three ways must give the same objective function at the same parameters.

The recomputed concentrations (page 42.6) show the analytic and matrix-exponential results agreeing to about 1e-15 relative at 0.5, 2, 8 and 24 h. The test makes three independent claims by reading the logs and the summary: the linear model was read as a general $DES model, was solved by the matrix exponential, and the non-linear variant was not (the adaptive integrator was used). Then the relative OFV differences are required below 1e-9 (matrix exponential) and 1e-7 (ODE). Testing that the right path was taken is as important as testing the number: a silent fall-back to the wrong solver could pass a loose numerical test and fail a tight one only by accident.

Case 4 — Exact standard errors of a linear model (OQ-FIT-012)

Hazard (RK-33). Standard errors are silent: a wrong one looks as plausible as a right one.

Reference. A linear regression written as a $PRED model has a closed-form solution. For 180 observations with an intercept and two slopes, the package's expected values come from the normal equations:

Quantity Value
Intercept (true 1.5) 1.574333
Slope on AMT (true 0.8) 0.786329
Slope on X (true −0.3) −0.300659
Residual variance s² = RSS/n 0.1615331
OFV = n(ln(RSS/n) + 1) −148.1481
SE of intercept 0.0723730
SE of the slope on AMT 0.0087528
SE of the slope on X 0.0106611
SE of s² = √(2 s⁴ / n) 0.0170271

The OFV formula is easy to verify: 180 · (ln 0.1615331 + 1) = 180 · (−1.82337 + 1) = −148.148, matching the expected value. With the inverse-Hessian covariance (--cov-matrix r) the program's standard errors must equal these to 1e-5 relative (1e-4 for the variance). The same fixture also serves OQ-FIT-011: the data column is called AMT, a name NONMEM reserves for the dose, and a $PRED model must treat it as an ordinary regressor.

The test is deliberately limited to the linear model, where exact standard errors exist. For non-linear models the covariance step uses numerical derivatives and the package says so in its residual-risk table: compare key standard errors with a bootstrap or SIR for a submission model.

Case 5 — Predictions recomputed from the closed form (OQ-FIT-003)

Hazard (RK-02). Diagnostics are built on IPRED; a wrong IPRED poisons every residual.

Reference. After a fit, IPRED for every observation is recomputed from the closed-form one-compartment oral solution using the final estimates and each individual's ETAs (and, for the covariate model, the individual's weight). For a typical subject at the generating values the closed form is

C(t) = D · K_A / (V (K_A − k)) · (e^(−k t) − e^(−K_A t)), with k = CL / V.

For a typical subject (η = 0, no weight effect) with CL = 4, V = 40, K_A = 1.2 and D = 100 this gives T_max = ln(K_A / k) / (K_A − k) = 2.259 h and C_max = 1.994 mg/L, and AUC∞ = D / CL = 25. The test recomputes 1500 observations and requires the largest relative difference below 1e-5, once for the base model and once for the covariate model with the weight exponents. A count check ("1500 observations") guards against the calculator silently skipping rows.

What the five have in common

Pattern Seen in
The reference is mathematical, not another program all five
The tolerance comes from the method's own behaviour Cases 1, 3
The input is chosen to be adversarial Case 2 (flat, extreme scales)
The test proves the right code path ran, not just the right number Case 3
A count check guards against silent skipping Case 5
The scope is stated honestly (linear models only) Case 4

Review checklist

  • For each numerical test, can you name the closed form or independent calculation?
  • Does an adversarial input (flat profile, extreme scale, tiny variance) exist for every High-risk numerical routine?
  • Is the code path verified as well as the value?
  • Are limits of exactness (e.g., non-linear standard errors) written down?

See the previous page, the next page and the complete validation reference.