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
17double poisson_rate(double events, double timeframe) {
18 return events / timeframe;
19}
20
25double poisson_expected(double rate, double time) { return rate * time; }
26
30double 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
46double poisson_x_successes(double expected, double x) {
47 return (std::pow(expected, x) * std::exp(-expected)) / fact(x);
48}
49
54double 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
65int 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 algorithms.
double fact(double x)
double poisson_x_successes(double expected, double x)
double poisson_rate(double events, double timeframe)
double poisson_expected(double rate, double time)
double poisson_range_successes(double expected, double lower, double upper)
int main()
std::string lower(std::string word)
function to convert a C++ string to lower case