Week 1: Foundations and Monte Carlo Integration

STAT 536 — Statistical Computing

Soumojit Das

Fall 2026

Why Statistical Computing

The Purpose of Statistical Computing

Core Objectives. We develop computational methods to solve two fundamental problems:

  1. Integration: Evaluate integrals numerically
  2. Optimization: Find maxima/minima of complex functions

For optimization, take \(h(x) = (\cos 50x + \sin^2 30x)^2\) on \([0,2]\). We want

\[\max_{0 \leq x \leq 2} h(x) \qquad \text{and} \qquad \argmax_{0 \leq x \leq 2} h(x).\]

Integration: Simple, Complex, Intractable

Analytical

\[\int_0^1 x \, dx = \frac{1}{2}\]

Complex

\[\int_0^7 (\tanh 17x + \cos^2 42x)^3 \, dx\]

Intractable

\[\int_0^1 e^{\cos(e^x)} \, dx\]

Three Integrands, Three Levels of Difficulty

Randomness for a Deterministic Problem

The objectives are deterministic. We can induce randomness to achieve them.

A Brief History of Monte Carlo Methods

Origins.

  • Named after Monaco’s Monte Carlo Casino
  • Developed during Manhattan Project (1940s)
  • Pioneers: Stanislaw Ulam, John von Neumann, Nicholas Metropolis

The Ulam Story. While recovering from illness, Ulam played solitaire and wondered about the probability of winning. He realized simulation was easier than combinatorial analysis.

First Applications.

  • Neutron diffusion
  • Particle physics
  • Now: finance, ML, statistics

Statistical Applications

Maximum Likelihood Estimation.

Given data \(X_1, \ldots, X_n \iid f(x|\theta)\):

\[\hat{\theta} = \argmax_\theta \sum_{i=1}^n \log f(x_i|\theta)\]

Often no closed form.

Bayesian Inference.

Posterior distribution:

\[\pi(\theta|X) = \frac{f(X|\theta)\pi(\theta)}{\int f(X|\theta)\pi(\theta)d\theta}\]

Normalizing constant intractable.

The Posterior Mean Is an Integral

Bayesian Computation. Want:

\[\hat{\theta} = \E[\theta|X] = \int \theta \, \pi(\theta|X) \, d\theta\]

Even if we know \(\pi(\theta|X)\), this integral may be impossible.

A Monte Carlo Estimate of an Intractable Integral

Implementation Considerations

Critical Numerical Issues.

Problems to Avoid:

  • Overflow: \(e^{1000} = \infty\)
  • Underflow: \(e^{-1000} = 0\)
  • Catastrophic cancellation
  • Loss of precision

Solutions:

  • Work on log scale
  • Avoid direct inversions
  • Use stable algorithms
  • Careful with subtraction

Example: Computing Probabilities. Instead of \(\prod_{i=1}^n p_i\), compute \(\exp\left(\sum_{i=1}^n \log p_i\right)\).

The Log-Sum-Exp Trick

Problem. Compute \(y = \log\left(\sum_{i=1}^n e^{x_i}\right)\) when \(x_i\) are very large or small.

Log-Sum-Exp. For any constant \(c\):

\[y = \log\sum_i e^{x_i} = \log\left(e^{c}\sum_i e^{x_i - c}\right) = c + \log\sum_i e^{x_i - c}\]

Log-Sum-Exp: Choosing the Shift

Implementation

Choose \(c = \max_i x_i\).

  • No overflow: \(\max_i e^{x_i - c} = e^0 = 1\)
  • Minimal underflow impact

Application. Essential for stable computation in mixture models, HMMs, neural networks.

Probability Foundations

Monte Carlo operates on two things: drawing random variables, and taking expectations of functions of them.

Random Variables: Foundation of Simulation

Random Variable. A variable whose outcomes are numerical values of a random phenomenon.

Discrete Example.

Coin toss: \(X = \begin{cases} 0 & \text{if tails} \\ 1 & \text{if heads} \end{cases}\)

\(P[X=1] = p\), \(P[X=0] = 1-p\)

Continuous Example.

Waiting time: \(X \sim \Exp(\lambda)\)

\(f(x) = \lambda e^{-\lambda x}\), \(x > 0\)

To simulate any distribution, we need to understand its CDF and relationships to other distributions.

Cumulative Distribution Function (CDF)

CDF. For a random variable \(X\):

\[F(x) = P[X \leq x], \quad F: \mathbb{R} \rightarrow [0,1]\]

Properties of the CDF.

  1. Non-decreasing: \(x_1 \leq x_2 \Rightarrow F(x_1) \leq F(x_2)\)
  2. Right-continuous: \(\lim_{x \to x_0^+} F(x) = F(x_0)\)
  3. Limits: \(\lim_{x \to -\infty} F(x) = 0\), \(\lim_{x \to \infty} F(x) = 1\)

The CDF completely characterizes a distribution. If we can invert it, we can simulate.

Discrete and Continuous CDFs

Probability Mass and Density Functions

Discrete: PMF.

\[f(x) = P[X = x]\]

  • \(\sum_x f(x) = 1\)
  • \(0 \leq f(x) \leq 1\)
  • \(F(x) = \sum_{t \leq x} f(t)\)

Continuous: PDF.

\[f(x) = \frac{dF(x)}{dx}\]

  • \(\int_{-\infty}^{\infty} f(x)dx = 1\)
  • \(f(x) \geq 0\), and \(f(x)\) can exceed \(1\)
  • \(F(x) = \int_{-\infty}^x f(t)dt\)

Computational Note. For simulation we usually need \(F^{-1}\).

Expected Value and Variance

Expected Value.

Discrete: \[\E[X] = \sum_x x f(x)\] \[\E[h(X)] = \sum_x h(x) f(x)\]

Continuous: \[\E[X] = \int_{-\infty}^{\infty} x f(x) dx\] \[\E[h(X)] = \int_{-\infty}^{\infty} h(x) f(x) dx\]

Variance.

\[\Var(X) = \E[(X - \E[X])^2] = \E[X^2] - (\E[X])^2\]

Computing a Variance Stably

The two expressions for \(\Var(X)\) are algebraically identical. Numerically they are not.

\(\E[X^2] - (\E[X])^2\) subtracts two large, nearly equal numbers whenever \((\E[X])^2 \gg \Var(X)\). Recall the catastrophic cancellations we talked about in the log-sum-exp trick? Compute sample variances from the centered form \(\frac{1}{n-1}\sum_i (x_i - \bar{x})^2\).

Two-pass algorithm. First pass computes \(\bar{x}\); second pass accumulates \(\sum_i(x_i - \bar{x})^2\). Stable, but requires storing all \(n\) values and reading them twice.

Key Discrete Distributions

Bernoulli(\(p\)) \[P[X=1] = p\] \[\E[X] = p\] \[\Var(X) = p(1-p)\]

Binomial(\(n,p\)) \[P[X=k] = \binom{n}{k}p^k(1-p)^{n-k}\] \[\E[X] = np\] \[\Var(X) = np(1-p)\]

Poisson(\(\lambda>0\)) \[P[X=k] = \frac{\lambda^k e^{-\lambda}}{k!}\] \[\E[X] = \lambda\] \[\Var(X) = \lambda\]

Key Relationships.

  • \(\Bin(n,p) \stackrel{d}{=} \sum_{i=1}^n \Ber(p)\) with the \(\Ber(p)\) independent
  • \(\Bin(n,p) \approx \Pois(np)\) when \(n\) is large and \(p\) small with \(np\) fixed

Key Continuous Distributions

Uniform(\(a,b\)) \[f(x) = \frac{1}{b-a}, \quad a < x < b\] \[\E[X] = \frac{a+b}{2}, \quad \Var(X) = \frac{(b-a)^2}{12}\]

Exponential(\(\lambda>0\)) \[f(x) = \lambda e^{-\lambda x}, \quad x > 0\] \[\E[X] = \frac{1}{\lambda}, \quad \Var(X) = \frac{1}{\lambda^2}\]

Normal(\(\mu,\sigma^2\)) \[f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\] \[\E[X] = \mu, \quad \Var(X) = \sigma^2\]

Gamma(\(\alpha>0,\beta>0\)) \[f(x) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha-1} e^{-\beta x}\] \[\E[X] = \frac{\alpha}{\beta}, \quad \Var(X) = \frac{\alpha}{\beta^2}\]

Why Monte Carlo Works

We have integrals we cannot do and distributions we can sample from. The laws of large numbers are what connect the two.

The Weak Law of Large Numbers

Weak Law of Large Numbers (WLLN). If \(X_1, X_2, \ldots, X_n\) are i.i.d. with \(\E|X| < \infty\), then:

\[\bar{X}_n = \frac{1}{n}\sum_{i=1}^n X_i \xrightarrow{P} \E[X] \text{ as } n \to \infty\]

Convergence in Probability: For any \(\epsilon > 0\):

\[\lim_{n \to \infty} P(|\bar{X}_n - \E[X]| > \epsilon) = 0\]

Essentially, this says that for large \(n\), large deviations of the sample mean from the true mean become highly unlikely.

The Strong Law of Large Numbers

Strong Law of Large Numbers (SLLN). Under the same conditions:

\[\bar{X}_n \xrightarrow{a.s.} \E[X] \text{ as } n \to \infty\]

Almost Sure Convergence:

\[P\!\left(\lim_{n \to \infty} \bar{X}_n = \E[X]\right) = 1\]

We can think of this as saying that the sample mean converges to the true mean for “almost every” sequence of random draws.

Both results are asymptotic: they guarantee convergence as \(n \to \infty\), not for any fixed \(n\).

WLLN vs SLLN: Why the Distinction Matters

WLLN Says.

  • For large \(n\), \(\bar{X}_n\) is probably close to \(\E[X]\)
  • No guarantee for every single sequence

Limitation. WLLN does not ensure your specific run converges. It only says the probability of large deviations goes to zero.

SLLN Says.

  • Your actual sequence converges almost surely
  • Probability 1 guarantee for the entire sample path

Interpretation. SLLN gives stronger reassurance for computation.

Both laws say \(\bar{X}_n \to \E[X]\), but neither says how fast. The CLT does.

The Central Limit Theorem

Central Limit Theorem (CLT). If \(X_1, \ldots, X_n\) are i.i.d. with \(\E[X] = \mu\) and \(\Var(X) = \sigma^2 < \infty\), then:

\[\sqrt{n}(\bar{X}_n - \mu) \xrightarrow{d} \Normal(0, \sigma^2)\]

From CLT to Error Bounds

\[ \begin{aligned} \text{From CLT: } & \bar{X}_n \approx \Normal\left(\mu, \frac{\sigma^2}{n}\right) \text{ for large } n\\ \text{Standard Error: } & \text{SE}(\bar{X}_n) = \frac{\sigma}{\sqrt{n}} \\ \text{95% CI: } & \bar{X}_n \pm 1.96 \cdot \frac{\sigma}{\sqrt{n}} \\ \text{Error: } & |\bar{X}_n - \mu| = O_P(n^{-1/2}) \end{aligned} \]

\(\sigma\) is unknown, so the reported interval uses the plug-in estimate \(\hat{\sigma}^2 = \frac{1}{n-1}\sum_{i=1}^n (X_i - \bar{X}_n)^2\), giving \(\bar{X}_n \pm 1.96\,\hat{\sigma}/\sqrt{n}\).

Three Asymptotic Results, Three Roles

Result Statement Role
WLLN \(\bar{X}_n \xrightarrow{P} \mu\) The method targets the right quantity
SLLN \(\bar{X}_n \xrightarrow{a.s.} \mu\) Your specific simulation run converges
CLT \(\sqrt{n}(\bar{X}_n - \mu) \xrightarrow{d} \Normal(0,\sigma^2)\) How close you are at the \(n\) you chose

Monte Carlo Integration

Definition. Monte Carlo integration is a method to approximate an integral using random sampling:

\[\theta = \int_a^b h(x)\,dx \approx \frac{b-a}{n}\sum_{i=1}^n h(U_i), \quad U_i \sim \Uniform(a,b)\]

Replace a deterministic sum with a random average, then apply the LLN.

Why use it.

  • Analytical integration is hard or impossible
  • High-dimensional integrals make grid methods infeasible
  • Monte Carlo error rate: \(O_P(n^{-1/2})\) regardless of dimension

From Integrals to Expectations

The Transformation. For any integral \(\theta = \int_0^1 h(x)dx\):

\[ \begin{aligned} \theta &= \int_0^1 h(x)dx \\ &= \int_0^1 h(x) \cdot 1 \, dx \\ &= \int_0^1 h(x) \cdot f_U(x) \, dx \quad \text{where } f_U(x) = 1 \text{ for } x \in [0,1] \\ &= \E[h(U)] \quad \text{where } U \sim \Uniform(0,1) \end{aligned} \]

Any Integral Is an Expectation

The Key Connection

\[\int h(x) f(x)\, dx = \E_f[h(X)]\]

for any density \(f\). On \([a,b]\) with \(f\) uniform, \(\int_a^b h(x)\,dx = (b-a)\,\E_{\Uniform(a,b)}[h(U)]\).

  • We’ve transformed an integral into an expectation
  • Now we can apply SLLN to approximate it
  • Sample averages \(\xrightarrow{a.s.}\) True integral

Works for any distribution. This is the foundation of Monte Carlo methods.

SLLN and Monte Carlo Integration

Strong Law of Large Numbers. If \(X_1, X_2, \ldots, X_n\) are i.i.d. with \(\E|X| < \infty\), then \(\bar{X}_n = \frac{1}{n}\sum_{i=1}^n X_i \xrightarrow{a.s.} \E[X]\) as \(n \to \infty\).

Monte Carlo Integration. To compute \(\theta = \int_0^1 h(x)\,dx\):

  1. Generate \(U_1,\ldots,U_n \iid \Uniform(0,1)\)
  2. Note: \(\theta = \E[h(U)]\)
  3. Estimate: \(\hat{\theta}_n = \frac{1}{n}\sum_{i=1}^n h(U_i)\)

By SLLN: \(\hat{\theta}_n \xrightarrow{a.s.} \theta\).

SLLN guarantees convergence as \(n \to \infty\). For finite \(n\), error depends on variance and is quantified by CLT.

Why SLLN Provides Stronger Assurance

Monte Carlo Setting. We run one simulation and increase \(n\), we observe one sequence \(\hat{\theta}_1, \hat{\theta}_2, \ldots\), and we want this sequence to converge to the truth.

If We Only Had WLLN.

  • Convergence in probability, not pathwise
  • No guarantee for every sequence
  • Still sufficient for Monte Carlo in practice

With SLLN.

  • Almost sure convergence of your sequence
  • Stronger theoretical reassurance
  • Still asymptotic. Finite-\(n\) error needs the CLT

SLLN gives stronger assurance, CLT gives practical error bounds.

One Sample Path, Converging

What You’re Seeing.

  • One sample path (one simulation run) of \(\{\bar{X}_n\}\).
  • SLLN: this path converges to the true mean (a.s.) eventually.
  • CLT: the 95% band shrinks at rate \(O(n^{-1/2})\).

LLN gives long-run convergence. CLT quantifies finite-\(n\) uncertainty.

The band is pointwise at each \(n\), not simultaneous over the path. Consecutive running means are strongly dependent, so one path can sit outside it for a stretch without contradicting the CLT.

Monte Carlo Error Falls at Rate \(n^{-1/2}\)

Shrinking bands come from CLT, not SLLN. SLLN ensures eventual convergence, CLT quantifies error for finite \(n\).

What You Actually Do in Practice

The theory describes a sequence \(\hat{\theta}_1, \hat{\theta}_2, \dots\) converging to \(\theta\). In practice you never watch that sequence. You fix \(n\), draw one sample \(X_1, \dots, X_n\), compute one number \(\hat{\theta}_n\), and report it. That’s the recipe for Monte Carlo estimation: one sample, one estimate, one confidence interval.

The roles of the two results.

  • SLLN certifies this recipe with explicit guarantees: the estimator targets the right quantity, and you can get arbitrarily close by increasing \(n\). It says nothing about the \(n\) you chose.
  • CLT enters at the \(n\) you chose: \(\hat{\theta}_n \approx \Normal(\theta,\, \sigma^2/n)\), so \(\hat{\theta}_n \pm 1.96\,\hat{\sigma}/\sqrt{n}\) is a 95% confidence interval. This is what you actually report.

SLLN tells you the method works. CLT tells you how well it worked at the \(n\) you used.

General Monte Carlo Principle

Monte Carlo Approximation. For any function \(g\) and random variable \(X\) with \(\E|g(X)| < \infty\):

\[\E[g(X)] \approx \frac{1}{n}\sum_{i=1}^n g(X_i)\]

where \(X_1, \ldots, X_n \iid X\).

Applications.

  • Integration: \(\int_a^b h(x)dx = (b-a)\E[h(U)]\), \(U \sim \Uniform(a,b)\)
  • Probability: \(P[X \in A] = \E[\mathbf{1}_A(X)]\)

If in addition \(\Var[g(X)] < \infty\), the CLT gives \(\left|\frac{1}{n}\sum_i g(X_i) - \E[g(X)]\right| = O_P(n^{-1/2})\), regardless of dimension.

Halving the Error Costs Four Times the Samples

  • Error decreases as \(1/\sqrt{n}\) (not \(1/n\))
  • To halve the error, need four times as many samples
  • Dimension-independent: Same rate for \(\int_0^1\) or \(\int_{[0,1]^{100}}\)

The Curse of Dimensionality in Numerical Integration

Traditional Quadrature Methods. A product rule with \(m\) points per axis costs \(N = m^d\) evaluations:

Dimension Points per axis Total evaluations \(N\)
1D: \(\int_0^1\) \(m\) \(m\)
10D: \(\int_{[0,1]^{10}}\) \(m\) \(m^{10}\)
100D: \(\int_{[0,1]^{100}}\) \(m\) \(m^{100}\)

Exponential Explosion

For \(m = 10\) points per dimension:

  • 3D: \(10^3 = 1{,}000\) evaluations
  • 10D: \(10^{10} = 10\) billion evaluations
  • 100D: \(10^{100}\) evaluations, more than the number of atoms in the observable universe

Traditional methods become computationally impossible.

Quadrature Error Against the Monte Carlo Rate

Product midpoint rule on \(\prod_i e^{x_i}\) over \([0,1]^d\) (exact value \((e-1)^d\)): relative error \(O(N^{-2/d})\) in the total evaluations \(N\). Plain Monte Carlo: \(O_P(N^{-1/2})\) in every \(d\), needing only finite variance. The exponents coincide at \(d = 4\); quadrature wins below it, Monte Carlo above it, and the gap widens with \(d\). Constants keep the midpoint rule ahead at \(d = 4\) here; at \(d = 8\) the rate takes over.

Monte Carlo: Breaking the Curse

Dimension-Independent Convergence. For any dimension \(d\), provided \(\Var[h(\mathbf{U})] = \sigma^2 < \infty\), the Monte Carlo estimator for \(\theta = \int_{[a,b]^d} h(\mathbf{x})d\mathbf{x}\):

\[\hat{\theta}_n = \frac{(b-a)^d}{n}\sum_{i=1}^n h(\mathbf{U}_i), \quad \mathbf{U}_i \sim \Uniform([a,b]^d)\]

has error \(|\hat{\theta}_n - \theta| = O_P(n^{-1/2})\) regardless of \(d\).

The Rate Does Not Depend on the Dimension

Breaking the Curse

Monte Carlo makes high-dimensional integration feasible.

Why It Works.

  • CLT applies to any expectation
  • \(\text{SE} = \frac{\sigma}{\sqrt{n}}\)
  • \(\sigma\) may depend on \(d\), but the rate \(n^{-1/2}\) does not

Same \(n\) for all \(d\). For standard error \(0.01\) with \(\sigma = 1\):

  • 1D: \(n \approx 10{,}000\) samples
  • 10D: \(n \approx 10{,}000\) samples
  • 1000D: \(n \approx 10{,}000\) samples

Punchlines, and What Comes Next

  • An integral you cannot do becomes an expectation you can average, as long as you can sample from a density covering the integrand.
  • The WLLN tells you that you targeted the right object with the right statistic.
  • The SLLN says that your statistic converges almost surely to the target.
  • When the variance is finite, the CLT budgets the approximation cost (at \(n^{-1/2}\)) and hands you a confidence interval with it.
  • Furthermore, that rate is the same in one dimension and in a million!

The Monte Carlo method, together with the LLNs and the CLT, hands us a universal recipe for approximating integrals and expectations. Without the last two, the statistician in us would be left unsatisfied, not ever knowing how close we are to the truth. With them, we can quantify the uncertainty in our approximation and report it.

Everything this week assumed we can draw \(X_1, \ldots, X_n \iid f\). Next week: how.