TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
poisson_dist.cpp
Go to the documentation of this file.
1
9
#include <cmath>
10
#include <iostream>
11
17
double
poisson_rate
(
double
events,
double
timeframe) {
18
return
events / timeframe;
19
}
20
25
double
poisson_expected
(
double
rate,
double
time) {
return
rate * time; }
26
30
double
fact
(
double
x) {
31
double
x_fact = x;
32
for
(
int
i = x - 1; i > 0; i--) {
33
x_fact *= i;
34
}
35
36
if
(x_fact <= 0) {
37
x_fact = 1;
38
}
39
return
x_fact;
40
}
41
46
double
poisson_x_successes
(
double
expected,
double
x) {
47
return
(std::pow(expected, x) * std::exp(-expected)) /
fact
(x);
48
}
49
54
double
poisson_range_successes
(
double
expected,
double
lower
,
double
upper) {
55
double
probability
= 0;
56
for
(
int
i =
lower
; i <= upper; i++) {
57
probability
+=
poisson_x_successes
(expected, i);
58
}
59
return
probability
;
60
}
61
65
int
main
() {
66
double
rate, expected;
67
rate =
poisson_rate
(3, 1);
68
std::cout <<
"Poisson rate : "
<< rate << std::endl;
69
70
expected =
poisson_expected
(rate, 2);
71
std::cout <<
"Poisson expected : "
<< expected << std::endl;
72
73
std::cout <<
"Poisson 0 successes : "
<<
poisson_x_successes
(expected, 0)
74
<< std::endl;
75
std::cout <<
"Poisson 0-8 successes : "
76
<<
poisson_range_successes
(expected, 0, 8) << std::endl;
77
78
return
0;
79
}
probability
Probability algorithms.
fact
double fact(double x)
Definition
poisson_dist.cpp:30
poisson_x_successes
double poisson_x_successes(double expected, double x)
Definition
poisson_dist.cpp:46
poisson_rate
double poisson_rate(double events, double timeframe)
Definition
poisson_dist.cpp:17
poisson_expected
double poisson_expected(double rate, double time)
Definition
poisson_dist.cpp:25
poisson_range_successes
double poisson_range_successes(double expected, double lower, double upper)
Definition
poisson_dist.cpp:54
main
int main()
Definition
poisson_dist.cpp:65
lower
std::string lower(std::string word)
function to convert a C++ string to lower case
Definition
text_search.cpp:19
probability
poisson_dist.cpp
Generated by
1.12.0