ArticleslgStudy

mathematics

Alias method

Alias method is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Alias method rather than just read about it. In short: 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.

Alias method — main illustration
Alias method — illustration

Key takeaways

  • Alias method belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Alias method to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Alias method from memory before moving on to harder problems.

Reference excerpt

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. That is, it returns integer values 1 ≤ i ≤ n according to some arbitrary discrete probability distribution pi. The algorithms typically use O(n log n) or O(n) preprocessing time, after which random values can be drawn from the distribution in O(1) time.

Operation Internally, the algorithm consults two tables, a probability table Ui and an alias table Ki (for 1 ≤ i ≤ n). To generate a random outcome, a fair die is rolled to determine an index i into the two tables. A biased coin is then flipped, choosing a result of i with probability Ui, or Ki otherwise (probability 1 − Ui). More concretely, the algorithm operates as follows:

Generate a uniform random variate 0 ≤ x < 1. Let i = ⌊nx⌋ + 1 and y = nx + 1 − i. (This makes i uniformly distributed on {1, 2, ..., n} and y uniformly distributed on [0, 1).) If y < Ui, return i. This is the biased coin flip. Otherwise, return Ki. An alternative formulation of the probability table, proposed by Marsaglia et al. as the square histogram method, avoids the computation of y by instead checking the condition x < Vi = (Ui + i − 1)/n in the third step.

Table generation The distribution may be padded with additional probabilities pi = 0 to increase n to a convenient value, such as a power of two. To generate the two tables, first initialize Ui = npi. While doing this, divide the table entries into three categories:

The "overfull" group, where Ui > 1, The "underfull" group, where Ui < 1 and Ki has not been initialized, and The "exactly full" group, where Ui = 1 or Ki has been initialized. If Ui = 1, the corresponding value Ki will never be consulted and is unimportant, but a value of Ki = i is sensible. This also avoids problems if the probabilities are represented as fixed-point numbers which cannot represent Ui = 1 exactly. As long as not all table entries are exactly full, repeat the following steps:

Arbitrarily choose an overfull entry Ui > 1 and an underfull entry j with 1 − Uj ≤ Ui − 1. (If one of these exists, the other must, as well.) Allocate all unused space in entry j to outcome i, by setting Kj ← i. Remove the allocated space from entry i by changing Ui ← Ui − (1 − Uj) = Ui + Uj − 1. Entry j is now exactly full. Assign entry i to the appropriate category based on the new value of Ui. 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 n −1 iterations. Each iteration can be done in O(1) time, so the table can be set up in O(n) time. Vose 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 Ui 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 y < Ui (because Ki does not need to be consulted), one goal during table generation is to maximize the sum of the Ui. 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 Ui and the smallest Uj. Because this requires sorting the Ui, it requires O(n log n) 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 x each time, even when only a few random bits are needed. One case arises when the probabilities are particularly well balanced, so many Ui = 1. For these values of i, Ki is not needed and generating y is a waste of time. For example if p1 = p2 = 1⁄2, then a 32-bit random variate x 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 Ui ≈ 0. For example if p1 = 0.999 and p2 = 0.001, 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 Donald Knuth, The Art of Computer Programming, Vol 2: Seminumerical Algorithms, section 3.4.1.

Implementations http://www.keithschwarz.com/darts-dice-coins/ Keith Schwarz: Detailed explanation, numerically stable version of Vose's algorithm, and link to Java implementation https://jugit.fz-juelich.de/mlz/ransampl Joachim Wuttke: Implementation as a small C library. https://gist.github.com/0b5786e9bfc73e75eb8180b5400cd1f8 Liam Huang's Implementation in C++ https://github.com/joseftw/jos.weightedresult/blob/develop/src/JOS.WeightedResult/AliasMethodVose.cs C# implementation of Vose's algorithm. https://github.com/cdanek/KaimiraWeightedList C# implementation of Vose's algorithm without floating point instability.

References

Illustrations

Alias method: A diagram of an alias table that represents the probability distribution〈0.25, 0.3, 0.1, 0.2, 0.15〉
A diagram of an alias table that represents the probability distribution〈0.25, 0.3, 0.1, 0.2, 0.15〉

Worked examples

Example 1 — a first encounter with Alias method

Start with the simplest possible case. Write down what Alias method claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Alias method before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Alias method ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Alias method

In research
Alias method appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Alias method in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Alias method is common in secondary-school and first-year university syllabi. It links to neighbouring topics Pseudorandom number generators, so understanding it makes those chapters shorter.
In everyday life
Look for Alias method outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Alias method” →

How to study Alias method in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Alias method means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Alias method out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Alias method in simple terms?

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.

Why does Alias method matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Alias method?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Alias method.

Tags

  • Pseudorandom number generators

Keep exploring