KatInterview

Alias Method Categorical Sampler

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designarraysdesign-dsmath-bits

Last updated

Generate discrete random samples according to custom category weights in constant time per sample using Vose's alias algorithm.

You are tasked with implementing a fast categorical distribution sampler based on Vose's Alias Method. Given a list of non-negative relative category weights and a sequence of uniform random floating-point numbers in the range $[0, 1)$, generate the corresponding drawn outcome indices.

The alias method constructs an $O(k)$ lookup structure (where $k$ is the number of categories) containing two length-$k$ arrays: threshold (floating-point cutoffs in $[0, 1]$) and alias (fallback indices). Each sample is drawn using a single uniform random value $u \in [0, 1)$:

  1. Calculate a continuous scaled position $x = u \times k$.
  2. Determine the column index $i = \min(\lfloor x \rfloor, k - 1)$.
  3. Compute the fractional residual $f = x - i$.
  4. If $f < \text{threshold}[i]$, choose outcome $i$. Otherwise, choose outcome $\text{alias}[i]$.

Construction Algorithm Details

  1. If the input list of weights is empty, return an empty list of samples.
  2. If the total sum of weights is less than or equal to $0$, raise a ValueError.
  3. Normalize and scale the input weights so their average is $1.0$: $\text{scaled}[i] = \frac{\text{weight}[i] \times k}{\sum \text{weights}}$.
  4. Separate category indices into two work lists: small (indices where $\text{scaled}[i] < 1.0$) and large (indices where $\text{scaled}[i] \ge 1.0$).
  5. While both small and large are non-empty:
    • Pop an index $s$ from small and an index $l$ from large.
    • Set $\text{threshold}[s] = \max(0.0, \min(1.0, \text{scaled}[s]))$.
    • Set $\text{alias}[s] = l$.
    • Subtract $(1.0 - \text{threshold}[s])$ from $\text{scaled}[l]$.
    • If the new $\text{scaled}[l] < 1.0$, push $l$ into small; otherwise, push $l$ into large.
  6. For any remaining indices left in either small or large (due to floating-point rounding), set $\text{threshold}[i] = 1.0$ and $\text{alias}[i] = i$.

Sampling Rules

For each value $u$ in uniforms:

  • If $u < 0.0$ or $u \ge 1.0$, raise a ValueError.
  • Map $u$ to a sampled category index using the alias table lookup described above.

Constraints

  • $0 \le k \le 10^5$, where $k$ is the length of weights.
  • $0 \le m \le 10^5$, where $m$ is the length of uniforms.
  • Each element in weights is a non-negative float ($0.0 \le \text{weights}[i] \le 10^9$).
  • Each element in uniforms is a float.

Standard Inputs & Outputs

  • Input: weights: List[float], uniforms: List[float]
  • Output: List[int] (the sampled category indices)

Examples

Example 1

Input: alias_method_samples([0.5, 0.3, 0.2], [0, 0.19, 0.2, 0.49, 0.5, 0.79, 0.8, 0.99])

Output: [0, 0, 0, 1, 1, 2, 2, 0]

Example 2

Input: alias_method_samples([1], [0, 0.2, 0.999999])

Output: [0, 0, 0]

Example 3

Input: alias_method_samples([0.25, 0.25, 0.25, 0.25], [0, 0.249999, 0.25, 0.5, 0.749999, 0.75, 0.999999])

Output: [0, 0, 1, 2, 2, 3, 3]

Example 4

Input: alias_method_samples([0.7, 0, 0.3], [0, 0.2, 0.69, 0.7, 0.99])

Output: [0, 0, 2, 2, 0]

Hints

Hint 1

Construct two work lists, one for indices with scaled weight < 1.0 and one for indices with scaled weight >= 1.0.

Hint 2

In each step of the table build, pair a underfull index with an overfull index to finalize a column in O(1) time.

Hint 3

Use the integer part of (u * k) to select the column and the fractional residual as the comparison threshold.