TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
poisson_dist.cpp File Reference

Poisson statistics More...

#include <cmath>
#include <iostream>
Include dependency graph for poisson_dist.cpp:

Go to the source code of this file.

Functions

double poisson_rate (double events, double timeframe)
 
double poisson_expected (double rate, double time)
 
double fact (double x)
 
double poisson_x_successes (double expected, double x)
 
double poisson_range_successes (double expected, double lower, double upper)
 
int main ()
 

Detailed Description

Poisson statistics

The Poisson distribution counts how many events occur over a set time interval.

Definition in file poisson_dist.cpp.

Function Documentation

◆ fact()

double fact ( double x)

Compute factorial of a given number

Definition at line 30 of file poisson_dist.cpp.

30 {
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}

◆ main()

int main ( void )

main function

Definition at line 65 of file poisson_dist.cpp.

65 {
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}
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)

◆ poisson_expected()

double poisson_expected ( double rate,
double time )

calculate the expected value over a time e.g rate of 2.5 over 10 mins = 2.5 x 10 = 25

Definition at line 25 of file poisson_dist.cpp.

25{ return rate * time; }

◆ poisson_range_successes()

double poisson_range_successes ( double expected,
double lower,
double upper )

probability of a success in range for Poisson dist (inclusive, inclusive)

\[P = \sum_i p(\mu,i)\]

Definition at line 54 of file poisson_dist.cpp.

54 {
55 double probability = 0;
56 for (int i = lower; i <= upper; i++) {
57 probability += poisson_x_successes(expected, i);
58 }
59 return probability;
60}
Probability algorithms.
std::string lower(std::string word)
function to convert a C++ string to lower case

◆ poisson_rate()

double poisson_rate ( double events,
double timeframe )

poisson rate:
calculate the events per unit time
e.g 5 dollars every 2 mins = 5 / 2 = 2.5

Definition at line 17 of file poisson_dist.cpp.

17 {
18 return events / timeframe;
19}

◆ poisson_x_successes()

double poisson_x_successes ( double expected,
double x )

Find the probability of x successes in a Poisson dist.

\[p(\mu,x) = \frac{\mu^x e^{-\mu}}{x!}\]

Definition at line 46 of file poisson_dist.cpp.

46 {
47 return (std::pow(expected, x) * std::exp(-expected)) / fact(x);
48}
double fact(double x)