|
ADS
Algorithms and Data Structures
|
< Philox4x64-10 More...
#include <ADS/numeric/RandomGenerators.h>
Public member functions | |
| AliasTable (const std::vector< double > &w) | |
| the table of the weights \( w \), not all zero | |
| std::size_t | size (void) const |
| the number \( n \) of the items | |
| template<FullWordGenerator G> | |
| std::size_t | operator() (G &&g) const |
| an index drawn with probability proportional to its weight | |
| double | probability (std::size_t i) const |
| the probability of \( i \), as the table gives it | |
< Philox4x64-10
the alias method of Walker and Vose: draws \( i \) with probability \( w_i / \sum_j w_j \), in \( O( 1 ) \) time from one random word
|
explicit |
the table of the weights \( w \), not all zero
In pseudocode (the construction of Vose, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{AliasTable}( w ) \cr 2 & \quad p_i \gets n w_i / \textstyle\sum_j w_j \text{ for each } i \cr 3 & \quad S \gets \{ i : p_i < 1 \};\ L \gets \{ i : p_i \ge 1 \} \cr 4 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{and}\ L \ne \emptyset\ \textbf{do} \cr 5 & \qquad s \gets \text{an item of } S;\ l \gets \text{an item of } L; \ S \gets S \setminus \{ s \} \cr 6 & \qquad \mathit{keep}[ s ] \gets p_s;\ \mathit{alias}[ s ] \gets l; \ p_l \gets p_l + p_s - 1 \cr 7 & \qquad \textbf{if}\ p_l < 1\ \textbf{then}\ \text{move } l \text{ from } L \text{ to } S \cr 8 & \quad \textbf{foreach}\ i \in S \cup L\ \textbf{do} \ \mathit{keep}[ i ] \gets 1 \end{array}} \]
| std::size_t ADS::AliasTable::operator() | ( | G && | g | ) | const |
an index drawn with probability proportional to its weight
In pseudocode (Walker, beyond the book), the cell and the coin from one product:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{draw}( g ) \cr 2 & \quad ( h , l ) \gets \text{the high and the low word of } g() \cdot n \cr 3 & \quad \textbf{if}\ l < \lfloor \mathit{keep}[ h ] 2^{64} \rfloor \ \textbf{then return}\ h \cr 4 & \quad \textbf{return}\ \mathit{alias}[ h ] \end{array}} \]
|
nodiscard |
the probability of \( i \), as the table gives it
|
inlinenodiscard |
the number \( n \) of the items