TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
probability::geometric_dist::geometric_distribution Class Reference

A class to model the geometric distribution. More...

Public Member Functions

 geometric_distribution (const float &p)
 Constructor for the geometric distribution.
 
float expected_value () const
 The expected value of a geometrically distributed random variable X.
 
float variance () const
 The variance of a geometrically distributed random variable X.
 
float standard_deviation () const
 The standard deviation of a geometrically distributed random variable X.
 
float probability_density (const uint32_t &k) const
 The probability density function.
 
float cumulative_distribution (const uint32_t &k) const
 The cumulative distribution function.
 
float inverse_cumulative_distribution (const float &cdf) const
 The inverse cumulative distribution function.
 
uint32_t draw_sample () const
 Generates a (discrete) sample according to the geometrical distribution.
 
float range_tries (const uint32_t &min_tries=1, const uint32_t &max_tries=std::numeric_limits< uint32_t >::max()) const
 This function computes the probability to have success in a given range of tries.
 

Private Attributes

float p
 The succes probability p.
 

Detailed Description

A class to model the geometric distribution.

Definition at line 55 of file geometric_dist.cpp.

Constructor & Destructor Documentation

◆ geometric_distribution()

probability::geometric_dist::geometric_distribution::geometric_distribution ( const float & p)
inlineexplicit

Constructor for the geometric distribution.

Parameters
pThe success probability

Definition at line 64 of file geometric_dist.cpp.

64: p(p) {}

Member Function Documentation

◆ cumulative_distribution()

float probability::geometric_dist::geometric_distribution::cumulative_distribution ( const uint32_t & k) const
inline

The cumulative distribution function.

The sum of all probabilities up to (and including) k trials. Basically CDF(k) = P(x <= k)

Parameters
kThe number of trials in [1,\infty)
Returns
The probability to have success within k trials

Definition at line 104 of file geometric_dist.cpp.

104 {
105 return 1.0f - std::pow((1.0f - p), static_cast<float>(k));
106 }

◆ draw_sample()

uint32_t probability::geometric_dist::geometric_distribution::draw_sample ( ) const
inline

Generates a (discrete) sample according to the geometrical distribution.

Returns
A geometrically distributed number in [1,\infty)

Definition at line 125 of file geometric_dist.cpp.

125 {
126 float uniform_sample = generate_uniform();
127 return static_cast<uint32_t>(
128 inverse_cumulative_distribution(uniform_sample)) +
129 1;
130 }
float inverse_cumulative_distribution(const float &cdf) const
The inverse cumulative distribution function.
float generate_uniform()
Returns a random number between [0,1].

◆ expected_value()

float probability::geometric_dist::geometric_distribution::expected_value ( ) const
inline

The expected value of a geometrically distributed random variable X.

Returns
E[X] = 1/p

Definition at line 71 of file geometric_dist.cpp.

71{ return 1.0f / p; }

◆ inverse_cumulative_distribution()

float probability::geometric_dist::geometric_distribution::inverse_cumulative_distribution ( const float & cdf) const
inline

The inverse cumulative distribution function.

This functions answers the question: Up to how many trials are needed to have success with a probability of cdf? The exact floating point value is reported.

Parameters
cdfThe probability in [0,1]
Returns
The number of (exact) trials.

Definition at line 116 of file geometric_dist.cpp.

116 {
117 return std::log(1.0f - cdf) / std::log(1.0f - p);
118 }

◆ probability_density()

float probability::geometric_dist::geometric_distribution::probability_density ( const uint32_t & k) const
inline

The probability density function.

As we use the first definition of the geometric series (1), we are doing k - 1 failed trials and the k-th trial is a success.

Parameters
kThe number of trials to observe the first success in [1,\infty)
Returns
A number between [0,1] according to p * (1-p)^{k-1}

Definition at line 93 of file geometric_dist.cpp.

93 {
94 return std::pow((1.0f - p), static_cast<float>(k - 1)) * p;
95 }

◆ range_tries()

float probability::geometric_dist::geometric_distribution::range_tries ( const uint32_t & min_tries = 1,
const uint32_t & max_tries = std::numeric_limits<uint32_t>::max() ) const
inline

This function computes the probability to have success in a given range of tries.

Computes P(min_tries <= x <= max_tries). Can be used to calculate P(x >= min_tries) by not passing a second argument. Can be used to calculate P(x <= max_tries) by passing 1 as the first argument

Parameters
min_triesThe minimum number of tries in [1,\infty) (inclusive)
max_triesThe maximum number of tries in [min_tries, \infty) (inclusive)
Returns
The probability of having success within a range of tries [min_tries, max_tries]

Definition at line 145 of file geometric_dist.cpp.

147 {
148 float cdf_lower = cumulative_distribution(min_tries - 1);
149 float cdf_upper = max_tries == std::numeric_limits<uint32_t>::max()
150 ? 1.0f
151 : cumulative_distribution(max_tries);
152 return cdf_upper - cdf_lower;
153 }
float cumulative_distribution(const uint32_t &k) const
The cumulative distribution function.

◆ standard_deviation()

float probability::geometric_dist::geometric_distribution::standard_deviation ( ) const
inline

The standard deviation of a geometrically distributed random variable X.

Returns
\sigma = \sqrt{V[X]}

Definition at line 84 of file geometric_dist.cpp.

84{ return std::sqrt(variance()); }
float variance() const
The variance of a geometrically distributed random variable X.

◆ variance()

float probability::geometric_dist::geometric_distribution::variance ( ) const
inline

The variance of a geometrically distributed random variable X.

Returns
V[X] = (1 - p) / p^2

Definition at line 77 of file geometric_dist.cpp.

77{ return (1.0f - p) / (p * p); }

Member Data Documentation

◆ p

float probability::geometric_dist::geometric_distribution::p
private

The succes probability p.

Definition at line 57 of file geometric_dist.cpp.


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