STAT 536 — Statistical Computing
Fall 2026
Core Objectives. We develop computational methods to solve two fundamental problems:
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).\]
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\]
The objectives are deterministic. We can induce randomness to achieve them.
Origins.
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.
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.
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.
Critical Numerical Issues.
Problems to Avoid:
Solutions:
Example: Computing Probabilities. Instead of \(\prod_{i=1}^n p_i\), compute \(\exp\left(\sum_{i=1}^n \log p_i\right)\).
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}\]
Implementation
Choose \(c = \max_i x_i\).
Application. Essential for stable computation in mixture models, HMMs, neural networks.
Monte Carlo operates on two things: drawing random variables, and taking expectations of functions of them.
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.
CDF. For a random variable \(X\):
\[F(x) = P[X \leq x], \quad F: \mathbb{R} \rightarrow [0,1]\]
Properties of the CDF.
The CDF completely characterizes a distribution. If we can invert it, we can simulate.
Discrete: PMF.
\[f(x) = P[X = x]\]
Continuous: PDF.
\[f(x) = \frac{dF(x)}{dx}\]
Computational Note. For simulation we usually need \(F^{-1}\).
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\]
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.
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.
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}\]
We have integrals we cannot do and distributions we can sample from. The laws of large numbers are what connect the two.
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.
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 Says.
Limitation. WLLN does not ensure your specific run converges. It only says the probability of large deviations goes to zero.
SLLN Says.
Interpretation. SLLN gives stronger reassurance for computation.
Both laws say \(\bar{X}_n \to \E[X]\), but neither says how fast. The CLT does.
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)\]
\[ \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}\).
| 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 |
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.
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} \]
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)]\).
Works for any distribution. This is the foundation of Monte Carlo methods.
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\):
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.
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.
With SLLN.
SLLN gives stronger assurance, CLT gives practical error bounds.
What You’re Seeing.
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.
Shrinking bands come from CLT, not SLLN. SLLN ensures eventual convergence, CLT quantifies error for finite \(n\).
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 tells you the method works. CLT tells you how well it worked at the \(n\) you used.
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.
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.
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}\) |
For \(m = 10\) points per dimension:
Traditional methods become computationally impossible.
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.
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\).
Breaking the Curse
Monte Carlo makes high-dimensional integration feasible.
Why It Works.
Same \(n\) for all \(d\). For standard error \(0.01\) with \(\sigma = 1\):
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.