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

Binomial distribution example More...

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

Go to the source code of this file.

Functions

double binomial_expected (double n, double p)
 
double binomial_variance (double n, double p)
 
double binomial_standard_deviation (double n, double p)
 
double nCr (double n, double r)
 
double binomial_x_successes (double n, double p, double x)
 
double binomial_range_successes (double n, double p, double lower_bound, double upper_bound)
 
int main ()
 

Detailed Description

Binomial distribution example

The binomial distribution models the number of successes in a sequence of n independent events

Summary of variables used:

  • n : number of trials
  • p : probability of success
  • x : desired successes

Definition in file binomial_dist.cpp.

Function Documentation

◆ binomial_expected()

double binomial_expected ( double n,
double p )

finds the expected value of a binomial distribution

Parameters
[in]n
[in]p
Returns
\(\mu=np\)

Definition at line 22 of file binomial_dist.cpp.

22{ return n * p; }

◆ binomial_range_successes()

double binomial_range_successes ( double n,
double p,
double lower_bound,
double upper_bound )

calculates the probability of a result within a range (inclusive, inclusive)

Returns
\(\displaystyle \left.P(n,p)\right|_{x_0}^{x_1} = \sum_{i=x_0}^{x_1} P(i) =\sum_{i=x_0}^{x_1} {n\choose i} p^i (1-p)^{n-i}\)

Definition at line 74 of file binomial_dist.cpp.

75 {
76 double probability = 0;
77 for (int i = lower_bound; i <= upper_bound; i++) {
78 probability += nCr(n, i) * std::pow(p, i) * std::pow(1 - p, n - i);
79 }
80 return probability;
81}
double nCr(double n, double r)
Probability algorithms.

◆ binomial_standard_deviation()

double binomial_standard_deviation ( double n,
double p )

finds the standard deviation of the binomial distribution

Parameters
[in]n
[in]p
Returns
\(\sigma = \sqrt{\sigma^2} = \sqrt{n\cdot p\cdot (1-p)}\)

Definition at line 36 of file binomial_dist.cpp.

36 {
37 return std::sqrt(binomial_variance(n, p));
38}
double binomial_variance(double n, double p)

◆ binomial_variance()

double binomial_variance ( double n,
double p )

finds the variance of the binomial distribution

Parameters
[in]n
[in]p
Returns
\(\sigma^2 = n\cdot p\cdot (1-p)\)

Definition at line 29 of file binomial_dist.cpp.

29{ return n * p * (1 - p); }

◆ binomial_x_successes()

double binomial_x_successes ( double n,
double p,
double x )

calculates the probability of exactly x successes

Returns
\(\displaystyle P(n,p,x) = {n\choose x} p^x (1-p)^{n-x}\)

Definition at line 65 of file binomial_dist.cpp.

65 {
66 return nCr(n, x) * std::pow(p, x) * std::pow(1 - p, n - x);
67}

◆ main()

int main ( void )

main function

Definition at line 84 of file binomial_dist.cpp.

84 {
85 std::cout << "expected value : " << binomial_expected(100, 0.5)
86 << std::endl;
87
88 std::cout << "variance : " << binomial_variance(100, 0.5) << std::endl;
89
90 std::cout << "standard deviation : "
91 << binomial_standard_deviation(100, 0.5) << std::endl;
92
93 std::cout << "exactly 30 successes : " << binomial_x_successes(100, 0.5, 30)
94 << std::endl;
95
96 std::cout << "45 or more successes : "
97 << binomial_range_successes(100, 0.5, 45, 100) << std::endl;
98
99 return 0;
100}
double binomial_x_successes(double n, double p, double x)
double binomial_expected(double n, double p)
double binomial_range_successes(double n, double p, double lower_bound, double upper_bound)
double binomial_standard_deviation(double n, double p)

◆ nCr()

double nCr ( double n,
double r )

Computes n choose r

Parameters
[in]n
[in]r
Returns
\(\displaystyle {n\choose r} = \frac{n!}{r!(n-r)!} = \frac{n\times(n-1)\times(n-2)\times\cdots(n-r)}{r!} \)

Definition at line 47 of file binomial_dist.cpp.

47 {
48 double numerator = n;
49 double denominator = r;
50
51 for (int i = n - 1; i >= ((n - r) + 1); i--) {
52 numerator *= i;
53 }
54
55 for (int i = 1; i < r; i++) {
56 denominator *= i;
57 }
58
59 return numerator / denominator;
60}