Alias Method
In this lesson we will be implementing the Alias method.
We'll cover the following...
In the previous lesson, we sketched out the “alias method”, which enables us to implement sampling from a weighted discrete distribution in constant time.
Implementing the Alias Method
The implementation of the Alias Method is slightly tricky, but we’ll go through it carefully.
The idea is:
- If we have weights then we’re going to make distributions.
- Every distribution is either a singleton that returns a particular number, or we use a projection on a Bernoulli to choose between two numbers.
- To sample, we uniformly choose from our distributions, and then we sample from that distribution, so each call to
Sample
on the weighted integer distribution does exactly two samples of other, simpler distributions.
How are we going to implement it?
The key insight is weights which are lower than the average weight “borrow” from weights which are higher than average.
Call the sum of all weights . We start by multiplying each weight by , which gives us ...