Sampling turns hard expectations and hard-to-draw distributions into algorithms. These notes build from Monte Carlo averages to inverse transform sampling, importance sampling, and rejection sampling.
Big picture: sometimes we want estimates, such as $\mathbb{E}[X]$ or $\mathbb{E}_f[w(X)]$; sometimes we want actual samples from a distribution. The algorithms below differ mainly in which of these goals they solve and what access they assume.
Sampling is a way to learn about a random variable: estimate expectations, distributional properties, and other functionals that are hard to compute in closed form. The simplest case is when we can run the random process itself many times.
Suppose there is a randomized process with associated random variable $X$. How do we discover $\mathbb{E}[X]=\mu$?
The law of large numbers is the reason this works: as $n$ grows, the sample average becomes a better estimate of the true expectation.
So the first algorithmic question becomes: if the computer gives us uniforms, how do we turn those uniforms into samples from some target distribution $f$?
The target density $f$ tells us where probability mass lives. For a continuous density, the probability of landing in a small bin around $x$ is the area under $f$ over that bin:
The cumulative distribution function (CDF) packages these areas into a monotone map $F$. This monotonicity is the key: a uniform number $r\in[0,1]$ can be interpreted as a target cumulative probability.
Draw $r\sim U[0,1]$, then read off $X=F^{-1}(r)$ from the CDF.
The returned value has the desired distribution. This method is exact whenever the inverse CDF is available and cheap enough to compute.
Inverse transform gives actual samples from $f$, but it needs $F^{-1}$. Often that is not realistic. A different goal is easier: instead of sampling from $f$, estimate expectations under $f$ using a distribution we can sample from.
Why direct sampling may be hard.
Suppose instead we can sample from a proposal $g(x)$. Start from the target expectation:
and rewrite it under $g$:
Estimate the right-hand side by Monte Carlo with samples $x_i\sim g$:
Thus we can estimate $\mathbb{E}_f[w(x)]$ using a proposal distribution $g$, without ever sampling from $f$. The price is that each sample gets reweighted by $f(x_i)/g(x_i)$.
Write the importance-weighted random variable
Then $\mathbb{E}_g[y]=\mathbb{E}_f[w]=\mu$, but $\mathrm{Var}_g(y)$ depends strongly on the choice of $g$.
This is the central tradeoff in importance sampling: unbiasedness is easy to preserve, but a poor proposal can make the variance enormous.
The previous section reduced proposal quality to estimator variance. Now ask what proposal would be ideal. We want a proposal that keeps the estimator unbiased while minimizing variance. With $y=f w / g$ and $x\sim g$,
So the design question is:
Taking $a=f|w|/\sqrt{g}$ and $b=\sqrt{g}$ gives
Equality holds when the two factors are proportional:
If $w(x)>0$ for all $x$, the absolute value disappears and $g\propto f\,w$.
When $w>0$ and we take $g=f w / c$ with $c=\int f w\,dx=\mu$,
Still, the message is practical: a good proposal should put mass where $|f w|$ is large. It does not have to be exactly optimal to help; it just needs to avoid wasting samples in unimportant regions.
A good proposal overlaps the important region of $|f w|$; a bad one wastes samples where the integrand is tiny.
Let $S=x_1+\cdots+x_n$ with $x_i\in\mathbb{R}$. Estimate the sum by sampling indices $i$ instead of reading every term. If index $i$ is sampled with probability $p_i$, an unbiased one-sample estimator is $x_i/p_i$. What choice of $p_i$ minimizes the estimator variance?
What if instead $\vec S=\vec x_1+\cdots+\vec x_n$ with $\vec x_i\in\mathbb{R}^d$? What should the sampling probabilities depend on now?
Importance sampling estimates expectations under $f$, but its samples still come from $g$. Sometimes we really need accepted samples whose distribution is $f$ itself. Rejection sampling converts proposal samples into target samples by randomly accepting only some of them.
Suppose we can sample from a proposal $g$, and we know a constant $M$ such that $g$ covers the support of $f$ and
The envelope $Mg$ must dominate $f$. Acceptance probability at $x$ is the vertical ratio $f(x)/(Mg(x))$.
Accepted samples follow the target density. Schematically,
The constant $M$ affects efficiency, not the final accepted distribution: larger $M$ means lower acceptance probability.
Sample a point uniformly from the unit disk in $d=2$ using only Uniform$(0,1)$ coordinates. First map two uniforms to a point in the square $[-1,1]^2$, then reject points outside the unit circle.
The rejection rate is \[ 1-\frac{\mathrm{Area(circle)}}{\mathrm{Area(square)}}. \] For $d=2$, the acceptance probability is $\pi/4$, so the rejection rate is $1-\pi/4$. What happens as $d$ grows? Can the same approach be used for a high-dimensional ball?
Sampling is the algorithmic side of the same concentration ideas that appear in the tail-bounds notes: once you can draw samples, Monte Carlo turns expectations into averages with explicit variance and concentration tradeoffs.