Tutorials › Optimal Design and Model-Informed Dosing › Benchmarking a Simulator Against Known Results

Simulation Studies of Designs · Tutorial 463 of 497

Benchmarking a Simulator Against Known Results

Learn to benchmark simulation code against results known in advance, and distinguish implementation errors from ordinary Monte Carlo variation.

Intermediate 11 min read

What You'll Learn

  • Separate deterministic model checks from checks of stochastic simulation output.
  • Use a one-compartment PK limiting case to verify calculated concentrations.
  • Compare simulated event frequencies with a known binomial probability.
  • Benchmark estimator bias, variance, and interval coverage against analytic results.
  • Use Monte Carlo standard errors to assess discrepancies without relying on arbitrary tolerances.
  • Recognize what analytic benchmarks can and cannot establish about a simulator.

Why Benchmark Before Comparing Designs?

A simulator can produce smooth, plausible-looking summaries while implementing the wrong model, parameterization, or random mechanism. That is especially consequential in design comparisons: a programming error shared unevenly across designs can alter their apparent ranking, while an error shared by every design can make all the results misleading. Before using simulation output to compare sampling plans, check the implementation in cases where the correct answer is already known.

Benchmarking is a comparison between an output from the simulator and an independently established reference result. The reference may be an exact calculation, a theoretical distribution, or a limiting case in which random variation disappears. A benchmark is strongest when its answer is derived independently of the code being checked; comparing two routines that use the same mistaken formula may simply reproduce the same error.

Definition: A simulation benchmark is a test case with specified inputs and a known or analytically derivable target output, used to assess whether a simulator reproduces the intended model or statistical behavior. For a stochastic benchmark, agreement is judged relative to Monte Carlo uncertainty as well as any justified numerical tolerance.

A useful sequence is to check deterministic calculations first, then random generation against known distributions, and finally end-to-end operating characteristics such as estimator bias or interval coverage. State the assumptions behind each reference before interpreting the comparison. A disagreement may indicate a coding defect, an incorrect assumption in the benchmark, or ordinary simulation variability; these possibilities should not be conflated.

Start With a Limiting Case

A limiting case removes one or more sources of complexity while preserving a result that the implementation should still satisfy. For example, set random-effects variances and residual-error variance to zero in a model that supports those components. Subject-level quantities then reduce to their fixed-effect predictions. This does not validate every stochastic feature, but it isolates the deterministic model calculation and can expose errors in units, parameterization, or time dependence.

Worked Example: A Deterministic One-Compartment Concentration

Consider a hypothetical simulator for a one-compartment model after an intravenous bolus. Assume linear elimination, a dose of 100 mg, volume \(V=20\) L, clearance \(CL=4\) L/h, and no interindividual variability or residual error. Under these assumptions, concentration at time \(t\) is

$$ C(t)=\frac{\mathrm{Dose}}{V}\exp\left(-\frac{CL}{V}t\right). $$

At \(t=5\) h, the initial concentration is \(100\text{ mg}/20\text{ L}=5\text{ mg/L}\), and the elimination rate constant is \(CL/V=4/20=0.2\text{ h}^{-1}\). Therefore,

$$ C(5)=5\exp(-0.2\times 5) =5\exp(-1) =1.839\text{ mg/L}. $$

With fixed parameters and no random errors, repeated simulator runs should return this same concentration, apart from small floating-point differences. A result near \(1.839\) but expressed in the wrong units, or a value that changes across runs, signals a problem to investigate. This check relies on the stated one-compartment, bolus, linear-elimination model; it does not validate absorption, random effects, residual-error generation, or parameter estimation.

For a deterministic benchmark, choose a numerical tolerance based on the calculation and the implementation’s numerical precision. Do not use a broad tolerance simply because the output looks close. Conversely, do not demand exact bit-for-bit equality when two correct calculations can differ slightly because of floating-point arithmetic.

Use Analytic Distributions to Check Random Output

A stochastic output will generally not equal its theoretical expectation in a finite run. The appropriate question is whether the discrepancy is plausible under the known sampling distribution. Suppose \(R\) independent simulation replicates produce an indicator \(I_r\), equal to 1 when a specified event occurs and 0 otherwise. If the event probability is \(p\), then the count \(K=\sum_{r=1}^{R} I_r\) has a binomial distribution with parameters \(R\) and \(p\). The simulated event fraction \(\hat{p}=K/R\) has expectation \(p\) and standard error \(\sqrt{p(1-p)/R}\).

Worked Example: Checking a Bernoulli Event Generator

Suppose a simulator is configured to generate an event with probability \(p=0.20\) in each of \(R=10{,}000\) independent replicates. The expected count is \(Rp=10{,}000(0.20)=2{,}000\). Assume the hypothetical run records 2,027 events, so

$$ \hat{p}=\frac{2{,}027}{10{,}000}=0.2027. $$

Under the stated binomial model, the standard error of the event fraction, evaluated at the known probability, is

$$ SE(\hat{p})= \sqrt{\frac{p(1-p)}{R}} =\sqrt{\frac{0.20(0.80)}{10{,}000}} =0.004000. $$

The discrepancy standardized by this Monte Carlo standard error is \((0.2027-0.20)/0.004000=0.675\). A normal-approximation 95% reference range is \(0.20\pm1.96(0.004000)\), or approximately 0.1922 to 0.2078. The observed fraction lies inside that range and is not unusual evidence of a problem. This is not proof that the generator is correct: it is one check of one event probability. The calculation assumes independent replicates and the same event probability in every replicate.

A standardized discrepancy is a diagnostic, not a pass/fail rule that makes all other checks unnecessary. Values near zero are compatible with the reference, while a large absolute value warrants investigation. If many probabilities, outputs, or scenarios are checked, some will look discrepant by chance; interpret the full set of checks rather than treating one isolated threshold crossing as conclusive.

Benchmark the Statistical Procedure, Not Just the Model Output

A simulator used to evaluate designs often generates data and then applies an estimator or interval procedure. In that setting, benchmark the complete procedure where an analytic result is available. Bias, variance, and coverage each assess different behavior: an estimator can have the correct average but the wrong variability, and intervals can have the wrong coverage even when point estimates appear reasonable.

For a simple reference, let each replicate contain \(n\) independent observations \(X_i\sim N(\mu,\sigma^2)\), with known \(\mu\) and \(\sigma\). The sample mean \(\bar{x}\) has expectation \(\mu\) and variance \(\sigma^2/n\). Across \(R\) independent simulated data sets, the average of the \(R\) sample means has variance \(\sigma^2/(nR)\). This follows because variances of independent means add:

$$ \operatorname{Var}\left(\frac{1}{R}\sum_{r=1}^{R}\bar{X}_r\right) =\frac{1}{R^2}\sum_{r=1}^{R}\operatorname{Var}(\bar{X}_r) =\frac{1}{R^2}R\frac{\sigma^2}{n} =\frac{\sigma^2}{nR}. $$

Thus the Monte Carlo standard error of the average simulated estimate is \(\sigma/\sqrt{nR}\), under these assumptions. This quantifies how much the reported benchmark summary itself is expected to fluctuate across repeated simulation runs. It is distinct from the sampling standard deviation of one replicate’s estimate, \(\sigma/\sqrt{n}\).

Worked Example: Checking Bias, Variance, and Coverage

Consider a hypothetical benchmark with \(R=10{,}000\) independent data sets. In each data set, \(n=25\) observations are generated from \(N(10,2^2)\). The simulator reports the sample mean and a 95% interval using the known standard deviation, \(\bar{x}\pm1.96(2/\sqrt{25})\). The model assumptions are independent normal observations with the stated mean and variance, and the interval uses the correct known \(\sigma=2\).

The expected sample mean is 10, and its standard deviation across individual data sets is \(2/\sqrt{25}=0.4\). The hypothetical mean of the 10,000 reported estimates is 10.003. Its Monte Carlo standard error relative to the known expectation is

$$ SE\left(\frac{1}{R}\sum_{r=1}^{R}\bar{X}_r\right) =\frac{2}{\sqrt{25(10{,}000)}}=0.004000. $$

The observed difference, 0.003, is \(0.003/0.004000=0.750\) Monte Carlo standard errors from the target. It is consistent with the known expectation and does not indicate noticeable bias in this run.

The theoretical variance of one sample mean is \(2^2/25=0.1600\). Suppose the hypothetical empirical variance across simulated means is 0.1612. For normal observations, the variance of the sample variance across \(R\) independent normal draws is \(2v^2/(R-1)\), where \(v\) is the true variance. Its standard error is therefore approximately

$$ SE(s^2)\approx v\sqrt{\frac{2}{R-1}} =0.1600\sqrt{\frac{2}{9{,}999}} =0.002263. $$

The variance discrepancy is \(0.0012/0.002263=0.530\) standard errors, also compatible with the reference. This variance check uses the normal-theory result and independent replicates; it would not have this standard error under an arbitrary distribution.

The interval half-width is \(1.96(2/5)=0.7840\). Under the stated normal model, its coverage probability is \(P(|Z|\leq1.96)\), approximately 0.9500 for \(Z\sim N(0,1)\). If 9,490 of the 10,000 intervals contain the true mean, the simulated coverage is 0.9490. Its Monte Carlo standard error under the theoretical coverage is \(\sqrt{0.95(0.05)/10{,}000}=0.002179\). The discrepancy is \((0.9490-0.9500)/0.002179=-0.459\) standard errors. The observed coverage is consistent with the reference, but this check does not establish correct behavior for nonnormal data, unknown variance, or other interval methods.

Choosing and Interpreting Benchmarks

A good benchmark matches the layer of the simulator that needs scrutiny. An exact model calculation checks deterministic equations. A known probability distribution checks random generation and aggregation. A tractable estimation problem checks the combined data-generation and analysis steps. When no exact result is available for the full model, reduce complexity until a useful reference is available, or compare a component with an independently implemented reference calculation. Agreement between independent implementations is informative but weaker than agreement with a mathematical result, because both implementations can share the same misunderstanding.

For stochastic comparisons, calculate the relevant Monte Carlo standard error from the benchmark assumptions, then report the simulated result, theoretical target, and discrepancy in context. A large standardized discrepancy is a reason to investigate, not an automatic diagnosis of a coding defect. Check inputs, units, parameter transformations, replicate independence, and whether the analytic assumptions actually match the simulation. If the theoretical target was estimated rather than known exactly, its uncertainty also matters; treating an uncertain reference as exact can overstate disagreement.

Increasing \(R\) reduces Monte Carlo standard error at a rate proportional to \(1/\sqrt{R}\), but it does not repair a biased implementation. A result that is consistently wrong becomes more precisely wrong as the number of replicates grows. Conversely, a modest discrepancy in a small run can be ordinary sampling variation. Report enough information for another analyst to distinguish those cases: the target, observed summary, replicate count, assumptions, and uncertainty calculation.

Key takeaway: Validate from simple to complex. First match deterministic limiting cases, then check random outputs against known distributions, and then assess end-to-end statistical behavior where theory provides a target. Judge stochastic differences against Monte Carlo uncertainty, and do not treat agreement in one benchmark as proof of universal correctness.

Common Pitfalls and Limitations

  • Using a visually plausible result as validation. Smooth plots and reasonable magnitudes do not establish that the intended model was implemented. Compare with a stated reference and quantify the discrepancy.
  • Confusing Monte Carlo error with model-sampling variability. The standard deviation among replicate estimates describes variation of estimates across data sets; the Monte Carlo standard error describes uncertainty in a summary calculated from a finite number of those replicates.
  • Demanding exact equality from random output. A finite simulation is not expected to equal its theoretical expectation. Use the reference distribution or Monte Carlo standard error to assess plausible variation.
  • Assuming one successful benchmark validates every feature. A fixed-parameter concentration check does not test random-effects distributions, residual errors, adaptive decisions, or estimation algorithms that were not exercised.
  • Choosing a tolerance after seeing the result. Define meaningful numerical tolerances and statistical comparisons from the model and calculation, not to make a surprising output appear acceptable.

Benchmarks are deliberately targeted, so their conclusions are local: they support confidence in the tested inputs, equations, and procedures, not every possible model configuration. They also do not replace checks of the implementation’s broader behavior. Their value is that they create clear reference points before computational results are used to support design decisions.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. A deterministic model calculation returns 3.5 units on every run, but the analytical benchmark is 3.0 units. Is increasing the number of runs an appropriate way to resolve the discrepancy? Why?
  2. A simulator generates a binary outcome with known probability \(p=0.10\) in \(R=1{,}000\) independent replicates. What is the expected event count and the standard error of the simulated event fraction?
  3. For \(n=16\) observations per replicate from \(N(\mu,3^2)\), what is the theoretical variance of the sample mean in one replicate? If there are \(R=100\) independent replicates, what is the Monte Carlo standard error of their average sample mean?
  4. Why does agreement between a simulator and a second program not necessarily establish that either implements the intended model?

Solutions. (1) No. The calculation is deterministic, so more runs will reproduce the same discrepancy. Check the formula, inputs, units, and implementation against the analytical result. (2) The expected count is \(Rp=1{,}000(0.10)=100\). The standard error of the event fraction is \(\sqrt{0.10(0.90)/1{,}000}=0.009487\). (3) The variance of one sample mean is \(3^2/16=0.5625\). The Monte Carlo standard error of the average across 100 independent means is \(3/\sqrt{16(100)}=0.07500\). (4) The programs may share the same incorrect equation, parameter convention, or assumption. An analytic reference or a genuinely independent derivation provides stronger evidence.

Clinical Biostats