Tutorials › Marginal Structural Models › Monte Carlo Error and Simulation Size

The Parametric g-Formula · Tutorial 307 of 501

Monte Carlo Error and Simulation Size

Separate simulation noise from data uncertainty, calculate Monte Carlo standard errors, and choose a simulation size that meets a stated precision target.

Advanced 11 min read

What You'll Learn

  • Define the simulation target conditional on the observed data and fitted models
  • Derive the conditional Monte Carlo variance and standard error
  • Distinguish repeated-run simulation variation from sampling uncertainty
  • Choose a simulation size from a variance estimate or a precision target
  • Diagnose dependence and other problems that invalidate a simple standard-error calculation
  • Compare Monte Carlo integration with exact or deterministic integration

Why Simulation Size Matters

In the parametric g-formula, a fitted set of longitudinal models can be used to simulate counterfactual histories under a specified treatment regime. Averaging an outcome summary over those simulated histories approximates the corresponding model-based expectation. Because the simulated histories are random, the average changes if the random-number seed changes, even when the data and fitted models stay fixed.

That variation is Monte Carlo error. It is not the same as sampling uncertainty: sampling uncertainty arises because the observed study data, and hence the fitted models and target estimate, would change in another sample. This tutorial quantifies the first source of variation and explains how to make it small enough for the calculation at hand.

Let \(D\) denote the observed data and let the fitted models be held fixed conditional on \(D\). For a chosen regime \(g\), write \(Z_{im}(g)\) for the outcome summary from the \(m\)th simulated trajectory associated with baseline individual \(i\). The summary may be a scalar quantity of interest; the simulation may generate a full longitudinal history before that summary is calculated. Suppose there are \(n\) baseline individuals and \(M\) independent simulated trajectories per individual. The simulated estimate is

$$ \hat{\psi}_{g,M} = \frac{1}{n}\sum_{i=1}^{n}\frac{1}{M}\sum_{m=1}^{M} Z_{im}(g). $$

Define \(\mu_i(g)=E[Z_{im}(g)\mid D]\) and \(\sigma_i^2(g)=\operatorname{Var}(Z_{im}(g)\mid D)\). The corresponding infinite-simulation, model-based estimate for these baseline individuals is

$$ \hat{\psi}_{g,\infty} = \frac{1}{n}\sum_{i=1}^{n}\mu_i(g). $$

This is the quantity the simulation is approximating. It is still based on the observed data and fitted models; “infinite simulation” removes only the random error from approximating its expectation.

Conditional Monte Carlo Error

Definition: Conditional on the observed data and fitted models, the Monte Carlo error is \(\hat{\psi}_{g,M}-\hat{\psi}_{g,\infty}\). Its conditional standard deviation, \(\sqrt{\operatorname{Var}(\hat{\psi}_{g,M}\mid D)}\), is the Monte Carlo standard error (MCSE).

For a simple variance formula, assume that, conditional on \(D\), simulated trajectories are independent across \(m\) and \(i\); that each individual’s trajectories have the same conditional mean \(\mu_i(g)\) and variance \(\sigma_i^2(g)<\infty\); and that \(n\) and the baseline individuals are fixed during simulation. Independence here concerns whole simulated trajectories, not the time points within a trajectory. Outcomes at different times may be dependent within a trajectory.

Theorem: Under these conditions, \(\hat{\psi}_{g,M}\) is conditionally unbiased for \(\hat{\psi}_{g,\infty}\), and
$$ E[\hat{\psi}_{g,M}\mid D]=\hat{\psi}_{g,\infty}, \qquad \operatorname{Var}(\hat{\psi}_{g,M}\mid D) = \frac{1}{n^2M}\sum_{i=1}^{n}\sigma_i^2(g). $$

Proof. By linearity of conditional expectation,

$$ E[\hat{\psi}_{g,M}\mid D] = \frac{1}{nM}\sum_{i=1}^{n}\sum_{m=1}^{M}E[Z_{im}(g)\mid D] = \frac{1}{nM}\sum_{i=1}^{n}M\mu_i(g) = \hat{\psi}_{g,\infty}. $$

For the variance, conditional independence makes the covariance between distinct simulated trajectories zero. Therefore,

$$ \begin{aligned} \operatorname{Var}(\hat{\psi}_{g,M}\mid D) &= \frac{1}{n^2M^2}\sum_{i=1}^{n}\sum_{m=1}^{M} \operatorname{Var}(Z_{im}(g)\mid D)\\ &= \frac{1}{n^2M^2}\sum_{i=1}^{n}M\sigma_i^2(g) = \frac{1}{n^2M}\sum_{i=1}^{n}\sigma_i^2(g). \end{aligned} $$

Taking the square root gives the MCSE. The factor \(1/M\) in the variance means that multiplying the simulation size by four divides the MCSE by two, provided the simulation design and conditional variances stay the same. This is a precision trade-off, not a guarantee that an estimate is close to the causal quantity of interest: model misspecification and other sources of error are not removed by increasing \(M\). \(\square\)

Estimating the MCSE and Choosing \(M\)

The conditional variances \(\sigma_i^2(g)\) are generally unknown, but they can be estimated from the simulated values. Let

$$ S_i^2(g) = \frac{1}{M-1}\sum_{m=1}^{M} \left\{Z_{im}(g)-\bar Z_i(g)\right\}^2, \qquad \bar Z_i(g)=\frac{1}{M}\sum_{m=1}^{M}Z_{im}(g). $$

For \(M\geq 2\), the conditional sample variance \(S_i^2(g)\) is unbiased for \(\sigma_i^2(g)\) when the simulated values are independent and identically distributed conditional on \(D\), with finite variance. Substitution gives the usual estimated MCSE:

$$ \widehat{\operatorname{MCSE}}(\hat{\psi}_{g,M}) = \sqrt{\frac{1}{n^2M}\sum_{i=1}^{n}S_i^2(g)}. $$

To target an MCSE no larger than \(\delta\), use a pilot simulation to estimate the variance component and then choose \(M\) accordingly. If the same number of trajectories is used for every baseline individual, the plug-in calculation is

$$ M \geq \frac{\sum_{i=1}^{n}S_i^2(g)}{n^2\delta^2}. $$

The pilot estimate is uncertain, so this calculation is a planning guide rather than a certainty about the final MCSE. A practical workflow is to run a pilot, calculate the estimated MCSE, increase \(M\) if it exceeds the target, and verify the MCSE in the final run. If several regimes or outcome summaries are reported, choose a target and check the simulation error for each reported quantity, not just one convenient estimate.

Worked Example: MCSE With Different Baseline Risks

Suppose there are two fixed baseline individuals. Under a chosen regime, each simulated outcome is binary, with probability \(0.2\) of an outcome for individual 1 and probability \(0.6\) for individual 2. Simulate \(M=100\) independent trajectories per individual. Find the model-based estimate and the MCSE.

For a binary variable with success probability \(p\), the variance is \(p(1-p)\). Thus \(\mu_1=0.2\), \(\mu_2=0.6\), \(\sigma_1^2=0.2(0.8)=0.16\), and \(\sigma_2^2=0.6(0.4)=0.24\). The infinite-simulation estimate is

$$ \hat{\psi}_{g,\infty} = \frac{0.2+0.6}{2} = 0.4. $$

Using the conditional variance formula,

$$ \operatorname{Var}(\hat{\psi}_{g,100}\mid D) = \frac{0.16+0.24}{2^2(100)} = \frac{0.40}{400} = 0.001. $$

Therefore, the MCSE is \(\sqrt{0.001}\approx 0.0316\). This is the standard deviation of the simulation estimate around \(0.4\) over repeated simulations with these same two individuals and the same fitted models. It is not a standard error for uncertainty due to sampling the study participants.

Worked Example: Selecting a Simulation Size for a Target MCSE

For one fixed baseline individual, suppose the simulated outcome is binary with probability \(0.25\). How many independent trajectories are needed for an MCSE no greater than \(0.005\)?

The conditional variance of one simulated outcome is \(0.25(0.75)=0.1875\). With \(M\) independent trajectories, the variance of their mean is \(0.1875/M\). The target requires

$$ \sqrt{\frac{0.1875}{M}}\leq 0.005 \quad\Longleftrightarrow\quad M\geq \frac{0.1875}{0.005^2} =\frac{0.1875}{0.000025} =7500. $$

Thus \(M=7500\) meets the target under the stated model and known variance. In practice, the variance is not known and is estimated from a pilot. If all that is known is that the output lies between 0 and 1, its variance is at most \(1/4\). A conservative bound then gives \(M\geq (1/4)/(0.005^2)=10{,}000\). That bound can be more conservative than a pilot-based choice.

Repeated Runs and Data Uncertainty

Repeated simulation runs are a direct diagnostic for Monte Carlo variation. Keep the data, fitted models, baseline individuals, regime, and simulation size fixed; then repeat the simulation with independent random numbers. The spread of the resulting estimates reflects simulation variability under those fixed conditions. For \(R\geq2\) independent runs with estimates \(\hat{\psi}_{g,M}^{(1)},\ldots,\hat{\psi}_{g,M}^{(R)}\), calculate their sample standard deviation:

$$ S_{\mathrm{run}} = \sqrt{\frac{1}{R-1}\sum_{r=1}^{R} \left(\hat{\psi}_{g,M}^{(r)}-\bar{\psi}_{g,M}\right)^2}, \qquad \bar{\psi}_{g,M}=\frac{1}{R}\sum_{r=1}^{R}\hat{\psi}_{g,M}^{(r)}. $$

This estimates the MCSE for that simulation design, subject to the variability from having only \(R\) runs. A handful of runs can reveal an obvious instability, but their sample standard deviation may itself be imprecise. Repeating runs does not cause the underlying data or fitted models to vary, so it cannot estimate their sampling uncertainty.

Theorem: If \(\hat{\psi}_{g,M}\) has finite variance over repeated samples and simulation randomness, then
$$ \operatorname{Var}(\hat{\psi}_{g,M}) = \operatorname{Var}\!\left(E[\hat{\psi}_{g,M}\mid D]\right) + E\!\left[\operatorname{Var}(\hat{\psi}_{g,M}\mid D)\right]. $$
The first term is variation in the conditional mean across data sets; the second is average conditional simulation variance.

Proof. Add and subtract \(E[\hat{\psi}_{g,M}\mid D]\) inside the deviation from the overall mean. Equivalently, write the centered estimate as the sum of two terms:

$$ \hat{\psi}_{g,M}-E[\hat{\psi}_{g,M}] = \left\{\hat{\psi}_{g,M}-E[\hat{\psi}_{g,M}\mid D]\right\} + \left\{E[\hat{\psi}_{g,M}\mid D]-E[\hat{\psi}_{g,M}]\right\}. $$

On squaring and taking expectations, the cross-product is zero. Conditional on \(D\), the first bracket has conditional expectation zero, while the second is fixed. The expected square of the first bracket is \(E[\operatorname{Var}(\hat{\psi}_{g,M}\mid D)]\); the expected square of the second is \(\operatorname{Var}(E[\hat{\psi}_{g,M}\mid D])\). Their sum is the stated identity. \(\square\)

When the conditional simulation estimate is unbiased for \(\hat{\psi}_{g,\infty}\), its conditional mean is \(\hat{\psi}_{g,\infty}\). The first term in the decomposition is then variation of that model-based estimate across data sets; the second is the average Monte Carlo variance. The two terms answer different questions and should not be reported as if they were interchangeable.

Worked Example: Interpreting Variation Across Repeated Runs

For a fixed data set and fixed fitted models, five independent runs with the same simulation size give estimates \(0.491, 0.497, 0.500, 0.503,\) and \(0.509\). Calculate their mean and sample standard deviation, then interpret them.

The mean is

$$ \bar{\psi}_{g,M} = \frac{0.491+0.497+0.500+0.503+0.509}{5} = \frac{2.500}{5} = 0.500. $$

The deviations from the mean are \(-0.009,-0.003,0,0.003,0.009\). Their squared values sum to \(0.000081+0.000009+0+0.000009+0.000081=0.000180\). Therefore,

$$ S_{\mathrm{run}} = \sqrt{\frac{0.000180}{5-1}} = \sqrt{0.000045} \approx 0.00671. $$

The repeated-run MCSE estimate is about \(0.0067\). The run mean \(0.500\) is not a new estimate of sampling uncertainty, and the five runs do not represent five independent study samples. They only show how much this calculation varies when its simulation randomness changes.

Diagnostics, Alternatives, and Limitations

A useful diagnostic is to compare the reported MCSE with the scale of the estimate and with the precision needed for the analysis. Also check that the MCSE decreases as \(M\) increases: under independent simulation and finite variance, doubling \(M\) should reduce it by a factor of \(1/\sqrt{2}\), approximately. A result that changes substantially across independent runs, or an estimated MCSE that remains large relative to the desired precision, indicates that more simulation may be needed.

The simple formula assumes independent simulated trajectories. If simulations are correlated—for example, because random numbers are reused across runs or trajectories—the covariance terms do not vanish, and the formula can understate or overstate the actual MCSE. Estimate variability using independent complete runs, or use a variance calculation that accounts for the dependence created by the design. For a contrast between regimes simulated with shared random numbers, calculate uncertainty for the paired contrast itself; separate marginal MCSEs do not include its covariance.

Monte Carlo integration is useful when the required expectation is difficult to compute directly. If exact or deterministic integration is feasible, it avoids random simulation error, although it may require other computational approximations. Monte Carlo error is only one limitation: increasing \(M\) does not repair misspecified conditional models, unsupported extrapolation, or a mismatch between the target population and the baseline individuals being averaged.

A common mistake is to treat a small MCSE as evidence that the causal estimate is precise or unbiased. The reasoning is wrong because the MCSE measures only variation from the simulated draws conditional on the fitted analysis. The correction is to assess simulation error separately from data and model uncertainty, and to report what the MCSE actually quantifies.

Check Your Understanding

Answer each question, then check the solutions below.

  1. If the simulation size increases from \(M\) to \(9M\), by what factor does the MCSE change under the independent-trajectory conditions?
  2. For a binary simulated outcome with probability \(0.5\), what simulation size gives an MCSE of at most \(0.01\) for one fixed baseline individual?
  3. Why can repeated simulations with the same fitted models not estimate sampling uncertainty?
  4. For \(n\) fixed baseline individuals and \(M\) independent trajectories per individual, state the conditional variance of the averaged estimate.
  5. Name one condition under which the usual conditional MCSE formula may fail.

Solutions. (1) The variance is proportional to \(1/M\), so the MCSE is proportional to \(1/\sqrt{M}\). Increasing \(M\) ninefold divides the MCSE by \(\sqrt{9}=3\). (2) The variance of one binary draw is \(0.5(0.5)=0.25\), so \(0.25/M\leq 0.01^2=0.0001\), requiring \(M\geq 2500\). (3) The data and fitted models are held fixed across those runs; only simulation randomness changes. (4) The variance is \(\sum_{i=1}^{n}\sigma_i^2(g)/(n^2M)\), assuming conditional independence across simulated trajectories. (5) For example, trajectories may be dependent because of the simulation design, making covariance terms nonzero.

Clinical Biostats