Documentation

4.3 Determinism and the seed

An analysis that gives a different answer each time it is run cannot be reviewed, and one that depends on how many cores the computer has cannot be repeated on another. nlmx is built so that neither happens. This section says what is promised, how it is done, what you can check yourself in a minute, and what is not promised.

4.3.1 The promise

For the same version of the program, the same inputs, the same settings and the same seed, a command gives the same results, and the results do not depend on --threads.

The first part makes a rerun comparable. The second makes the number of threads a choice about speed only: you may run on a laptop with 2 cores and on a server with 64 and compare the files.

4.3.2 How it is done: named streams

Many commands use random numbers: sims and vpc simulate, power simulates and refits, cov draws parameters and forest draws from the uncertainty of the estimates. If these commands simply took numbers from one generator in the order the work happens to be done, the order would depend on how the work was divided between threads.

nlmx avoids this by never sharing a generator. Every piece of work that needs random numbers gets its own stream, derived from the seed and from a label that names the work: a simulated subject in a replicate, a draw of the parameters, a resampling. The derivation is a fixed function (the generator is SplitMix64), the label is built from what is being simulated and not from where it runs, and so the subject 17 of replicate 3 sees the same numbers whether it is computed first or last, on the first thread or on the fifth. A quasi-random option (--draw-method in the covariate engine) also keys its shifts by label.

Two consequences are worth knowing.

  • Changing the seed changes everything that is random, and nothing else. --seed N (or seed in the settings) is the master seed. The default differs between commands, because each command's default was chosen for its own stream; the console and the settings echo state it. In the tour, sims printed seed 20260101, the VPC settings carry 20240611, and the power command printed seed 12345 because Section 3.6 gave it.
  • A stream does not depend on its neighbours. Because the stream of a piece of work is fixed by the seed and its label, the order in which pieces are computed cannot matter, which is why the thread count cannot.

The derivation also carries a version number. A checkpoint written by a different derivation cannot be resumed (exit code 11) because its streams would differ from those the new run would use.

4.3.3 Check it yourself

The sims command is fast enough to test both parts of the promise in a few seconds. Run it twice with different thread counts, and once with another seed:

nlmx sims models/run101.ctl --threads 1 --no-plots -o s_t1
nlmx sims models/run101.ctl --threads 2 --no-plots -o s_t2
nlmx sims models/run101.ctl --seed 7     --no-plots -o s_seed7
diff s_t1/checksums.sha256 s_t2/checksums.sha256

The checksum files differ in three lines only: run101_settings.sims, sims_report.html and run101_summary.json. The difference in the JSON is, in its last lines:

<   "seconds": 0.084645984,         >   "seconds": 0.075683926,
<     "threads": 1                  >     "threads": 2

The settings echo, the report and the summary record the elapsed time and the number of threads they were run on, and that is where they differ. The four data files (run101_data.csv, run101_sim.csv, run101_subjects.csv, run101_metrics.csv) and the summary text have identical hashes for 1 and 2 threads. With --seed 7, every data file changes, and so do the results: the mean peak concentration moves from 2.506 to 2.583 mg/L, and the mean AUC over the interval from 34.65 to 36.30 mg·h/L. The difference between 2.506 and 2.583 is the Monte Carlo noise of a simulation of 100 subjects; it is the size of the effect of a seed, and the reason a simulation study states its seed with its results.

4.3.4 What can still change a result

The promise has conditions, and they are the ones to keep in your quality file.

  • The version. A new version may change an algorithm, a default or the order in which random numbers are drawn. The release notes (CHANGELOG, Section 2.5) say when a result is affected. Record the version with every analysis (it is in the log header and in input_manifest.json).
  • The platform. Floating-point arithmetic is deterministic on one platform with one build, but a different processor architecture, operating system or compiler can round the last digits differently, and an iterative estimation may then stop one iteration earlier or later. The differences are in the last printed digits, not in the conclusions. The tests of the project run on three operating systems (Section 2.2); the recordings of this book are from one.
  • The inputs. A dataset with one more row, a settings file with one more key, a control stream whose initial estimates differ: all of these are different inputs, and the input manifest and the settings echo exist to show which was used.
  • Other programs. NONMEM, with its own random streams and algorithms, will not reproduce an nlmx estimation to the last digit; Section 1.4 and Chapter 40 discuss how close they are expected to be.
  • Time. Nothing in a result depends on the date or on the clock, except the date written in the header of a log.

4.3.5 Using the seed well

Fix the seed in the settings file when you plan to report a result, so that it is in the echo. Do not hunt for a seed that gives the answer you hoped for: when a conclusion depends on the seed, the number of replicates is too small, and the honest fix is more replicates. The SSE of Section 3.6 is the example: with 20 replicates the power of 0.90 had a confidence interval from 0.70 to 0.97, and no seed changes that.

Common mistakes

  • Comparing two runs made with different versions and calling the difference "random". It is a version.
  • Expecting identical files from a repeated fit in a folder you changed. The inputs differ.
  • Reading the timing in a report as part of the result. Times and thread counts are recorded, and they differ by design.

Checklist

  • The seed is stated in the settings of any simulation you will report.
  • The version is recorded with the analysis.
  • A rerun of a key result on the same version produces the same checksums, apart from timing.

Further reading

Chapter 9 (the numerical core and reproducibility), Chapter 30 (replicates and Monte Carlo error), Section 2.5 (versions).