Mapping a uniform draw to a discrete law
Question
You have a generator of uniform numbers and a discrete distribution taking values with probabilities . How do you produce samples from that distribution?
Short answer
Inverse transform on the cumulative probabilities. Precompute , draw , and return the first with . A linear scan costs per draw; binary search on the cumulative array gives , and Walker's alias method gives per draw after setup.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.2, p.157; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solutionLessons behind this question
- Root-Finding and Numerical Integration
- Continuous Random Variables and Probability Density Functions
- Cumulative Distribution Functions
Start with a free lesson: Normal Distribution is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.
Read it free