Distribution.hpp

A set of pre-calculated discrete distributions that can quickly generate random values.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2018-2024 Michigan State University MIT Software license; see doc/LICENSE.md

A Distribution is a pre-calculated set of probabilities to quickly pick a whole-number result. These should be used when either we need to draw from the same distribution many time (and hence the extra time to pre-calculate it is amortized away) -or- in functions that we want to call with a range of distributions that we may not know ahead of time.

Currently, we have:

Uniform - All values in a range are equally likely to be picked. Binomial - How many successes with p probability will occur in N attempts? NegativeBinomial - How many attempts to reach N successes, with p probability per attempt?

Developer Notes:

  • We should setup an offset in the base Distribution class to ignore “impossible” low values.

Note

Status: BETA

Defines

INCLUDE_EMP_MATH_DISTRIBUTION_HPP_GUARD
class Distribution
#include <Distribution.hpp>

Subclassed by Binomial, GeometricDistribution, NegativeBinomial, Uniform

Public Functions

inline size_t GetSize() const
inline double GetTotalProb() const
inline double operator[](size_t id) const
inline size_t PickPosition(double in_value)

Pick an item from a distribution using a value between 0.0 and 1.0.

inline size_t PickRandom(Random &random) const

Pick a random item using this distribution.

Protected Attributes

UnorderedIndexMap weights
class Uniform : public Distribution
#include <Distribution.hpp>

Public Functions

inline Uniform(size_t _min, size_t _max)
inline size_t GetMin() const
inline size_t GetMax() const
inline void Setup(size_t _min, size_t _max)

Private Members

size_t min_val = 0
size_t max_val = 0
class GeometricDistribution : public Distribution
#include <Distribution.hpp>

How many attempts before a probability p succeeds?

Public Functions

inline GeometricDistribution()
inline GeometricDistribution(double _p, double _precision = 0.0000000001, size_t max_size = 1000000)
inline double GetP() const
inline bool Setup(double _p, const double _precision, const size_t max_size)

Private Members

double p = 0.0
double precision = 0.0
class Binomial : public Distribution
#include <Distribution.hpp>

How many successes with p probability and N attempts?

Public Functions

Binomial() = default
Binomial(const Binomial&) = default
Binomial(Binomial&&) = default
inline Binomial(double _p, size_t _N)
inline double GetP() const
inline double GetN() const
inline void Setup(double _p, size_t _N)

Private Members

double p = 0.0
size_t N = 0
class NegativeBinomial : public Distribution
#include <Distribution.hpp>

How many attempts to reach N successes, assuming p probability per attempt?

Public Functions

inline NegativeBinomial()
inline NegativeBinomial(double _p, size_t _N = 1)
inline double GetP() const
inline double GetN() const
inline void Setup(double _p, size_t _N = 1)

Private Members

double p = 0.0
size_t N = 0