Hexa Scholar

Inverse transform sampling

A brass funnel and curved ivory chute above an empty navy bowl, with wooden balls beside the stand.
A funnel, chute and receiving bowl suggest a machine that turns inputs into outputs.

The question

Can you shape a CDF so that 800 uniform draws produce a chosen four-bin histogram?

Build the sampler, then separate its distribution from the noise in one run.

  1. 1 SeeShape a CDF to match a histogram.
  2. 2 TryTest an exponential sampler.
  3. 3 ProveBuild the quantile proof.
  4. 4 ApplyGenerate values and audit a sampler.
Before you start · 3 quick checks on CDFs, uniform probabilities and logarithms

Two ideas before you start

2 ideas

A uniform input is a probability level

A constant density on the unit interval, with a selected interval shaded.

A uniform draw selects a level between zero and one. An interval's length is its probability.

Exact definition

Write \(U\sim\operatorname{Uniform}(0,1)\). For \(0\le a\le b\le1\), \(P(a\lt U\le b)=b-a\). Each endpoint has probability zero. The mathematical model uses a continuous uniform variable; computer draws have finite precision.

A CDF records probability already accumulated

The CDF of a uniform variable on an interval, with a selected position marked.

The horizontal coordinate is a possible value; the vertical coordinate is accumulated probability. Keep those roles separate.

Exact definition

A cumulative distribution function is \(F(x)=P(X\le x)\). It is nondecreasing, right-continuous, and tends to zero at negative infinity and to one at positive infinity. Here the card uses a uniform variable on \([0,4]\), so \(F(x)=x/4\) inside that interval. Outside it, extend the graph by zero on the left and one on the right. See PMF, PDF and CDF for how a CDF relates to masses and density areas.

Refresher: infimum and right-continuity

The infimum of a nonempty set bounded below is its greatest lower bound. It need not belong to the set. Right-continuity means \(F(x+h)\to F(x)\) as positive \(h\) tends to zero. For a CDF, the events \(\{X\le x+h\}\) decrease to \(\{X\le x\}\); continuity of probability for decreasing events gives this limit. These facts will let the proof include jumps and flat stretches, where an ordinary inverse may not exist.

We use real-valued random variables. A CDF describes a probability distribution, not a guarantee that any finite sample will have precisely those proportions.

1 See

Build a sampler for the target

Outlined bars: target probability in each bin. Filled bars appear after a run and show sample proportions. The horizontal scale is the output value; bar heights are probabilities.
Move the handles on your piecewise linear CDF. Its endpoints stay fixed. Handles cannot cross, so the curve remains nondecreasing.
What the score measures

The distribution score is \(100(1-D)\), where \(D=\tfrac12\sum_i|p_i-t_i|\), \(p_i\) is your curve's probability in bin \(i\), and \(t_i\) is the target probability. A perfect distribution score means these bin probabilities agree. The sample discrepancy uses observed proportions instead. It can remain positive even for a perfect curve.

Each run uses the same seeded inputs, making edits comparable. Repeating Run without changing the curve repeats the result. This is a teaching experiment, not a test of the generator's independence. Endpoints are held at \((0,0)\) and \((4,1)\); arrow keys move by 0.01, Page Up or Page Down by 0.1, and Home or End moves to the neighboring handle's height.

2 Try

Test an exponential sampler

Suppose a simulation needs exponential waiting times with rate \(\lambda\gt0\) per minute. Its CDF is \(F(x)=1-e^{-\lambda x}\) for \(x\ge0\), and zero for negative \(x\). The exponential distribution lesson explains when this waiting-time model is appropriate.

3 Prove

Why the output has the requested CDF

Inverse transform theorem

Let \(F\) be a CDF on the real line and let \(U\sim\operatorname{Uniform}(0,1)\). For \(0\lt u\lt1\), define the generalized inverse, or quantile, by

\[Q(u)=\inf\{x\in\mathbb R:F(x)\ge u\}.\]

Then \(X=Q(U)\) has CDF \(F\). No positive density, strict increase, or differentiability is assumed. For the proof write \(A_u=\{t:F(t)\ge u\}\) and \(q=\inf A_u\). Each proof choice concerns these objects under the stated assumptions.

4 Apply

Generate values and check the rule

Use what you found above. Each problem has hints and a worked solution. Enter a decimal; answers within 0.0005 of the unrounded value are accepted. The tolerance is for arithmetic practice, not a claim about the error of a simulation.

Key point Begin with a valid CDF, transform an input level with its quantile, and keep the endpoint convention explicit. Matching a few sampled counts does not prove the sampler correct.
Level UndergraduatePrerequisites CDFs and probability; logarithms and greatest lower bounds refreshed hereTime About 30 minutes

Further reading