Non-uniform Discrete Distribution (Continued)
In this lesson, we will continue modifying our non-uniform discrete distribution and also learn about rejection sampling.
We'll cover the following...
In the previous lesson, we sketched out an algorithm for sampling from a weighted distribution of integers by implementing a discrete variation on the “inverse transform” method where the “inverse” step involves a binary search.
Modification of Non-Uniform Discrete Distribution
Can we do better than ? Yes — but we’ll need an entirely different method. We will sketch out two methods, and implement one this time, and the other in the next lesson.
Again, let’s suppose we have some weights, say , , . We have three possible results, and the highest weight is . Let’s construct a by rectangle that looks like our ideal histogram; we’ll put dashes to indicate the “spaces”:
0|**********-
1|***********
2|*****------
Here’s an algorithm for sampling from this distribution:
- Uniformly choose a random row and column in the rectangle; it’s easy to choose a number from to for the row and a number from to for the column.
- If that point is a
*
, then the sample is the generated row number. - If the point is a
–
, try again.
Basically, we’re throwing darts at the rectangle, and the likelihood of hitting a valid point on a particular row is proportional to the probability of that row.
In case that’s not clear, let’s work out the probabilities. To throw our dart, first we’ll pick a row, uniformly, and then we’ll pick a point in that row, uniformly. We have a ...