-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
poisson_dist.cpp
79 lines (69 loc) · 1.88 KB
/
poisson_dist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @file
* @brief [Poisson
* statistics](https://en.wikipedia.org/wiki/Poisson_distribution)
*
* The Poisson distribution counts how many
* events occur over a set time interval.
*/
#include <cmath>
#include <iostream>
/**
* poisson rate:\n
* calculate the events per unit time\n
* e.g 5 dollars every 2 mins = 5 / 2 = 2.5
*/
double poisson_rate(double events, double timeframe) {
return events / timeframe;
}
/**
* 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; }
/**
* Compute factorial of a given number
*/
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;
}
/**
* Find the probability of x successes in a Poisson dist.
* \f[p(\mu,x) = \frac{\mu^x e^{-\mu}}{x!}\f]
*/
double poisson_x_successes(double expected, double x) {
return (std::pow(expected, x) * std::exp(-expected)) / fact(x);
}
/**
* probability of a success in range for Poisson dist (inclusive, inclusive)
* \f[P = \sum_i p(\mu,i)\f]
*/
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;
}
/**
* main function
*/
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;
std::cout << "Poisson 0 successes : " << poisson_x_successes(expected, 0)
<< std::endl;
std::cout << "Poisson 0-8 successes : "
<< poisson_range_successes(expected, 0, 8) << std::endl;
return 0;
}