Alias method explained

In computing, the alias method is a family of efficient algorithms for sampling from a discrete probability distribution, published in 1974 by Alastair J. Walker.[1] [2] That is, it returns integer values according to some arbitrary discrete probability distribution . The algorithms typically use or preprocessing time, after which random values can be drawn from the distribution in time.[3]

Operation

Internally, the algorithm consults two tables, a probability table and an alias table (for). To generate a random outcome, a fair die is rolled to determine an index into the two tables. A biased coin is then flipped, choosing a result of with probability, or otherwise (probability).[4]

More concretely, the algorithm operates as follows:

  1. Generate a uniform random variate .
  2. Let and . (This makes uniformly distributed on and uniformly distributed on .)
  3. If, return . This is the biased coin flip.
  4. Otherwise, return .

An alternative formulation of the probability table, proposed by Marsaglia et al. as the square histogram method, avoids the computation of by instead checking the condition in the third step.

Table generation

The distribution may be padded with additional probabilities to increase to a convenient value, such as a power of two.

To generate the two tables, first initialize . While doing this, divide the table entries into three categories:

If, the corresponding value will never be consulted and is unimportant, but a value of is sensible. This also avoids problems if the probabilities are represented as fixed-point numbers which cannot represent exactly.

As long as not all table entries are exactly full, repeat the following steps:

  1. Arbitrarily choose an overfull entry and an underfull entry . (If one of these exists, the other must, as well.)
  2. Allocate the unused space in entry to outcome, by setting .
  3. Remove the allocated space from entry by changing .
  4. Entry is now exactly full.
  5. Assign entry to the appropriate category based on the new value of .

Each iteration moves at least one entry to the "exactly full" category (and the last moves two), so the procedure is guaranteed to terminate after at most iterations. Each iteration can be done in time, so the table can be set up in time.

Vose[3] points out that floating-point rounding errors may cause the guarantee referred to in step 1 to be violated. If one category empties before the other, the remaining entries may have set to 1 with negligible error. The solution accounting for floating point is sometimes called the Walker-Vose method or the Vose alias method.

Because of the arbitrary choice in step 1, the alias structure is not unique.

As the lookup procedure is slightly faster if (because does not need to be consulted), one goal during table generation is to maximize the sum of the . Doing this optimally turns out to be NP hard, but a greedy algorithm comes reasonably close: rob from the richest and give to the poorest. That is, at each step choose the largest and the smallest . Because this requires sorting the, it requires time.

Efficiency

Although the alias method is very efficient if generating a uniform deviate is itself fast, there are cases where it is far from optimal in terms of random bit usage. This is because it uses a full-precision random variate each time, even when only a few random bits are needed.

One case arises when the probabilities are particularly well balanced, so many . For these values of, is not needed and generating is a waste of time. For example if, then a 32-bit random variate could be used to generate 32 outputs, but the alias method will only generate one.

Another case arises when the probabilities are strongly unbalanced, so many . For example if and, then the great majority of the time, only a few random bits are required to determine that case 1 applies. In such cases, the table method described by Marsaglia et al. is more efficient. If we make many choices with the same probability we can on average require much less than one unbiased random bit. Using arithmetic coding techniques arithmetic we can approach the limit given by the binary entropy function.

Literature

Implementations

Notes and References

  1. 10.1049/el:19740097 . New fast method for generating discrete random numbers with arbitrary frequency distributions . Electronics Letters . 10 . 8 . 127–128 . 18 April 1974 . Walker . A. J. . 1974ElL....10..127W .
  2. 10.1145/355744.355749 . An Efficient Method for Generating Discrete Random Variables with General Distributions . ACM Transactions on Mathematical Software . 3 . 3 . 253–256 . September 1977 . Walker . Alastair J. . 4522588 . free .
  3. 10.1109/32.92917 . A linear algorithm for generating random numbers with a given distribution . IEEE Transactions on Software Engineering . 17 . 9 . 972–975 . September 1991 . Vose . Michael D. . https://web.archive.org/web/20131029203736/http://web.eecs.utk.edu/~vose/Publications/random.pdf . 2013-10-29. 10.1.1.398.3339 .
  4. Web site: Darts, Dice, and Coins: Sampling from a Discrete Distribution . 29 December 2011 . KeithSchwarz.com . 2011-12-27.