skip to content

How does inverse-transform sampling turn uniform draws into a target distribution?

level: middleimportance: must knowfreq 54%

answer

  1. start from a Uniform(0,1) draw
  2. the CDF maps the target onto (0,1)
  3. run that mapping backwards
  4. exponential: take a logarithm of U
  5. discrete case walks a cumulative ladder

basics

~10 s

Apply the target's inverse CDF to a Uniform(0,1) draw: X = F^-1(U) has exactly the distribution F. An exponential with rate lambda comes out as -ln(U)/lambda. Discrete variables use a cumulative-probability ladder instead.

solid answer

~40 s

Every continuous distribution with CDF `F` satisfies the probability integral transform: if `U` is Uniform(0,1), then `X = F^-1(U)` has CDF `F`. The one-line proof is `P(F^-1(U) <= x) = P(U <= F(x)) = F(x)`, using that `F` is non-decreasing and `U` is uniform. So a single uniform draw plus one inverse-CDF evaluation gives an exact draw from the target. For an exponential with rate lambda, `F(x) = 1 - exp(-lambda*x)`, so `F^-1(u) = -ln(1-u)/lambda`, and since `1-U` is also Uniform(0,1) the shorter `-ln(U)/lambda` works too. For a discrete distribution you walk a cumulative-probability ladder: draw `U` and return the first category whose cumulative probability reaches `U`. The method is exact and needs exactly one uniform per draw, but only where the inverse CDF is cheap to evaluate.

go deeper

for a junior

Recall the shape of the recipe: one uniform draw in, one inverse-CDF evaluation out. Memorise the exponential case as minus the log of a uniform divided by the rate.

for a middle

Prove it in a line: P(F inverse of U is at most x) equals P(U at most F(x)) equals F(x). Then show both the continuous and the discrete cumulative-ladder versions.

for a senior

Judge when it is the wrong tool: an inverse CDF that must be found numerically per draw may cost more than a well-chosen alternative sampler, and per-draw cost dominates long simulation runs.

for a principal

Own the standardisation call: one uniform per draw and a fixed seed makes simulation runs auditable and comparable across teams, which is often worth more than shaving microseconds with a faster but stream-hungry sampler.

## The problem it solves A random number generator hands you Uniform(0,1) values. Simulation needs draws from exponentials, discrete categories, power laws, custom distributions fitted to data. Inverse-transform sampling is the general bridge between the two. ## The theorem Let `X` be a random variable with cumulative distribution function `F(x) = P(X <= x)`. `F` is non-decreasing, runs from 0 to 1, and for a continuous strictly increasing `F` it has an inverse `F^-1`. Claim: if `U` is Uniform(0,1), then `Y = F^-1(U)` has CDF `F`. Proof: `P(Y <= x) = P(F^-1(U) <= x)`. Since `F` is non-decreasing, `F^-1(U) <= x` exactly when `U <= F(x)`. And for a uniform variable, `P(U <= t) = t` for any `t` between 0 and 1. So `P(Y <= x) = F(x)`, which is the definition of having distribution `F`. The companion statement, the probability integral transform, runs the other way: if `X` is continuous with CDF `F`, then `F(X)` is Uniform(0,1). One direction manufactures draws, the other flattens them. ## Worked case: the exponential An exponential with rate lambda has CDF `F(x) = 1 - exp(-lambda*x)` for `x >= 0`. Solve `u = 1 - exp(-lambda*x)` for `x`: exp(-lambda*x) = 1 - u x = -ln(1 - u) / lambda So `X = -ln(1-U)/lambda` is exponential with rate lambda and mean `1/lambda`. Because `1-U` is itself Uniform(0,1) when `U` is, the equivalent `X = -ln(U)/lambda` is used in practice; it saves a subtraction and is the form worth remembering. Note the rate-versus-mean trap: dividing by lambda gives rate lambda, while multiplying by lambda would give rate `1/lambda`. ## Worked case: a discrete variable Suppose outcomes A, B, C have probabilities 0.5, 0.3, 0.2. Build the cumulative ladder 0.5, 0.8, 1.0. Draw `U` and return the first rung that `U` does not exceed: `U = 0.42` gives A, `U = 0.77` gives B, `U = 0.91` gives C. This is inverse transform with the generalised inverse `F^-1(u) = min{x : F(x) >= u}`, which is defined even where `F` jumps or is flat, so the method covers discrete and mixed distributions without modification. A linear scan costs O(k) per draw for `k` categories; a binary search over the cumulative array costs O(log k), which matters when the ladder is long and the draws are many. ## Where it breaks down The method is only as practical as the inverse CDF. The normal distribution has no closed-form inverse CDF, so drawing normals this way needs a numerical approximation of the inverse; alternatives exist that avoid it, notably the Box-Muller transform, which turns two independent uniforms `U1, U2` into two independent standard normals via `sqrt(-2*ln(U1))*cos(2*pi*U2)` and the same expression with sine. Many fitted or empirical distributions have a CDF you can only evaluate numerically, and inverting it per draw can cost more than an alternative sampler. When the inverse is expensive or unavailable, rejection sampling with a convenient envelope is the usual fallback. ## Why practitioners like it anyway Three properties keep inverse transform in constant use. It is exact, with no acceptance step and no tuning. It consumes exactly one uniform per draw, which makes runs reproducible and easy to reason about when a seed is fixed. And it is monotone: larger `U` gives larger `X`. Monotonicity is what makes it compose cleanly with paired-draw tricks, since the pair `U` and `1-U` maps to a matched low-high pair of target draws, and with common random numbers when two competing system designs must be compared on the same underlying randomness. ## What to say out loud State the theorem with its one-line proof, give the exponential as the worked example, give the cumulative ladder as the discrete case, and name the normal as the standard counter-example where the inverse CDF is not available in closed form.

  • Why can't you sample a normal distribution this way in closed form?
    The normal CDF has no elementary closed form, so its inverse has none either; implementations rely on numerical approximations. That is why alternatives exist, such as the Box-Muller transform, which converts two independent uniforms into two independent standard normals with `sqrt(-2*ln(U1))*cos(2*pi*U2)` and the sine counterpart.
  • How do you handle a discrete distribution with many categories efficiently?
    Precompute the cumulative probabilities once, then binary-search the array for each uniform draw: O(log k) instead of an O(k) linear scan over `k` categories. The precomputation is done once for the whole run, so only the search cost is paid per draw. For very large category counts with fixed probabilities, alias-style table methods reach constant time per draw.
  • Why does inverse transform pair so well with matched-pair simulation tricks?
    It is monotone: bigger `U` always yields a bigger `X`. So the paired uniforms `U` and `1-U` produce a matched low-high pair of target draws whose randomness cancels, and reusing the same uniform stream across two competing designs makes their difference far less noisy than two independent runs would.

The CDF flattens any distribution onto the interval from 0 to 1, like unrolling a lumpy carpet. Sampling uniformly on the flat version and rolling it back up reproduces the original lumps.

saying these in an interview costs you the question

  • Applies the CDF instead of its inverse to the uniform draw
  • Writes the exponential draw as -lambda times ln(U)
  • Claims every distribution has a closed-form inverse CDF
  • Thinks the method needs an acceptance or rejection step
  • Cannot state the discrete cumulative-ladder version

context