GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
normalDistribution.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <cmath>
5 
6 double rationalApproximation(double t)
7 {
8  // Abramowitz and Stegun formula 26.2.23.
9  // The absolute value of the error should be less than 4.5 e-4.
10  const double c[] = {2.515517, 0.802853, 0.010328};
11  const double d[] = {1.432788, 0.189269, 0.001308};
12  return t - ((c[2] * t + c[1])*t + c[0]) /
13  (((d[2]*t + d[1])*t + d[0])*t + 1.0);
14 }
15 
16 // https://www.johndcook.com/blog/normal_cdf_inverse/
17 double normalCDFInverse(double p)
18 {
19  if(p <= 0.0 || p >= 1.0)
20  {
21  throw std::invalid_argument("Invalid input argument - 0.0 > p < 1.0");
22  }
23 
24  // Original approximation applies ifp >= 0.5
25  if(p < 0.5)
26  {
27  // F^-1(p) = - G^-1(p)
28  return -rationalApproximation(sqrt(-2.0 * log(p)));
29  }
30  // Otherwise, as normal distribution is symmetrical, flip p
31  else
32  {
33  // F^-1(p) = G^-1(1-p)
34  return rationalApproximation(sqrt(-2.0 * log(1.0 - p)));
35  }
36 }
double normalCDFInverse(double p)
Definition: normalDistribution.h:17
double rationalApproximation(double t)
Definition: normalDistribution.h:6