2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief [Poisson
|
|
|
|
* statistics](https://en.wikipedia.org/wiki/Poisson_distribution)
|
|
|
|
*
|
|
|
|
* The Poisson distribution counts how many
|
|
|
|
* events occur over a set time interval.
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
#include <cmath>
|
2020-05-29 05:14:39 +08:00
|
|
|
#include <iostream>
|
2020-05-22 04:48:44 +08:00
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* poisson rate:\n
|
|
|
|
* calculate the events per unit time\n
|
|
|
|
* e.g 5 dollars every 2 mins = 5 / 2 = 2.5
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
double poisson_rate(double events, double timeframe) {
|
|
|
|
return events / timeframe;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* calculate the expected value over a time
|
|
|
|
* e.g rate of 2.5 over 10 mins = 2.5 x 10 = 25
|
|
|
|
*/
|
|
|
|
double poisson_expected(double rate, double time) { return rate * time; }
|
2020-05-22 04:48:44 +08:00
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* Compute factorial of a given number
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
double fact(double x) {
|
|
|
|
double x_fact = x;
|
|
|
|
for (int i = x - 1; i > 0; i--) {
|
|
|
|
x_fact *= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x_fact <= 0) {
|
|
|
|
x_fact = 1;
|
|
|
|
}
|
|
|
|
return x_fact;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* Find the probability of x successes in a Poisson dist.
|
|
|
|
* \f[p(\mu,x) = \frac{\mu^x e^{-\mu}}{x!}\f]
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
double poisson_x_successes(double expected, double x) {
|
2020-05-29 05:14:39 +08:00
|
|
|
return (std::pow(expected, x) * std::exp(-expected)) / fact(x);
|
2020-05-22 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* probability of a success in range for Poisson dist (inclusive, inclusive)
|
|
|
|
* \f[P = \sum_i p(\mu,i)\f]
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
double poisson_range_successes(double expected, double lower, double upper) {
|
|
|
|
double probability = 0;
|
|
|
|
for (int i = lower; i <= upper; i++) {
|
|
|
|
probability += poisson_x_successes(expected, i);
|
|
|
|
}
|
|
|
|
return probability;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
/**
|
|
|
|
* main function
|
|
|
|
*/
|
2020-05-22 04:48:44 +08:00
|
|
|
int main() {
|
|
|
|
double rate, expected;
|
|
|
|
rate = poisson_rate(3, 1);
|
|
|
|
std::cout << "Poisson rate : " << rate << std::endl;
|
|
|
|
|
|
|
|
expected = poisson_expected(rate, 2);
|
|
|
|
std::cout << "Poisson expected : " << expected << std::endl;
|
|
|
|
|
2020-05-29 05:14:39 +08:00
|
|
|
std::cout << "Poisson 0 successes : " << poisson_x_successes(expected, 0)
|
|
|
|
<< std::endl;
|
2020-05-22 04:48:44 +08:00
|
|
|
std::cout << "Poisson 0-8 successes : "
|
2020-05-29 05:14:39 +08:00
|
|
|
<< poisson_range_successes(expected, 0, 8) << std::endl;
|
2020-05-22 04:48:44 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|