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 , 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 , the inverse transform is . NumPy’s log1p(-U) evaluates accurately when 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.
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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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?
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?
Need a hint?
Hint 1
Write the CDF as a map from values to probabilities.
Hint 2
Reverse the direction of that map.