AIToolbox
A library that offers tools for AI problem solving.
|
This class represents the Alias sampling method. More...
#include <AIToolbox/Utils/Probability.hpp>
Public Member Functions | |
VoseAliasSampler (const ProbabilityVector &p) | |
Basic constructor. More... | |
template<typename G > | |
size_t | sampleProbability (G &generator) const |
This function samples a number that follows the distribution of the class. More... | |
This class represents the Alias sampling method.
This is an O(1) way to sample from a fixed distribution. Construction takes O(N).
The class stores two vectors of size N, and converts the input probability distribution into a set of N weighted coins, each of which represents a choice between two particular numbers.
When sampled, the class simply decides which coin to use, and it rolls it. This is much faster than the sampleProbability method, which is O(N), as it needs to iterate over the input probability vector.
This is the preferred method of sampling for distributions that generally do not change (as if the distribution changes, the instance of VoseAliasSampler must be rebuilt).
AIToolbox::VoseAliasSampler::VoseAliasSampler | ( | const ProbabilityVector & | p | ) |
Basic constructor.
p | The probability distribution to sample from. |
|
inline |
This function samples a number that follows the distribution of the class.
generator | A random number generator. |