Tutorials › Marginal Structural Models › Analytic Integration Versus Monte Carlo Integration

The Parametric g-Formula · Tutorial 320 of 501

Analytic Integration Versus Monte Carlo Integration

Learn to choose and assess analytic, deterministic numerical, and Monte Carlo integration when evaluating a g-formula estimand.

Advanced 12 min read

What You'll Learn

  • Express a regime-specific g-formula estimand as an integral over baseline and longitudinal covariates.
  • Recognize when finite sums or closed-form expectations make integration exact.
  • Distinguish deterministic quadrature error from Monte Carlo sampling error.
  • Derive the consistency and sampling variance of a Monte Carlo g-formula estimator.
  • Use conditional averaging to reduce simulation variance.
  • Diagnose computational limits without confusing integration error with model or identification error.

Why Integration Strategy Matters

A g-formula estimand averages a modeled counterfactual outcome over the covariate histories that would arise under a specified intervention. The target is an expectation; the practical question is how to evaluate it. For a small discrete history space, summing every possible history may be exact and inexpensive. For continuous covariates, a closed form may or may not exist. Deterministic quadrature can approximate a low-dimensional integral accurately, while Monte Carlo integration can handle large, complicated history spaces by sampling trajectories.

These are computational alternatives for evaluating the same estimand, not alternative identification strategies. Each relies on the same target population, intervention, and fitted conditional models. The usual identification assumptions, including consistency, sequential exchangeability, and positivity for the intervention of interest, remain relevant; changing the integration method does not repair their failure or correct misspecified models.

Definition: Let \(L_0\) denote baseline covariates, \(L_t\) time-varying covariates, and \(Y\) the outcome. Under a regime \(d\), the g-formula estimand is the expectation of the modeled counterfactual outcome over the baseline and covariate-history distributions induced by \(d\). Analytic integration evaluates that expectation in closed form or by exact summation; deterministic numerical integration approximates it using fixed evaluation points and weights; Monte Carlo integration approximates it using random draws from the relevant distributions.

For a longitudinal history, the expectation can be written schematically as an integral of the conditional mean outcome against the baseline distribution and each covariate transition distribution under \(d\). In the discrete case, integrals are sums. In either case the fitted models determine the integrand and the distributions being averaged over.

$$ \psi(d) = \int \cdots \int E[Y\mid L_0=l_0,\bar L_T=\bar l_T,\bar A_T=\bar d_T] \prod_{t=1}^{T} f(l_t\mid l_0,\bar l_{t-1},\bar a_{t-1}= \bar d_{t-1}) \, f(l_0)\, d\bar l_T\,dl_0 . $$

The expression is schematic: the conditional distributions and outcome regression must match the study’s time ordering and covariate history. It emphasizes the computational distinction. Exact finite summation enumerates the support; quadrature chooses fixed points and weights to approximate the integral; Monte Carlo generates histories according to the distributions in the formula and averages their predicted outcomes.

Exact Summation and Analytic Integration

Exact evaluation is attractive when the relevant support is finite and small, or when the required expectations have tractable forms. A product of binary covariates over many time points can, however, create exponentially many histories. A closed form may also be unavailable even when the models themselves are simple. Thus, “analytic” does not automatically mean computationally feasible: the number of terms and the complexity of each term matter.

Worked Example: Enumerating a Two-Stage Discrete History

Suppose baseline \(B\) is Bernoulli with \(P(B=1)=0.4\). Under a fixed regime \(d\), an intermediate covariate \(L_1\) is Bernoulli with \(P(L_1=1\mid B=0)=0.2\) and \(P(L_1=1\mid B=1)=0.7\). The fitted outcome risks under the regime are \(0.05,0.20,0.30,0.60\) for \((B,L_1)=(0,0),(0,1),(1,0),(1,1)\), respectively. Find the regime-specific risk by exact summation.

For \(B=0\), average over the two possible values of \(L_1\):

$$ E[Y^d\mid B=0] =(1-0.2)(0.05)+0.2(0.20) =0.8(0.05)+0.2(0.20) =0.04+0.04=0.08. $$

For \(B=1\), the corresponding average is:

$$ E[Y^d\mid B=1] =(1-0.7)(0.30)+0.7(0.60) =0.3(0.30)+0.7(0.60) =0.09+0.42=0.51. $$

Finally average over the baseline distribution:

$$ \psi(d) =(1-0.4)(0.08)+0.4(0.51) =0.6(0.08)+0.4(0.51) =0.048+0.204=0.252. $$

There are only four covariate histories, so exact enumeration is straightforward and has no integration approximation error. With many binary covariates across many times, the same calculation can require an impractical number of terms.

Monte Carlo Integration: What It Estimates

Monte Carlo integration draws \(N\) independent histories from the baseline and intervention-specific transition distributions, calculates the modeled outcome for each history, and averages those predictions. If the conditional outcome mean is used, write the draw-specific prediction as \(m(H_i)\), where \(H_i\) is the simulated history. The estimate is the sample mean of these predictions.

$$ \hat\psi_N(d)=\frac{1}{N}\sum_{i=1}^{N}m(H_i), \qquad H_i\sim F_d . $$

Here \(F_d\) is the distribution of covariate histories under the intervention. For a fixed fitted model and independent draws from \(F_d\), the estimate is unbiased when \(E_{F_d}[|m(H)|]<\infty\). If \(E_{F_d}[m(H)^2]<\infty\), its variance is \(\operatorname{Var}_{F_d}(m(H))/N\), and the strong law of large numbers gives \(\hat\psi_N(d)\xrightarrow{a.s.}\psi(d)\). If the variance is positive and finite, the central limit theorem further gives \(\sqrt{N}\{\hat\psi_N(d)-\psi(d)\}\xrightarrow{d}N(0,\operatorname{Var}_{F_d}(m(H)))\). These statements concern Monte Carlo integration conditional on the fitted models; they do not include uncertainty from estimating model parameters.

Theorem: If \(H_1,\ldots,H_N\) are independent and identically distributed from \(F_d\), \(E_{F_d}[|m(H)|]<\infty\), and \(E_{F_d}[m(H)^2]<\infty\), then \(\hat\psi_N(d)\) is unbiased for \(\psi(d)=E_{F_d}[m(H)]\), has variance \(\operatorname{Var}_{F_d}(m(H))/N\), and converges almost surely to \(\psi(d)\). If \(0<\operatorname{Var}_{F_d}(m(H))<\infty\), its centered, scaled limit is normal as stated above.

Justification. Linearity of expectation gives \(E[\hat\psi_N(d)]=N^{-1}\sum_i E[m(H_i)]=\psi(d)\). Independence makes all cross-covariances zero, so \(\operatorname{Var}(\hat\psi_N(d))=N^{-2}\sum_i\operatorname{Var}(m(H_i))=\operatorname{Var}(m(H))/N\). The strong law applies to the independent, identically distributed integrable variables \(m(H_i)\). Under the additional finite positive variance condition, the independent, identically distributed central limit theorem applies to those same variables. \(\square\)

Worked Example: Conditional Averaging Reduces Simulation Variance

Let \(X\sim N(0,1)\), and suppose the modeled conditional mean under a regime is \(m(X)=0.3+0.4X+0.2X^2\). The estimand is \(E[m(X)]\). Since \(E[X]=0\) and \(E[X^2]=1\),

$$ \psi=0.3+0.4(0)+0.2(1)=0.5. $$

If the outcome is simulated as well, suppose \(Y\mid X\) has this conditional mean and conditional variance \(1\). The total variance formula gives

$$ \operatorname{Var}(Y) =E[\operatorname{Var}(Y\mid X)] +\operatorname{Var}(E[Y\mid X]) =1+\operatorname{Var}(m(X)). $$

For a standard normal variable, \(\operatorname{Var}(X)=1\), \(\operatorname{Var}(X^2)=2\), and \(\operatorname{Cov}(X,X^2)=E[X^3]-E[X]E[X^2]=0\). Therefore,

$$ \operatorname{Var}(m(X)) =(0.4)^2(1)+(0.2)^2(2)+2(0.4)(0.2)(0) =0.16+0.08=0.24. $$

A simulation that draws both \(X\) and \(Y\) has per-draw variance \(1.24\); one that draws \(X\) and averages \(m(X)\) has per-draw variance \(0.24\). With \(N=100\), the respective estimator variances are \(1.24/100=0.0124\) and \(0.24/100=0.0024\). Both target the same mean, but averaging the conditional mean avoids unnecessary outcome noise.

Deterministic Numerical Integration

Numerical quadrature evaluates the integrand at selected points and combines the results with fixed weights. In low-dimensional continuous problems, a well-chosen quadrature rule can be accurate with relatively few evaluations. Unlike Monte Carlo, the result is generally deterministic once the grid, rule, and fitted model are fixed. Its error is approximation error from the quadrature rule and, when an unbounded integral is truncated, from the omitted tails. Increasing the grid resolution does not guarantee a simple universal error rate; accuracy depends on the integrand’s smoothness, dimension, domain, and the rule used.

Worked Example: Midpoint Integration Versus Monte Carlo

Let \(X\) be Uniform on \([0,1]\), and let the modeled outcome risk be \(m(X)=X^2\). The target is

$$ \psi=\int_0^1 x^2\,dx=\frac{1}{3}\approx 0.3333. $$

Using the midpoint rule with two equal subintervals gives midpoints \(0.25\) and \(0.75\), each with weight \(0.5\):

$$ \hat\psi_{\mathrm{mid}} =\frac{1}{2}(0.25^2+0.75^2) =\frac{1}{2}(0.0625+0.5625) =0.3125. $$

The deterministic error is \(0.3125-\frac{1}{3}=-\frac{1}{48}\), approximately \(-0.0208\). For Monte Carlo, draw independent \(X_i\sim\operatorname{Uniform}(0,1)\) and average \(X_i^2\). This estimator is unbiased, with variance per draw

$$ \operatorname{Var}(X^2) =E[X^4]-E[X^2]^2 =\frac{1}{5}-\left(\frac{1}{3}\right)^2 =\frac{4}{45}. $$

Thus the variance of the Monte Carlo estimate at \(N=100\) is \(4/(45\cdot100)\approx0.000889\), with standard error about \(0.0298\). The particular Monte Carlo estimate varies across runs; the midpoint result does not. This comparison does not establish which method is more accurate for a particular run: it shows that quadrature has a fixed approximation error here, whereas Monte Carlo has random error quantified by its variance.

Choosing a Method and Diagnosing Failure

The practical choice depends on the dimension and structure of the integral. Exact summation is compelling for small discrete supports. Closed-form expectations are useful when the distribution and integrand permit them. Deterministic quadrature is often effective in low dimensions, especially when the integrand is smooth. Monte Carlo is often more scalable as the number of continuous covariates or longitudinal time points grows, although its sampling error usually declines only at the rate \(N^{-1/2}\). Hybrid calculations can integrate some components exactly and simulate the rest.

A useful hybrid principle is conditional averaging: whenever part of the integrand can be averaged analytically conditional on the remaining random quantities, use that conditional expectation before simulation. The preceding example illustrated the variance benefit of replacing simulated outcomes by their conditional mean. More generally, if \(Z\) is a simulated quantity and \(W\) is retained, then

$$ \operatorname{Var}(E[Z\mid W]) \leq \operatorname{Var}(Z), $$

because the total variance identity gives \(\operatorname{Var}(Z)=E[\operatorname{Var}(Z\mid W)]+\operatorname{Var}(E[Z\mid W])\), and the first term is nonnegative.

Diagnostics should address the chosen approximation directly. For Monte Carlo, compare independent runs or batches, monitor the estimated standard error as \(N\) increases, and check that simulated histories follow the specified baseline and transition distributions. For quadrature, compare successive grid resolutions or rules, and inspect tail truncation when relevant. For exact summation, verify that probabilities over all enumerated histories sum to one. In every case, compare a smaller problem against a known exact result when possible.

A common mistake is to treat a very large simulation as a remedy for every discrepancy. Increasing \(N\) reduces Monte Carlo error, but it does not fix an incorrect transition model, a coding error, or lack of positivity; neither does a fine quadrature grid. Conversely, disagreement between two numerical integration methods can signal inadequate resolution or a simulation problem, but does not by itself identify which method is wrong. Keep integration error, model-estimation uncertainty, and bias from identification or model assumptions conceptually separate. Bootstrap inference addresses sampling uncertainty in the fitted analysis; it does not make a poor numerical approximation exact.

Key takeaway: Choose the integration method based on the mathematical structure and computational scale of the estimand. Check its approximation error separately from uncertainty in estimated models and from the assumptions that identify the causal estimand.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. A regime-specific covariate history has only eight possible values. What integration approach is a natural first choice, and what should be checked? Solution: Exact enumeration is natural if all eight probabilities and modeled outcomes can be evaluated. Check that the history probabilities sum to one and that each possible history is included.
  2. If the variance of the simulated conditional predictions is \(0.36\), what is the Monte Carlo standard error for \(N=400\) independent histories? Solution: The variance of the average is \(0.36/400=0.0009\), so the standard error is \(\sqrt{0.0009}=0.03\).
  3. Does increasing the Monte Carlo sample size reduce bias from a misspecified outcome model? Solution: No. It reduces random integration error around the expectation implied by the fitted model, not model misspecification bias.
  4. Why can averaging conditional outcome means be more efficient than simulating outcomes too? Solution: Conditional averaging removes the nonnegative \(E[\operatorname{Var}(Y\mid H)]\) component of variance, since \(\operatorname{Var}(Y)=E[\operatorname{Var}(Y\mid H)]+\operatorname{Var}(E[Y\mid H])\).
Clinical Biostats