Inverse-transform sampling
Background
Inverse-transform sampling turns uniform random numbers into samples from any distribution with a known CDF. For a discrete distribution with pmf , you build the cumulative distribution and, for each , return the first category whose cumulative probability reaches . It is the standard way numpy/random draw categorical samples.
Problem statement
Implement inverse_transform_sample(pmf, n, seed=0) drawing n integer category indices:
Build the cumulative sum of pmf and use np.searchsorted(cdf, u, side='right') to map each uniform to a category.
Input
pmf—np.ndarray(K,), a probability mass function (sums to 1, non-negative).n—int, number of samples.seed—int, RNG seed (np.random.default_rng).
Output
An np.ndarray of n integer indices in [0, K).
Examples
Example 1
Input: pmf = [0.2, 0.3, 0.5], n = large
Output: indices 0/1/2 occurring ~20% / 30% / 50% of the time
Explanation: the CDF is . A uniform lands in category 0 with probability 0.2 (when ), category 1 with 0.3, and category 2 with 0.5 — reproducing the pmf.
Constraints
- Map each uniform via the cumulative distribution (
searchsorted), not by looping per category. - Returned indices are integers in
[0, len(pmf)). - A degenerate pmf (all mass on one category) must always return that category.
Notes
- The method generalizes to continuous distributions: with the inverse CDF (e.g. for an exponential).
- Using
side='right'ensures maps into the first category that actually carries probability mass.
This problem ships 5 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Empirical frequencies match the pmf
- •Indices are within range
- •Degenerate pmf always returns the same category
- •Returns exactly n samples
- •Reproducible with the same seed