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 chance of hitting a star from row , an chance of hitting a star from row , a chance of hitting a star from row , and that leaves a chance of going again. We will eventually pick a *
, and the values generated will conform to the desired distribution.
Let’s implement it. We’ll throw away our previous attempt and start over.
private readonly List<IDistribution<int>> distributions;
private WeightedInteger(List<int> weights)
{
this.weights = weights;
this.distributions =
new List<IDistribution<int>>(weights.Count);
int max = weights.Max();
foreach(int w in weights)
distributions.Add(Bernoulli.Distribution(w, max – w));
}
All right, we have three distributions; in each, a zero is a success and a one is a failure. In our example of weights , , , the first distribution is “10 to 1 odds of success”, the second is “always success”, and the third is “5 to 6 odds of success”. And now we sample in a loop until we succeed. We uniformly choose a distribution and then sample from that distribution until we get success.
public int Sample()
{
var rows = SDU.Distribution(0, weights.Count – 1);
while (true)
{
int row = rows.Sample();
if (distributions[row].Sample() == 0)
return row;
}
}
We do two samples per loop iteration; how many iterations are we likely to do? In our example, we’ll get a result on the first iteration of the time, because we have hits out of possibilities. That’s of the time. We get a result after one or two iterations of the time. The vast majority of the time we are going to get a result in just a handful of iterations.
But now let’s think again about pathological cases. Remember our distribution from last time that had weights: . Consider what that histogram looks like.
0|*------...--- (1 star, 1000 dashes)
1|*------...--- (1 star, 1000 dashes)
...
...
...
998|*------...--- (1 star, 1000 dashes)
999|*******...*** (1001 stars, 0 dashes)
Our first example has stars and dashes. In our pathological example, there will be stars and dashes, so the probability of exiting the loop on any particular iteration is about one in . We are typically going to loop hundreds of times in this scenario! This is far worse than our option in pathological cases.
Rejection Sampling
This algorithm is called “rejection sampling” (because we “reject” the samples that do not hit a *
) and it works very well if all the weights are close to the maximum weight, but it works extremely poorly if there is a small number of high-weight outputs and a large number of low-weight outputs.
Fortunately, there is a way to fix this problem. We are going to reject rejection sampling and move on to our third and final technique.
Let’s re-draw our original histogram, but I’m going to make two changes. First, instead of stars, we are going to fill in the number sampled, and make it a by rectangle, and triple the size of every row.
0|000000000000000000000000000000---
1|111111111111111111111111111111111
2|222222222222222------------------
This is logically no different; we could “throw a dart”, and the number that we hit is the sample; if we hit a dash, we go again. But we still have the problem that out of times we’re going to hit a dash.
Our goal is to get rid of all the dashes, but we are going to start by trying to get dashes in each row. There are dashes available, three rows, so that’s seven in each row.
To achieve that, we are going to first pick an “excessive” row (too many numbers, too few dashes) and “deficient row” (too few numbers, too many dashes) and move some of the numbers from the excessive row to the deficient row, such that the deficient row now has exactly seven dashes. For example, we’ll move eleven of the s into the row, and swap eleven of the row’s dashes into the row.
0|0000000000000000000--------------
1|111111111111111111111111111111111
2|22222222222222200000000000-------
We’ve achieved our goal for the row. Now we do the same thing again. We are going to move seven of the s into the row:
0|00000000000000000001111111-------
1|11111111111111111111111111-------
2|22222222222222200000000000-------
We’ve achieved our goal. And now we can get rid of the dashes without changing the distribution:
0|00000000000000000001111111
1|11111111111111111111111111
2|22222222222222200000000000
Now we can throw darts and never get any rejections!
The result is a graph where we can again, pick a column, and then from that column sample which results we want; there are only ever one or two possibilities in a column, so each column can be represented by a Bernoulli or Singleton distribution. Therefore we can sample from this distribution by doing two samples: a uniform integer to pick the row, and the second one from the distribution associated with that row.
You might wonder why we chose to move eleven s and then seven s. We didn’t have to! I also could have moved eleven s into the row, and then four s into the row. Doesn’t matter; either would work. As we’ll see in the next lesson, as long as you make progress towards a solution, we’ll find a solution.
This algorithm is called the alias method because some portion of each row is “aliased” to another row. But for now, let’s have a look at the changes we have made in this lesson.
Implementation
In this lesson, we update the WeightedInteger.cs
file. The rest of the code remains the same as the previous lesson.
Get hands-on with 1400+ tech skills courses.