SETestKCL  1.0.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BinomialCalculator.cpp
Go to the documentation of this file.
1 #include "BinomialCalculator.h"
2 #include <boost/math/special_functions/binomial.hpp>
3 #include <boost/math/distributions/normal.hpp>
4 
5 #include <cmath>
6 #include <iostream>
7 
9 {
10  this->NumberOfExperiments = 1;
11  this->ProbabilityOfSuccess = 0.5;
12 
13 };
14 
16 {
17  unsigned int n = this->NumberOfExperiments;
18  if (k > n)
19  {
20  std::cerr << "ERROR: number of successes cannot be greater than number of experiments" <<std::endl;
21  return 0;
22  }
23 
24  float p = this->ProbabilityOfSuccess;
25 
26  float mass = 0.0;
27 
28  if (n > 100)
29  {
30  // Use the Normal approximation for the mass function to avoid overflow
31  float normal_mean = n * p;
32  float normal_sd = std::sqrt (n * p * (1 - p));
33  mass = boost::math::pdf (boost::math::normal(normal_mean, normal_sd), k);
34  }
35  else
36  {
37  // Use the binomial distribution for small observation numbers
38  float C = boost::math::binomial_coefficient<float> (n, k);
39  mass = C * std::pow (p, k) * std::pow(1.0 - p, n - k);
40  }
41 
42  return mass;
43 }
float GetProbabilityMassFunction(unsigned int k) const