This file defines the basic pseudorandom number generation functionality used by the core library, as well as functions to sample from a number of built-in distributions such as categorical, discrete uniform, continuous uniform, Bernoulli, geometric, beta, gamma, and Dirichlet.
Classes, functions, and variables in this file | |
---|---|
unsigned int | get_seed () |
void | set_seed (unsigned int new_seed) |
bool | read_random_state (Stream & in) |
bool | write_random_state (Stream & out) |
unsigned int | sample_categorical (V * probability, unsigned int length) |
unsigned int | select_categorical (const U * probability, V random, unsigned int length) |
unsigned int | sample_categorical (const V * probability, V sum, unsigned int length) |
unsigned int | sample_categorical (const unsigned int * probability, unsigned int sum, unsigned int length) |
unsigned int | sample_uniform (unsigned int n) |
const T & | sample_uniform (const T * elements, unsigned int length) |
const T & | sample_uniform (const T (&elements)[N]) |
const T & | sample_uniform (const array< T > & elements) |
V | sample_uniform () |
bool | sample_bernoulli (const V & p) |
unsigned int | sample_geometric (const V & p) |
double | log_probability_negative_binomial (unsigned int x, const V & r, const V & p) |
V | sample_beta (const V & alpha) |
V | sample_beta (const V & alpha, const V & beta) |
V | sample_gamma (const V & alpha, const V & beta) |
V | log_probability_gamma (const V & x, const V & alpha, const V & beta) |
void | sample_dirichlet (V * dst, const V * alpha, unsigned int length) |
Returns the initial random seed used by all core functions that require pseudorandom number generation.
unsigned int | new_seed | ) |
Sets the seed of the underlying pseudorandom number generator.
Reads the state of the pseudorandom number generator from in
. This is useful to persist the state of the pseudorandom number generator.
Writes the state of the pseudorandom number generator to out
. This is useful to persist the state of the pseudorandom number generator.
Samples from a categorical distribution, where the unnormalized probability of returning the index i
is given by probability[i]
. This function normalizes and overwrites probability
with its cumulative distribution function.
const U * | probability, | |
V | random, | |
unsigned int | length | ) |
Returns the smallest index i
such that random < sum from j=0 to i-1 of probability[j]
. Thus, this function implements the inverse cumulative distribution function for the categorical distribution with unnormalized probabilities given by probability
.
const V * | probability, | |
V | sum, | |
unsigned int | length | ) |
Samples from a categorical distribution, where the unnormalized probability of returning the index i
is given by probability[i]
and its sum is given by the floating-point sum
. This function doesn't modify probability
.
const unsigned int * | probability, | |
unsigned int | sum, | |
unsigned int | length | ) |
Samples from a categorical distribution, where the unnormalized probability of returning the index i
is given by probability[i]
and its sum is given by the unsigned integer sum
. This function doesn't modify probability
.
unsigned int | n | ) |
Returns a sample from the discrete uniform distribution over {0, ..., n - 1}
.
Returns a sample from the discrete uniform distribution over elements
.
Returns a sample from the discrete uniform distribution over elements
.
Returns a sample from the discrete uniform distribution over elements
.
Returns a sample from the continuous uniform distribution over [0, 1].
Returns a sample from the Bernoulli distribution: with probability 0.5, true
is returned, otherwise false
is returned.
Returns a sample from the geometric distribution with success probability p
.
unsigned int | x, | |
const V & | r, | |
const V & | p | ) |
Returns a sample from the negative binomial distribution with number of failures r
and success probability p
.
Returns a sample from the beta distribution with shape parameter alpha
and scale parameter 1. This function assumes alpha > 0
.
Returns a sample from the beta distribution with shape parameter alpha
and scale parameter beta
. This function assumes alpha > 0
and beta > 0
.
Returns a sample from the gamma distribution with shape parameter alpha
and rate parameter beta
. This function assumes alpha > 0
and beta > 0
.
Returns the log probability of drawing the observation x
from a gamma distribution with shape parameter alpha
and rate parameter beta
. This function assumes x > 0
, alpha > 0
, and beta > 0
.
Samples from the Dirichlet distribution with parameter alpha
and dimension length
. The sample is written to dst
. This function assumes dst
has size at least length
, alpha[i] > 0
for all i = 0, ..., length - 1
.