binomial_dist.cpp File Reference

Binomial distribution example More...

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

## 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

## 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\)

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

{
double probability = 0;
for (int i = lower_bound; i <= upper_bound; i++) {
probability += nCr(n, i) * std::pow(p, i) * std::pow(1 - p, n - i);
}
return probability;
}
nCr
double nCr(double n, double r)
Definition: binomial_dist.cpp:47
std::pow
T pow(T... args)
std::upper_bound
T upper_bound(T... args)
Here is the call graph for this function:

## 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)}\)

{
return std::sqrt(binomial_variance(n, p));
}
binomial_variance
double binomial_variance(double n, double p)
Definition: binomial_dist.cpp:29
std::sqrt
T sqrt(T... args)
Here is the call graph for this function:

## 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)\)

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

{
return nCr(n, x) * std::pow(p, x) * std::pow(1 - p, n - x);
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

main function

{
std::cout << "expected value : " << binomial_expected(100, 0.5)
<< std::endl;
std::cout << "variance : " << binomial_variance(100, 0.5) << std::endl;
std::cout << "standard deviation : "
<< binomial_standard_deviation(100, 0.5) << std::endl;
std::cout << "exactly 30 successes : " << binomial_x_successes(100, 0.5, 30)
<< std::endl;
std::cout << "45 or more successes : "
<< binomial_range_successes(100, 0.5, 45, 100) << std::endl;
return 0;
}
std::cout
binomial_x_successes
double binomial_x_successes(double n, double p, double x)
Definition: binomial_dist.cpp:65
binomial_expected
double binomial_expected(double n, double p)
Definition: binomial_dist.cpp:22
binomial_range_successes
double binomial_range_successes(double n, double p, double lower_bound, double upper_bound)
Definition: binomial_dist.cpp:74
binomial_standard_deviation
double binomial_standard_deviation(double n, double p)
Definition: binomial_dist.cpp:36
std::endl
T endl(T... args)
Here is the call graph for this function:

## 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!} \)

{
double numerator = n;
double denominator = r;
for (int i = n - 1; i >= ((n - r) + 1); i--) {
numerator *= i;
}
for (int i = 1; i < r; i++) {
denominator *= i;
}
return numerator / denominator;
}