LearnQuestion bank

With only a uniform random-number generator, how would you sample from (a) an exponential distribution and (b) a distribution whose density you can evaluate up to a constant but cannot invert? Discuss efficiency, including in high dimensions.

ResearchCodingMedium

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Exponential: exact inverse CDF X = −ln(1−U)/λ, O(1).
  • Unnormalised density: rejection with envelope Mq ≥ p; accept with p/(Mq); acceptance 1/M normalised, constant Z/M unnormalised; M grows exponentially with dimension, so acceptance collapses in high dimensions.
  • Expectations: importance sampling, self-normalised weights; watch weight degeneracy.
  • High dimensions: Metropolis–Hastings; Z cancels; tuning/mixing/burn-in.
  • Categorical: cumulative sums + binary search O(log n); alias method O(1) after O(n) setup.
  • Language models: temperature, top-k, nucleus/top-p as truncated categorical sampling.

In full

Core point: use inverse CDF when available; otherwise use rejection/MCMC and never need the normalising constant. For exponential, draw UUnif(0,1) and set X=−ln(1−U)/λ (equivalently −ln U/λ), exact and O(1). For a density p∝q that cannot be inverted, pick a proposal g with M g(x) ≥ q(x), sample yg, and accept with probability q(y)/(M g(y)); for a normalised target with envelope M g ≥ p the acceptance rate is 1/M, and in the unnormalised case it is the constant Z/M, unknown but not needed. Rejection sampling breaks down in high dimensions: for most targets the smallest valid M grows exponentially with dimension (a slightly mismatched Gaussian proposal in d dimensions already needs M growing like c^d), so the acceptance rate collapses. For expectations, importance sampling estimates E_p[h] as E_g[h q/g]/E_g[q/g] (self-normalised), but weights can degenerate in high dimensions, so Metropolis–Hastings is usually more practical: propose y~r(y|x), accept with min(1, q(y)r(x|y)/(q(x)r(y|x))), which cancels Z and gives asymptotically exact samples at the cost of burn-in, tuning, and correlated draws. For finite categorical distributions, cumulative sums plus binary search give O(log n) sampling after O(n) setup, while the alias method gives O(1) sampling after O(n) setup. In language models this is the same categorical problem: temperature rescales logits p_i∝exp(z_i/T), top-k keeps the k largest probabilities and renormalises, and nucleus/top-p keeps the smallest set with cumulative mass ≥p and renormalises, trading exactness for controlled diversity.

Corrected by an independent AI reviewer.

What a strong answer covers

  • Inverse CDF: X = −ln(1−U)/λ.
  • Rejection sampling with an envelope Mq ≥ p; acceptance rate 1/M.
  • Importance sampling for expectations; MCMC (Metropolis–Hastings) for high-dimensional cases.
  • Categorical sampling via cumulative sums or the alias method.
  • For language models: temperature, top-k and nucleus sampling.

Common mistakes

  • An envelope that does not dominate the target.
  • Ignoring the curse of dimensionality for rejection sampling.