Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
geometric_dist.cpp File Reference

Geometric Distribution More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <limits>
#include <random>
#include <vector>
Include dependency graph for geometric_dist.cpp:

Classes

class  probability::geometric_dist::geometric_distribution
 A class to model the geometric distribution. More...
 

Namespaces

namespace  probability
 Probability algorithms.
 
namespace  geometric_dist
 Functions for the Geometric Distribution algorithm implementation.
 

Functions

float probability::geometric_dist::generate_uniform ()
 Returns a random number between [0,1]. More...
 
void sample_test (const probability::geometric_dist::geometric_distribution &dist)
 Tests the sampling method of the geometric distribution. More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Geometric Distribution

The geometric distribution models the experiment of doing Bernoulli trials until a sucess was observed. There are two formulations of the geometric distribution: 1) The probability distribution of the number X of Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, ... } 2) The probability distribution of the number Y = X − 1 of failures before the first success, supported on the set { 0, 1, 2, 3, ... } Here, the first one is implemented.

Common variables used: p - The success probability k - The number of tries

Author
Domenic Zingsheim

Function Documentation

◆ generate_uniform()

float probability::geometric_dist::generate_uniform ( )

Returns a random number between [0,1].

Returns
A uniformly distributed random number between 0 (included) and 1 (included)
42 {
43 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
44}
T rand(T... args)
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
232 {
233 srand(time(nullptr));
234 test(); // run self-test implementations
235 return 0;
236}
static void test()
Self-test implementations.
Definition: geometric_dist.cpp:179
T srand(T... args)
T time(T... args)
Here is the call graph for this function:

◆ sample_test()

void sample_test ( const probability::geometric_dist::geometric_distribution dist)

Tests the sampling method of the geometric distribution.

Draws 1000000 random samples and estimates mean and variance These should be close to the expected value and variance of the given distribution to pass.

Parameters
distThe distribution to test
150 {
151 uint32_t n_tries = 1000000;
152 std::vector<float> tries;
153 tries.resize(n_tries);
154
155 float mean = 0.0f;
156 for (uint32_t i = 0; i < n_tries; ++i) {
157 tries[i] = static_cast<float>(dist.draw_sample());
158 mean += tries[i];
159 }
160
161 mean /= static_cast<float>(n_tries);
162
163 float var = 0.0f;
164 for (uint32_t i = 0; i < n_tries; ++i) {
165 var += (tries[i] - mean) * (tries[i] - mean);
166 }
167
168 //Unbiased estimate of variance
169 var /= static_cast<float>(n_tries - 1);
170
171 std::cout << "This value should be near " << dist.expected_value() << ": " << mean << std::endl;
172 std::cout << "This value should be near " << dist.variance() << ": " << var << std::endl;
173}
float expected_value() const
The expected value of a geometrically distributed random variable X.
Definition: geometric_dist.cpp:65
uint32_t draw_sample() const
Generates a (discrete) sample according to the geometrical distribution.
Definition: geometric_dist.cpp:121
float variance() const
The variance of a geometrically distributed random variable X.
Definition: geometric_dist.cpp:73
T endl(T... args)
T resize(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
179 {
181
182 const float threshold = 1e-3f;
183
184 std::cout << "Starting tests for p = 0.3..." << std::endl;
185 assert(std::abs(dist.expected_value() - 3.33333333f) < threshold);
186 assert(std::abs(dist.variance() - 7.77777777f) < threshold);
187 assert(std::abs(dist.standard_deviation() - 2.788866755) < threshold);
188 assert(std::abs(dist.probability_density(5) - 0.07203) < threshold);
189 assert(std::abs(dist.cumulative_distribution(6) - 0.882351) < threshold);
190 assert(std::abs(dist.inverse_cumulative_distribution(dist.cumulative_distribution(8)) - 8) < threshold);
191 assert(std::abs(dist.range_tries() - 1.0f) < threshold);
192 assert(std::abs(dist.range_tries(3) - 0.49f) < threshold);
193 assert(std::abs(dist.range_tries(5, 11) - 0.2203267f) < threshold);
194 std::cout << "All tests passed" << std::endl;
195 sample_test(dist);
196
198
199 std::cout << "Starting tests for p = 0.5..." << std::endl;
200 assert(std::abs(dist.expected_value() - 2.0f) < threshold);
201 assert(std::abs(dist.variance() - 2.0f) < threshold);
202 assert(std::abs(dist.standard_deviation() - 1.4142135f) < threshold);
203 assert(std::abs(dist.probability_density(5) - 0.03125) < threshold);
204 assert(std::abs(dist.cumulative_distribution(6) - 0.984375) < threshold);
205 assert(std::abs(dist.inverse_cumulative_distribution(dist.cumulative_distribution(8)) - 8) < threshold);
206 assert(std::abs(dist.range_tries() - 1.0f) < threshold);
207 assert(std::abs(dist.range_tries(3) - 0.25f) < threshold);
208 assert(std::abs(dist.range_tries(5, 11) - 0.062011f) < threshold);
209 std::cout << "All tests passed" << std::endl;
210 sample_test(dist);
211
213
214 std::cout << "Starting tests for p = 0.8..." << std::endl;
215 assert(std::abs(dist.expected_value() - 1.25f) < threshold);
216 assert(std::abs(dist.variance() - 0.3125f) < threshold);
217 assert(std::abs(dist.standard_deviation() - 0.559016f) < threshold);
218 assert(std::abs(dist.probability_density(5) - 0.00128) < threshold);
219 assert(std::abs(dist.cumulative_distribution(6) - 0.999936) < threshold);
220 assert(std::abs(dist.inverse_cumulative_distribution(dist.cumulative_distribution(8)) - 8) < threshold);
221 assert(std::abs(dist.range_tries() - 1.0f) < threshold);
222 assert(std::abs(dist.range_tries(3) - 0.04f) < threshold);
223 assert(std::abs(dist.range_tries(5, 11) - 0.00159997f) < threshold);
224 std::cout << "All tests have successfully passed!" << std::endl;
225 sample_test(dist);
226}
A class to model the geometric distribution.
Definition: geometric_dist.cpp:50
void sample_test(const probability::geometric_dist::geometric_distribution &dist)
Tests the sampling method of the geometric distribution.
Definition: geometric_dist.cpp:150
Here is the call graph for this function: