Advanced sampling and rare eventsLesson 61 of 76 · 8 min

Invert a cumulative distribution

Generate a target distribution by transforming a uniform draw through its inverse CDF.

A cumulative distribution function (CDF) gives F(x)=P(X≤x)F(x)=P(X\le x), the probability at or below x. Its inverse takes a probability level u and returns its quantile. For jumps or flat stretches, use the generalised inverse: the smallest x whose cumulative probability reaches u. Feeding independent uniform levels into it generates the target distribution.

For an exponential distribution with scale ss, the inverse transform is −slog⁡(1−U)-s\log(1-U). NumPy’s log1p(-U) evaluates log⁡(1−U)\log(1-U) accurately when UU is small. The same uniform input should give the same quantile through this formula and the library inverse CDF; keep the domain and units attached.

The transformation is a sampling mechanism, not evidence that an exponential model fits a real process. Check its support, quantiles and moments against the stated target. Other inverse CDFs may require numerical evaluation, and finite precision can matter in extreme tails.

Map probability to a target quantile
00.250.50.75102.65.37.910.6Transformed waitUniform input u
00.250.50.75102.65.37.910.6Transformed waitUniform input u
F−1(u)=−2log⁡(1−u)F^{-1}(u)=-2\log(1-u)
Reference illustration. Exact inverse-CDF transform for exponential scale 2. Uniform inputs near one generate large waits. The shown range stops at 0.99; the target has no finite upper bound.

What does a uniform probability near one map to in a right-tailed exponential distribution?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Maximum inverse-CDF difference: 0.0
Sample / model means: 1.9947 2
Sample median / model median: 1.3808 1.3863

Read the first result

The two implementations share the same uniform inputs. The analytical formula and distribution reference agree for these sampled values.

Try an edit

Add print("Cuts at u=0, .5, .99:", stats.expon.ppf([0, .5, .99], scale=scale).round(4)) immediately after print("Sample median / model median:", round(np.median(transformed), 4), round(scale*np.log(2), 4)). Select Run Python again. Use the new Cuts at u=0, .5, .99 values. What do the three input probabilities mean, and why do the printed cutoffs have the scale’s units?

Back to the Python editor

Scope. This example has a closed-form inverse. Other distributions and extreme probabilities can require careful numerical treatment.

Try a fresh case

What does the inverse CDF take as its input?

Choose an answer
Need a hint?
Hint 1

Write the CDF as a map from values to probabilities.

Hint 2

Reverse the direction of that map.

Your notes