AIToolbox
A library that offers tools for AI problem solving.
AIToolbox::VoseAliasSampler Class Reference

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...
 

Detailed Description

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).

Constructor & Destructor Documentation

◆ VoseAliasSampler()

AIToolbox::VoseAliasSampler::VoseAliasSampler ( const ProbabilityVector p)

Basic constructor.

Parameters
pThe probability distribution to sample from.

Member Function Documentation

◆ sampleProbability()

template<typename G >
size_t AIToolbox::VoseAliasSampler::sampleProbability ( G &  generator) const
inline

This function samples a number that follows the distribution of the class.

Parameters
generatorA random number generator.
Returns
A number between 0 and the size of the original ProbabilityVector.

The documentation for this class was generated from the following file: