7 #include "GaussianDistribution.h"
20 GaussianDistribution::GaussianDistribution(
const double mean,
const double standardDeviation) {
22 p_stdev = standardDeviation;
34 double GaussianDistribution::Probability(
const double value) {
36 return std::exp(-0.5 * std::pow((value - p_mean) / p_stdev, 2)) / (std::sqrt(2 *
PI) * p_stdev);
48 double GaussianDistribution::CumulativeDistribution(
const double value) {
50 if(value == DBL_MIN) {
53 else if(value == DBL_MAX) {
59 double x = (value - p_mean) / p_stdev;
66 for(
int n = 0; pre != sum; n++) {
69 sum += std::pow(x, 2 * n + 1) / (fact * (2 * n + 1) * std::pow(-2.0, n));
74 return 50.0 + 100.0 / std::sqrt(2.0 *
PI) * sum;
90 double GaussianDistribution::InverseCumulativeDistribution(
const double percent) {
92 static double lowCutoff = 2.425;
93 static double highCutoff = 97.575;
95 if((percent < 0.0) || (percent > 100.0)) {
96 string m =
"Argument percent outside of the range 0 to 100 in";
97 m +=
" [GaussianDistribution::InverseCumulativeDistribution]";
98 throw IException(IException::Programmer, m, _FILEINFO_);
109 else if(percent == 100.0) {
113 if(percent < lowCutoff) {
114 double q = std::sqrt(-2.0 * std::log(percent / 100.0));
117 else if(percent < highCutoff) {
118 double q = percent / 100.0 - 0.5,
123 double q = std::sqrt(-2.0 * std::log(1.0 - percent / 100.0));
124 x = -1.0 * C(q) / D(q);
129 double e = CumulativeDistribution(p_stdev * x + p_mean) - percent;
130 double u = e * std::sqrt(2.0 *
PI) * std::exp(-0.5 * x * x);
131 x = x - u / (1 + 0.5 * x * u);
133 return p_stdev * x + p_mean;
140 double GaussianDistribution::A(
const double x) {
141 static const double a[6] = { -39.69683028665376,
149 return((((a[0] * x + a[1]) * x + a[2]) * x + a[3]) * x + a[4]) * x + a[5];
152 double GaussianDistribution::B(
const double x) {
153 static const double b[6] = { -54.47609879822406,
161 return((((b[0] * x + b[1]) * x + b[2]) * x + b[3]) * x + b[4]) * x + b[5];
164 double GaussianDistribution::C(
const double x) {
165 static const double c[6] = { -0.007784894002430293,
173 return((((c[0] * x + c[1]) * x + c[2]) * x + c[3]) * x + c[4]) * x + c[5];
176 double GaussianDistribution::D(
const double x) {
177 static const double d[5] = { 0.007784695709041462,
184 return(((d[0] * x + d[1]) * x + d[2]) * x + d[3]) * x + d[4];