RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
randlib::BinomialDistribution< IntType > Class Template Reference

The BinomialDistribution class
Abstract class for Binomial distribution. More...

#include <binomial.hpp>

Inheritance diagram for randlib::BinomialDistribution< IntType >:
Collaboration diagram for randlib::BinomialDistribution< IntType >:

Public Member Functions

Support SupportType () const override
 
IntType MinValue () const override
 
IntType MaxValue () const override
 
IntType GetNumber () const noexcept
 
double GetProbability () const noexcept
 
double SufficientStatistic (IntType x) const override
 
double SourceParameters () const override
 
double SourceToNatural (double sourceParameters) const override
 
double NaturalParameters () const override
 
double LogNormalizer (double theta) const override
 
double LogNormalizerGradient (double theta) const override
 
double CarrierMeasure (IntType x) const override
 
double EntropyAdjusted () const override
 
double P (const IntType &k) const override
 
double logP (const IntType &k) const override
 
double F (const IntType &k) const override
 
double S (const IntType &k) const override
 
template<std::uniform_random_bit_generator G>
IntType operator() (G &g) const
 
long double Mean () const override
 
long double Variance () const override
 
IntType Median () const override
 
IntType Mode () const override
 Mode is the value, which has the largest probability to happen, or, in the case of continuous distribution, has the largest value of density function.
 
long double Skewness () const override
 
long double ExcessKurtosis () const override
 
double GetLogFactorialN () const noexcept
 
double GetLogProbability () const noexcept
 
double GetLog1mProbability () const noexcept
 
void FitProbability (std::span< const IntType > sample)
 Fit probability p with maximum-likelihood estimation.
 
BetaRand FitProbabilityBayes (std::span< const IntType > sample, const BetaDistribution<> &priorDistribution, bool MAP=false)
 Fit probability p with prior assumption p ~ Beta(α, β)
 
BetaRand FitProbabilityMinimax (std::span< const IntType > sample)
 Fit probability p with minimax estimator.
 
- Public Member Functions inherited from randlib::DiscreteDistribution< IntType >
virtual ~DiscreteDistribution ()
 
void ProbabilityMassFunction (std::span< const IntType > x, std::span< double > y) const
 fill vector y with P(x)
 
void LogProbabilityMassFunction (std::span< const IntType > x, std::span< double > y) const
 fill vector y with logP(x)
 
IntType Mode () const override
 Mode is the value, which has the largest probability to happen, or, in the case of continuous distribution, has the largest value of density function.
 
double Hazard (const IntType &x) const override
 
double LikelihoodFunction (std::span< const IntType > sample) const override
 
double LogLikelihoodFunction (std::span< const IntType > sample) const override
 
bool PearsonChiSquaredTest (std::span< const IntType > orderStatistic, double alpha, int lowerBoundary, int upperBoundary, size_t numberOfEstimatedParameters=0) const
 
bool PearsonChiSquaredTest (std::span< const IntType > orderStatistic, double alpha, size_t numberOfEstimatedParameters=0) const
 
- Public Member Functions inherited from randlib::UnivariateDistribution< IntType >
virtual ~UnivariateDistribution ()
 
bool isLeftBounded () const
 
bool isRightBounded () const
 
IntType Quantile (double p) const
 
IntType Quantile1m (double p) const
 
void QuantileFunction (std::span< const double > p, std::span< IntType > y) const
 
void QuantileFunction1m (std::span< const double > p, std::span< IntType > y) const
 
std::complex< double > CF (double t) const
 
void CharacteristicFunction (std::span< const double > t, std::span< std::complex< double > > y) const
 
void HazardFunction (std::span< const IntType > x, std::span< double > y) const
 
virtual long double Kurtosis () const
 
virtual long double SecondMoment () const
 
virtual long double ThirdMoment () const
 
virtual long double FourthMoment () const
 
- Public Member Functions inherited from randlib::ProbabilityDistribution< T >
virtual ~ProbabilityDistribution ()=default
 
virtual std::string Name () const =0
 
virtual double F (const T &x) const =0
 
void CumulativeDistributionFunction (std::span< const T > x, std::span< double > y) const
 
virtual double S (const T &x) const
 
void SurvivalFunction (std::span< const T > x, std::span< double > y) const
 
template<std::uniform_random_bit_generator G>
T operator() (G &g) const
 Draws a variate with the given engine through the virtual interface.
 
T Variate () const
 Uses the engine of this object, so it is not thread-safe for one object; use operator() with one engine per thread instead.
 
void Sample (std::span< T > outputData) const
 Fills the output with variates drawn with the engine of this object.
 
template<std::uniform_random_bit_generator G>
void Sample (G &g, std::span< T > outputData) const
 Fills the output with variates drawn with the given engine.
 
void Reseed (std::uint64_t seed)
 
- Public Member Functions inherited from randlib::ExponentialFamily< IntType, double >
 ExponentialFamily ()
 
virtual ~ExponentialFamily ()
 
double ProbabilityMeasure (IntType x) const
 
double LogProbabilityMeasure (IntType x) const
 
double KullbackLeiblerDivergence (double parameters) const
 
virtual double CrossEntropyAdjusted (double parameters) const
 

Static Public Member Functions

template<std::uniform_random_bit_generator G>
static IntType Variate (IntType number, double probability, G &g)
 
- Static Public Member Functions inherited from randlib::UnivariateDistribution< IntType >
static long double GetSampleSum (std::span< const IntType > sample)
 
static long double GetSampleMean (std::span< const IntType > sample)
 
static long double GetSampleLogMean (std::span< const IntType > sample)
 
static long double GetSampleVariance (std::span< const IntType > sample, double mean)
 
static long double GetSampleLogVariance (std::span< const IntType > sample, double logMean)
 
static LongDoublePair GetSampleMeanAndVariance (std::span< const IntType > sample)
 
static LongDoublePair GetSampleLogMeanAndVariance (std::span< const IntType > sample)
 
static std::tuple< long double, long double, long double, long double > GetSampleStatistics (std::span< const IntType > sample)
 

Protected Member Functions

 BinomialDistribution (IntType number, double probability)
 
void SetParameters (IntType number, double probability)
 
- Protected Member Functions inherited from randlib::DiscreteDistribution< IntType >
 DiscreteDistribution ()
 
IntType quantileImpl (double p, IntType initValue) const override
 
IntType quantileImpl (double p) const override
 
IntType quantileImpl1m (double p, IntType initValue) const override
 
IntType quantileImpl1m (double p) const override
 
long double ExpectedValue (const std::function< double(IntType)> &funPtr, IntType minPoint, IntType maxPoint) const override
 
std::complex< double > CFImpl (double t) const override
 
- Protected Member Functions inherited from randlib::UnivariateDistribution< IntType >
 UnivariateDistribution ()
 
- Protected Member Functions inherited from randlib::ProbabilityDistribution< T >
std::string toStringWithPrecision (const double a_value, const int n=6) const
 
 ProbabilityDistribution ()=default
 
DefaultEngine & engine () const
 
virtual void sample (detail::EngineRef g, std::span< T > outputData) const
 Fills the output with variate(g); distributions with a faster bulk algorithm override it.
 
fit_error fitError (FitErrorCode code, const std::string &explanation) const
 

Protected Attributes

double p = 0.5
 probability of success
 
double q = 0.5
 probability of failure
 
double logProb = -randlib::consts::ln2
 log(p)
 
double log1mProb = -randlib::consts::ln2
 log(q)
 

Additional Inherited Members

- Public Types inherited from randlib::ProbabilityDistribution< T >
using value_type = T
 
- Static Protected Member Functions inherited from randlib::UnivariateDistribution< IntType >
static bool allElementsAreNotGreaterThan (IntType value, std::span< const IntType > sample)
 
static bool allElementsAreNotSmallerThan (IntType value, std::span< const IntType > sample)
 
static bool allElementsAreNonNegative (std::span< const IntType > sample)
 
static bool allElementsArePositive (std::span< const IntType > sample)
 
- Static Protected Attributes inherited from randlib::ProbabilityDistribution< T >
static constexpr size_t MAX_ITER_REJECTION = detail::MAX_ITER_REJECTION
 MAX_ITER_REJECTION upper boundary for maximum amount of iterations in rejection methods one thousand should be enough to be sure there is a bug (or rejection method is too slow to be used)
 
static constexpr char POSITIVITY_VIOLATION [] = "All elements should be positive"
 
static constexpr char NON_NEGATIVITY_VIOLATION [] = "All elements should be non-negative"
 
static constexpr char UPPER_LIMIT_VIOLATION [] = "No element should be greater than "
 
static constexpr char LOWER_LIMIT_VIOLATION [] = "No element should be smaller than "
 

Detailed Description

template<typename IntType>
class randlib::BinomialDistribution< IntType >

The BinomialDistribution class
Abstract class for Binomial distribution.

Notation: X ~ Bin(n, p)

Related distributions:
If X ~ Bin(1, p), then X ~ Bernoulli(p)
X ~ Multin(n, 1 - p, p)

Constructor & Destructor Documentation

◆ BinomialDistribution()

template<typename IntType >
randlib::BinomialDistribution< IntType >::BinomialDistribution ( IntType  number,
double  probability 
)
protected

Member Function Documentation

◆ CarrierMeasure()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::CarrierMeasure ( IntType  x) const
overridevirtual

◆ EntropyAdjusted()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::EntropyAdjusted ( ) const
overridevirtual

◆ ExcessKurtosis()

template<typename IntType >
long double randlib::BinomialDistribution< IntType >::ExcessKurtosis ( ) const
overridevirtual
Returns
E[((X - μ) / σ) ^ 4] - 3 (fourth moment around the mean divided by the square of the variance of the probability distribution minus 3)

var is finite, so is mu

Reimplemented from randlib::UnivariateDistribution< IntType >.

◆ F()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::F ( const IntType &  k) const
override

log B(n - k, k + 1) without the cancellation between log-factorials of n

◆ FitProbability()

template<typename IntType >
void randlib::BinomialDistribution< IntType >::FitProbability ( std::span< const IntType >  sample)

Fit probability p with maximum-likelihood estimation.

Parameters
sample

◆ FitProbabilityBayes()

template<typename IntType >
BetaRand randlib::BinomialDistribution< IntType >::FitProbabilityBayes ( std::span< const IntType >  sample,
const BetaDistribution<> &  priorDistribution,
bool  MAP = false 
)

Fit probability p with prior assumption p ~ Beta(α, β)

Parameters
sample
priorDistribution
MAPif true, use MAP estimator
Returns
posterior distribution

◆ FitProbabilityMinimax()

template<typename IntType >
BetaRand randlib::BinomialDistribution< IntType >::FitProbabilityMinimax ( std::span< const IntType >  sample)

Fit probability p with minimax estimator.

Parameters
sample
Returns
posterior distribution

◆ GetLog1mProbability()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::GetLog1mProbability ( ) const
inlinenoexcept
Returns
log(1-p)

◆ GetLogFactorialN()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::GetLogFactorialN ( ) const
inlinenoexcept
Returns
log(n!)

◆ GetLogProbability()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::GetLogProbability ( ) const
inlinenoexcept
Returns
log(p)

◆ GetNumber()

template<typename IntType >
IntType randlib::BinomialDistribution< IntType >::GetNumber ( ) const
inlinenoexcept

◆ GetProbability()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::GetProbability ( ) const
inlinenoexcept

◆ LogNormalizer()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::LogNormalizer ( double  theta) const
overridevirtual

◆ LogNormalizerGradient()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::LogNormalizerGradient ( double  theta) const
overridevirtual

◆ logP()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::logP ( const IntType &  x) const
overridevirtual
Parameters
x
Returns
logarithm of probability to get x

saddle-point form; the direct sum of logarithms cancels catastrophically for large n

Implements randlib::DiscreteDistribution< IntType >.

◆ MaxValue()

template<typename IntType >
IntType randlib::BinomialDistribution< IntType >::MaxValue ( ) const
inlineoverridevirtual
Returns
maximum possible value that can be achieved by random variable

Implements randlib::ProbabilityDistribution< T >.

◆ Mean()

template<typename IntType >
long double randlib::BinomialDistribution< IntType >::Mean ( ) const
overridevirtual
Returns
Mathematical expectation

Implements randlib::UnivariateDistribution< IntType >.

◆ Median()

template<typename IntType >
IntType randlib::BinomialDistribution< IntType >::Median ( ) const
overridevirtual
Returns
such x that F(x) = 0.5

The median is floor(np) or ceil(np)

Reimplemented from randlib::UnivariateDistribution< IntType >.

◆ MinValue()

template<typename IntType >
IntType randlib::BinomialDistribution< IntType >::MinValue ( ) const
inlineoverridevirtual
Returns
minimum possible value that can be achieved by random variable

Implements randlib::ProbabilityDistribution< T >.

◆ Mode()

template<typename IntType >
IntType randlib::BinomialDistribution< IntType >::Mode ( ) const
overridevirtual

Mode is the value, which has the largest probability to happen, or, in the case of continuous distribution, has the largest value of density function.

Returns
mode. In case, when it's not unique, return any of them.

Implements randlib::UnivariateDistribution< IntType >.

◆ NaturalParameters()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::NaturalParameters ( ) const
overridevirtual

◆ operator()()

template<typename IntType >
template<std::uniform_random_bit_generator G>
IntType randlib::BinomialDistribution< IntType >::operator() ( G &  g) const

if X ~ Bin(n, p') and Y ~ Bin(n - X, (p - p') / (1 - p')) then Z = X + Y ~ Bin(n, p)

◆ P()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::P ( const IntType &  k) const
overridevirtual
Parameters
k
Returns
probability to get k

Reimplemented from randlib::DiscreteDistribution< IntType >.

◆ S()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::S ( const IntType &  k) const
override

◆ SetParameters()

template<typename IntType >
void randlib::BinomialDistribution< IntType >::SetParameters ( IntType  number,
double  probability 
)
protected

◆ Skewness()

template<typename IntType >
long double randlib::BinomialDistribution< IntType >::Skewness ( ) const
overridevirtual
Returns
E[((X - μ) / σ) ^ 3] where μ is central moment and σ is standard deviation

var is finite, so is mu

Reimplemented from randlib::UnivariateDistribution< IntType >.

◆ SourceParameters()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::SourceParameters ( ) const
overridevirtual

◆ SourceToNatural()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::SourceToNatural ( double  sourceParameters) const
overridevirtual

◆ SufficientStatistic()

template<typename IntType >
double randlib::BinomialDistribution< IntType >::SufficientStatistic ( IntType  x) const
overridevirtual

◆ SupportType()

template<typename IntType >
Support randlib::BinomialDistribution< IntType >::SupportType ( ) const
inlineoverridevirtual
Returns
type of support

Implements randlib::UnivariateDistribution< IntType >.

◆ Variance()

template<typename IntType >
long double randlib::BinomialDistribution< IntType >::Variance ( ) const
overridevirtual
Returns
Variance of random variable

Implements randlib::UnivariateDistribution< IntType >.

◆ Variate()

template<typename IntType >
template<std::uniform_random_bit_generator G>
IntType randlib::BinomialDistribution< IntType >::Variate ( IntType  number,
double  probability,
G &  g 
)
static

sanity check

Member Data Documentation

◆ log1mProb

template<typename IntType >
double randlib::BinomialDistribution< IntType >::log1mProb = -randlib::consts::ln2
protected

log(q)

◆ logProb

template<typename IntType >
double randlib::BinomialDistribution< IntType >::logProb = -randlib::consts::ln2
protected

log(p)

◆ p

template<typename IntType >
double randlib::BinomialDistribution< IntType >::p = 0.5
protected

probability of success

◆ q

template<typename IntType >
double randlib::BinomialDistribution< IntType >::q = 0.5
protected

probability of failure


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