# Algorithms_in_C++: probability::geometric_dist::geometric_distribution Class Reference

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. More...
float | expected_value () const
| The expected value of a geometrically distributed random variable X. More...
float | variance () const
| The variance of a geometrically distributed random variable X. More...
float | standard_deviation () const
| The standard deviation of a geometrically distributed random variable X. More...
float | probability_density (const uint32_t &k) const
| The probability density function. More...
float | cumulative_distribution (const uint32_t &k) const
| The cumulative distribution function. More...
float | inverse_cumulative_distribution (const float &cdf) const
| The inverse cumulative distribution function. More...
uint32_t | draw_sample () const
| Generates a (discrete) sample according to the geometrical distribution. More...
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. More...

## Private Attributes

float | p
| The succes probability p.

## Detailed Description

A class to model the geometric distribution.

## Constructor & Destructor Documentation

## geometric_distribution()

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

inlineexplicit

Constructor for the geometric distribution.

Parameters

  p | The success probability

: p(p) {}
probability::geometric_dist::geometric_distribution::p
float p
The succes probability p.
Definition: geometric_dist.cpp:57

## 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

  k | The number of trials in [1,\infty)

Returns
  The probability to have success within k trials

{
return 1.0f - std::pow((1.0f - p), static_cast<float>(k));
}
std::pow
T pow(T... args)
Here is the call graph for this function:

## 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)

{
float uniform_sample = generate_uniform();
return static_cast<uint32_t>(
inverse_cumulative_distribution(uniform_sample)) +
1;
}
probability::geometric_dist::geometric_distribution::inverse_cumulative_distribution
float inverse_cumulative_distribution(const float &cdf) const
The inverse cumulative distribution function.
Definition: geometric_dist.cpp:116
probability::geometric_dist::generate_uniform
float generate_uniform()
Returns a random number between [0,1].
Definition: geometric_dist.cpp:48
Here is the call graph for this function:

## 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

{ 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

  cdf | The probability in [0,1]

Returns
  The number of (exact) trials.

{
return std::log(1.0f - cdf) / std::log(1.0f - p);
}
std::log
T log(T... args)
Here is the call graph for this function:

## 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

  k | The number of trials to observe the first success in [1,\infty)

Returns
  A number between [0,1] according to p * (1-p)^{k-1}

{
return std::pow((1.0f - p), static_cast<float>(k - 1)) * p;
}
Here is the call graph for this function:

## 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_tries | The minimum number of tries in [1,\infty) (inclusive)
  max_tries | The maximum number of tries in [min_tries, \infty) (inclusive)

Returns
  The probability of having success within a range of tries [min_tries, max_tries]

{
float cdf_lower = cumulative_distribution(min_tries - 1);
float cdf_upper = max_tries == std::numeric_limits<uint32_t>::max()
? 1.0f
: cumulative_distribution(max_tries);
return cdf_upper - cdf_lower;
}
probability::geometric_dist::geometric_distribution::cumulative_distribution
float cumulative_distribution(const uint32_t &k) const
The cumulative distribution function.
Definition: geometric_dist.cpp:104
std::numeric_limits::max
T max(T... args)
Here is the call graph for this 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]}

{ return std::sqrt(variance()); }
probability::geometric_dist::geometric_distribution::variance
float variance() const
The variance of a geometrically distributed random variable X.
Definition: geometric_dist.cpp:77
std::sqrt
T sqrt(T... args)
Here is the call graph for this function:

## 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

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

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

- probability/geometric_dist.cpp