Documentation

A.14 Random numbers and simulation: streams, generators, draws and Monte Carlo precision

From a seed and a label to a simulated observation

What you will learn

  • Follow a random stream from its key (a seed and a text label) through the generator to a uniform, a standard normal and a correlated multivariate draw.
  • Compute a few draws by hand, including a quasi-random (randomised Halton) sequence and its antithetic alternative.
  • Simulate observations from the mixed-effects model and with parameter uncertainty.
  • Judge how many replicates a simulated summary needs.

Prerequisites: A.4, A.5 and A.6.

Streams: the rule that makes results independent of threads

Every random stream is keyed by a seed \(s\) and a text label \(\ell\) (for example "sims-parameters|3", a purpose and an index), and never by the thread that happens to run it. The initial state is

\[ x_0=\bigl(s\cdot\texttt{0x9E3779B97F4A7C15}+h(\ell)+v_{\text{rng}}\bigr)\ \big\lvert\ 1\pmod{2^{64}}, \]

where \(h\) is a hash of the label, \(v_{\text{rng}}\) is the version of the derivation, and \(\lvert\,1\) sets the lowest bit. Three properties follow, and each is a testable promise (page D.12):

Property Consequence
a stream depends on \((s,\ell,v_{\text{rng}})\) only the same seed and settings give the same numbers on any number of threads
different labels give different streams adding a new consumer of random numbers does not shift the draws of the existing ones
the derivation has a version a change of the derivation changes every result, so a checkpoint written under another version is refused when a run is resumed

The generator: SplitMix64

The generator advances a 64-bit state and scrambles it with two multiply–xor-shift rounds:

\[ x\leftarrow x+\texttt{0x9E3779B97F4A7C15},\qquad z=x,\quad z\leftarrow(z\oplus z\gg30)\cdot\texttt{0xBF58476D1CE4E5B9},\quad z\leftarrow(z\oplus z\gg27)\cdot\texttt{0x94D049BB133111EB},\quad \text{output }z\oplus z\gg31, \]

all arithmetic modulo \(2^{64}\) (\(\oplus\) is exclusive-or, \(\gg k\) a right shift by \(k\) bits). The increment is the 64-bit fraction of the golden ratio, which spreads consecutive states well.

From bits to numbers

Target Construction
Uniform on \((0,1)\) \(u=\bigl(\lfloor w/2^{11}\rfloor+0.5\bigr)/2^{53}\) for a 64-bit output \(w\): 53 bits, shifted by half a step, so \(u\) is never exactly 0 or 1 (a logarithm or an inverse distribution function is always defined)
Standard normal (Box–Muller) \(z=\sqrt{-2\log u_1}\,\cos(2\pi u_2)\)
Integer in \([0,n)\) Lemire's multiply-and-reject method, which has no modulo bias
Shuffle Fisher–Yates, driven by the integer generator
\(N(0,S)\) \(x=Lz\), \(z\sim N(0,I)\), \(LL^{\mathsf T}=S\) (page A.4)

Worked illustration. The generator started from the state \(x=0\) (an example of the algorithm, not a stream of the program, whose states come from the key above) gives the outputs

\[ w_1=\texttt{0xE220A8397B1DCDAF},\qquad w_2=\texttt{0x6E789E6AA1B965F4}. \]

The first value is the one commonly used as a test vector for this generator from a zero state, which makes it a check on an implementation. The two uniforms are \(u_1=0.883311\) and \(u_2=0.431528\), and the Box–Muller normal is

\[ z=\sqrt{-2\log0.883311}\ \cos(2\pi\times0.431528)=0.49815\times(-0.90887)=-0.4528 . \]

(Box–Muller produces a pair of normals from two uniforms; the sine form gives the second.)

Correlated and parameter-uncertain draws

Between-subject and residual effects. A simulated observation is

\[ y^{\text{sim}}_{ij}=Y_{ij}(\eta_i,\varepsilon_{ij}),\qquad\eta_i=L_\Omega z_i,\quad\varepsilon_{ij}=L_\Sigma z'_{ij}, \]

the same structure as $SIMULATION in NONMEM.

Worked illustration. The appendix subject with standard-normal inputs \(z=0.5\) for the ETA and \(z'=(0.5,\ -1,\ 0.25)\) for the three residuals, with a proportional SD of \(0.2\): \(\eta=0.3\times0.5=0.15\); the predictions at \(\eta=0.15\) are \(f=(3.963,\ 1.974,\ 0.308)\) at 1, 4 and 12 h; \(\varepsilon=0.2z'=(0.10,\ -0.20,\ 0.05)\); so

\[ y^{\text{sim}}=f(1+\varepsilon)=(4.360,\ 1.579,\ 0.323). \]

Parameter uncertainty. To include the imprecision of the estimates in a simulation (sims, power, forest, vpc), the parameters themselves are drawn: \(\psi^\ast=\hat\psi+Lz\) with \(LL^{\mathsf T}=\widehat{\operatorname{Cov}}(\hat\psi)\) from the covariance step. Draws that are inadmissible (for example a negative variance) are redrawn up to a limit. If \(\widehat{\operatorname{Cov}}\) is slightly indefinite, the robust Cholesky of page A.4 repairs it and the repair is reported.

Quasi-random and antithetic draws

Posterior latent draws (the covariate and posterior-stability engines, and the visual predictive check with uncertainty) can use alternatives to independent normals. For subject \(i\) and draw index \(m\), the standard-normal vector \(z_{im}\in\mathbb R^p\) is one of:

Method Definition
normal independent normals from the stream of the label and the draw index
antithetic_normal draws \(2k\) and \(2k+1\) share a stream; the odd one is exactly \(-z\)
randomised Halton (the quasi-random options) \(u_{ik}=\operatorname{frac}\bigl(R_{b_k}(m+1)+\delta_{ik}\bigr)\), \(z_{ik}=\Phi^{-1}(u_{ik})\)

The van der Corput radical inverse in base \(b\) reflects the digits of \(n\) about the radix point:

\[ n=\sum_jd_jb^{j}\ \Longrightarrow\ R_b(n)=\sum_jd_jb^{-(j+1)} , \]

with a different prime base \(b_k\) (2, 3, 5, 7, …) for each dimension \(k\), and a per-subject random shift \(\delta_{ik}\sim U(0,1)\) (a Cranley–Patterson rotation) that restores unbiasedness and gives a measure of error by repetition. The \(u\)'s are clamped to \([10^{-12},\,1-10^{-12}]\).

Worked illustration. The first eight points of base 2 are \(R_2(1..8)=0.5,\ 0.25,\ 0.75,\ 0.125,\ 0.625,\ 0.375,\ 0.875,\ 0.0625\) (for \(n=5=101_2\): \(1\cdot2^{-1}+1\cdot2^{-3}=0.625\)) and the first six of base 3 are \(0.333,\ 0.667,\ 0.111,\ 0.444,\ 0.778,\ 0.222\). Pairing them gives a two-dimensional low-discrepancy set which fills the unit square more evenly than random points:

\(m+1\) 1 2 3 4 5 6
\((R_2,R_3)\) (0.500, 0.333) (0.250, 0.667) (0.750, 0.111) (0.125, 0.444) (0.625, 0.778) (0.375, 0.222)
shifted by \(\delta=(0.1,\ 0.2)\) (0.600, 0.533) (0.350, 0.867) (0.850, 0.311) (0.225, 0.644) (0.725, 0.978) (0.475, 0.422)

and, for example, \(\Phi^{-1}(0.25)=-0.6745\), \(\Phi^{-1}(0.75)=0.6745\), \(\Phi^{-1}(0.125)=-1.1503\), \(\Phi^{-1}(0.0625)=-1.5341\). An antithetic pair \(\pm z\) has an exactly zero mean in every coordinate: any estimate that is odd in \(z\) (a mean of a linear function) is exact for the pair.

How many replicates?

A simulated summary is itself an estimate with Monte Carlo error. Two rules cover most cases.

A proportion (a coverage, a power, a fraction in range) from \(R\) replicates has SE \(\sqrt{p(1-p)/R}\), and an interval from the Wilson formula (page A.5).

A quantile \(q_p\) (a prediction band, a median exposure) has SE

\[ \mathrm{SE}(\hat q_p)\approx\frac{\sqrt{p(1-p)/R}}{f(q_p)}, \]

with \(f\) the density at the quantile: the tails are far less precise than the centre, because the density there is small. Illustration: for a standard normal, the 5th percentile (\(q=-1.645\), \(f=0.1031\)) has SE \(0.211\) with 100 replicates, \(0.067\) with 1000 and \(0.021\) with 10 000; the median (\(f=0.399\)) with 1000 replicates has SE \(0.040\). A band built on the 5th and 95th percentiles of 100 simulated curves is wobbly in a way that a median line is not; this is why a predictive check or a design study needs many replicates before its band edges or tail probabilities can be trusted.

The program's quantile is the type-7 (linear interpolation) rule: position \(p(n-1)\) on a 0-based scale, interpolating between neighbouring order statistics.

Common mistakes

  • Seeding a program from the clock and not recording the seed: the run can then never be reproduced.
  • Using the same seed with the same label for two purposes that should be independent.
  • Reading the difference between two simulated results that used different seeds as a real difference, when it is within the Monte Carlo error of the summary.
  • Reporting extreme percentiles from a small number of replicates.

Review checklist

  • Can you say what a stream is keyed by, and why that makes results independent of the thread count?
  • Can you compute a Box–Muller normal and a radical inverse by hand?
  • Can you simulate one observation from the model, with and without parameter uncertainty?
  • Can you give the standard error of a simulated proportion and of a simulated percentile?

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