liveBook · Manning

Alias method

As explained in Fabulous Adventures in Data Structures and Algorithms

The alias algorithm is a discrete weighted-sampling technique that preprocesses outcomes into uniform-indexed rows containing at most two possible values.

The alias method preprocesses a weighted categorical distribution into a rectangular table. Each row represents a nonzero-weight category slot and contains at most two possible values, allowing every sample to select a row uniformly and finish with a bounded decision rather than rejection. section 12.11 section 12.13

Construct the table incrementally:

  • Treat the total weight as the width and the number of nonzero categories as the row count; scale each category’s marked area by the row count. section 12.11
  • Pair a short row with a long row, move enough cells from the long row to fill the short row, and repeat until every row has equal width. section 12.11
  • Sample by choosing a row uniformly and then sampling that row’s stored distribution; preprocessing needs at most n balancing attempts. section 12.11 section 12.13

Weights 3, 4, 5, 3

Four categories produce a table with four rows and fifteen columns. Their initial marked areas are 12, 16, 20, and 12 cells. Moving three cells from row 1 to row 0, then balancing the remaining short and long rows, yields a rectangle whose rows encode the original proportions. section 12.11

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains table construction, row balancing, representations, and complexity, while deferring full implementation details to C# code.
Examples
Uses weights 3, 4, 5, and 3 to illustrate balancing, then encodes rows with singleton and Bernoulli distributions.
What it assumes
Assumes weighted categories and discrete distributions, while defining the row-and-cell construction used by the method.
What you can do after
Build it from scratch as a categorical sampler with O(1) draws and no rejection loop.
How it connects
Connects categorical distribution with rejection sampling, discrete uniform distribution, and Bernoulli distribution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.