Documentation

A.18 Hand checks, and where each formula is used

Five checks and the map from formula to command

What you will learn

  • Apply five kinds of check to any formula or number: an identity, a limiting case, a finite difference, an independent calculation and a simulation.
  • Find, for each worked illustration of this appendix, the check that confirms it.
  • Look up which command uses which family of equations, and the chapter that develops it.

Prerequisites: pages A.1 to A.17, as needed.

Why check by hand

A formula in a book, a number in an output and a line of code can all be wrong in different ways. A check is useful to the extent that it is independent of what it checks: the same code run twice proves nothing, while a closed form, a limiting case or a derivative taken a second way can expose an error that a test of the program would pass. The validation chapter (Chapter 42) applies the same idea to the whole program; this page applies it to individual equations.

Five kinds of check

Check Question it asks Example from this appendix
1. An identity does the formula equal a second expression that must be equal to it? the determinant lemma against a direct determinant (A.2): \(3.3616\times10^{-4}\) both ways
2. A limiting case does it reduce to the right answer when a parameter goes to an extreme? \((e^L-1-L)/L^2\to\tfrac12\) as \(L\to0\) (A.15); the information \(n/(\sigma^2+n\omega^2)\to1/\omega^2\) as \(n\to\infty\) (A.16)
3. A finite difference does a derivative agree with the slope of the function? the gradient \(54.8430\) against a central difference (A.3); the dual-number derivative against a difference table (A.9)
4. An independent calculation does a second route, with no shared code or steps, give the same number? the exact integral by quadrature against the approximations (A.8); the NCA by hand against the segment table (A.15)
5. A simulation does the long-run average of simulated draws match the formula? the importance-sampling estimate against the exact integral (A.11)

A good habit: never trust a derivation until one of these has been applied to a number. The first three need no computer.

The worked illustrations and their checks

Every number below was recomputed with arithmetic independent of the program.

Page Quantity Value Check applied
A.1 CV of \(\omega^2=0.09\) and \(0.50\) 30.7 %, 80.5 % formula evaluated directly
A.2 \(\lvert C\rvert\) by the lemma, and by direct expansion \(3.3616\times10^{-4}\) identity (1)
A.2 \(r^{\mathsf T}C^{-1}r\) by Woodbury and by solving 3.5151 identity (1)
A.3 \(\partial J/\partial\eta\) at \(-0.2\) 54.8430 finite difference (3)
A.4 Cholesky factor of a \(2\times2\) \(\Omega\) \(L_{22}=0.17321\) \(LL^{\mathsf T}\) reproduces \(\Omega\) (1)
A.6 exact \(\mathrm{OFV}_i\) of the appendix subject \(-0.8150\) quadrature (4)
A.8 FO, FOCE, FOCE-I, Laplace \(-1.283,\ -1.602,\ -1.087,\ -0.758\) against the exact value (4)
A.9 Newton mode search \(\hat\eta=-0.33425\) gradient zero, curvature 251.0 (3)
A.9 forward-mode derivative \(-0.818731\) finite differences at several steps (3)
A.10 step-size average 0.0350 equals the arithmetic mean (2)
A.11 importance-sampling \(\mathrm{OFV}_i\) \(-0.8151\) at \(M=3000\) exact value (4, 5)
A.12 sandwich SE of a mean 0.363 \(\times\sqrt{n/(n-1)}\) gives the textbook 0.383 (2)
A.12 delta-method SE of a ratio 1.00 direct expansion of the variance (1)
A.13 AIC, BIC variants and weights tables formulas evaluated directly
A.14 first outputs of the generator from a zero state 0xE220A8397B1DCDAF the value commonly used as a test vector (4)
A.15 \(\lambda_z\) of the 4-point window 0.12498 \(S_{xy}/S_{xx}\) by hand (4)
A.15 \(\mathrm{AUC}_\infty\), CL/F, \(V_z\)/F 64.935, 1.540, 12.32 components recomputed (4)
A.16 Wald power at \(\lambda=6.25\) 0.7054 Poisson-mixture series, and the non-central distribution function (4)
A.17 Cox \(\ell_p(0)\) \(-7.7142\) \(-\sum\ln n_t\) by hand (1)

From formula to command

Each family of equations belongs to some commands. The chapters named are where the method is developed and the case studies are in Chapter 41.

Equations Page of this appendix Commands that use them Developed in
matrix identities; Cholesky A.2, A.4 fit, sims, power, vpc, forest Chapter 18
likelihood, OFV, record types A.6, A.7 fit, vpc, power Chapter 18
FO, FOCE, FOCE-I, Laplace A.8 fit Chapter 18
optimisation, transforms, differentiation A.9 fit Chapter 18
SAEM and importance sampling A.10, A.11 fit Chapter 19
covariance step, delta method, tests A.12 fit, forest, er Chapter 18, Chapter 26
information criteria, likelihood-ratio tests A.13 screen, cov, er Chapter 17, Chapter 22
random streams, simulation, quasi-random draws A.14 sims, power, vpc, generate, cov, posterior Chapter 9, Chapter 24, Chapter 28
NCA A.15 nca, autoinit Chapter 14, Chapter 15
information, power, SSE A.16 power Chapter 30, Chapter 31
exposure–response models A.17 er Chapter 32, Chapter 33

Equations of the program that are not in this appendix, because they are specific to one command, are in each command's own chapter: the data checks (Chapter 11), the control-stream checks (Chapter 12), the descriptive statistics and smoothers (Chapters 13 and 13A), the covariate engines (Chapters 22 to 25) and the exposure metrics of sims (Chapter 28).

When a check fails

Symptom Likely cause First action
hand value and program value differ in the 3rd–4th digit a different rule (linear against log trapezoid; an inclusive against an exclusive window) restate the rule used on each side; page A.15
differ by a constant an omitted constant (\(n_{\text{obs}}\log2\pi\)) or a different likelihood convention page A.6
differ by a factor of 2 or 4 OFV is \(-2\ell\): a missed factor in \(R\) or \(S\) page A.12
differ by a factor \(\sqrt{n/(n-1)}\) maximum-likelihood against unbiased variance page A.5
agree for one subject and differ for the total a Monte Carlo error that accumulates page A.11
differ only for large effects or small samples an asymptotic approximation at its limit page A.12
differ at random an unseeded or shared random stream page A.14

Review checklist

  • Can you name the five kinds of check and give a case of each?
  • Have you applied at least one of them to a number before relying on a derivation?
  • When a hand value and a program value differ, can you say which of the common causes in the table fits?

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