Discrete Random Variables
Point Mass Distribution
Definition
has a point mass distribution at , written , if in which case
The probability mass function is for , and 0 otherwise.
Discrete Uniform Distribution
https://en.wikipedia.org/wiki/Discrete_uniform_distribution
Definition
Let be a given integer. Suppose has a probability mass function given by
We then say that has a uniform distribution on .
Bernoulli Distribution
https://en.wikipedia.org/wiki/Bernoulli_distribution
Definition
Let represent a binary coin flip. Then and for some . We say that has a Bernoulli distribution written . The probability mass function is given by
for .
Generating Bernoulli Samples
template <typename T>
auto bernoulli(T p, Generator gen) -> T {
const auto u = uniform_real_sample(0, 1, gen);
return static_cast<T>(u < p);
}
Binomial Distribution
https://en.wikipedia.org/wiki/Binomial_distribution
Definition
The probability of getting exactly successes in independent Bernoulli trials (with the same rate ) is given by the probability mass function
for . A random variable with this mass function is called a Binomial random variable, and we write .
Theorem
If and , then .
Generating Binomial Samples
template <typename T>
auto binomial(T p, T n, Generator gen) -> T {
T sum{};
for (auto _ : std::views::iota(0, n)) {
const auto u = uniform_real_sample(0, 1, gen);
sum += static_cast<T>(u < p);
}
}
Geometric Distribution
https://en.wikipedia.org/wiki/Geometric_distribution
Definition
The Geometric distribution is either one of the two discrete probability distributions:
- The probability distribution of the number of Bernoulli trials needed to get one success, supported on
- The probability distribution of the number of failures before the first success, supported on .
If the probability of success on each trial is , then the probability that the -th trial is the first success is
for . The above form of the geometric distribution is used for modeling the number of trials up to and including the first success. By contrast, the following form of the geometric distribution is used for modeling the number of failures until the first success
for .
Generating Geometric Samples
template <typename T>
auto geometric(T p, Generator gen) -> T {
const auto u = uniform_real_sample(0, 1, gen);
return p == 1 ? 1 : std::ceil(std::log1p(-u) / std::log1p(-p));
}
Poisson Distribution
https://en.wikipedia.org/wiki/Poisson_distribution
Definition
A discrete random variable is said to have a Poisson distribution with parameter , denoted as , if it has the probability mass function given by
where is the number of occurrences. The Poisson distribution is often used as a model for the counts of rare events.
Theorem
If and , then .
Generating Poisson Samples
Devroye, Luc, and Luc Devroye. “Discrete univariate distributions.” Non-Uniform Random Variate Generation (1986): 485-553.
template <typename T>
auto poisson(T lambda, Generator gen) -> T {
T x{};
auto p = std::exp(-lambda);
auto s = p;
const auto u = uniform_real_sample(static_cast<T>(0), static_cast<T>(1), gen);
while (u > s) {
++x;
p *= lambda / x;
s += p;
}
return x;
}