NormalDistribution.h
Go to the documentation of this file.
1 #ifndef _utl_NormalDistribution_h_
2 #define _utl_NormalDistribution_h_
3 
4 
5 #include <cmath>
6 #include <utl/Math.h>
7 #include <utl/MathConstants.h>
8 
9 
10 namespace utl {
11 
12  inline double NormalPDF(const double x)
13  { return std::exp(-0.5*Sqr(x)) / kSqrt2Pi; }
14 
15 
16  inline double NormalPDF(const double x, const double sigma)
17  { return NormalPDF(x / sigma) / sigma; }
18 
19 
20  inline double NormalPDF(const double x, const double mean, const double sigma)
21  { return NormalPDF(x - mean, sigma); }
22 
23 
24  // logarithm-of versions of functions are useful in log-likelihoods
25  // where log(Normal) are usually summed up
26 
27  inline double LogarithmOfNormalPDF(const double x)
28  { return -0.5*Sqr(x) - std::log(kSqrt2Pi); }
29 
30 
31  inline double LogarithmOfNormalPDF(const double x, const double sigma)
32  { return LogarithmOfNormalPDF(x / sigma) - std::log(sigma); }
33 
34 
35  inline double LogarithmOfNormalPDF(const double x, const double mean, const double sigma)
36  { return LogarithmOfNormalPDF(x - mean, sigma); }
37 
38 
39  inline
40  double
41  LogarithmOfErfc(const double x)
42  {
43  if (x < 10)
44  return std::log(std::erfc(x));
45  // asymptotic expansion
46  const double x2 = Sqr(x);
47  return -(x2 + std::log(x) + 0.5/x2 + 0.5*std::log(M_PI));
48  }
49 
50 
51  inline double NormalCDF(const double x)
52  { return 0.5 * std::erfc(-x / M_SQRT2); }
53 
54 
55  inline double NormalCDF(const double x, const double sigma)
56  { return NormalCDF(x / sigma); }
57 
58 
59  inline double NormalCDF(const double x, const double mean, const double sigma)
60  { return NormalCDF(x - mean, sigma); }
61 
62 
63  inline double LogarithmOfNormalComplementCDF(const double x)
64  { return -M_LN2 + LogarithmOfErfc(x / M_SQRT2); }
65 
66 
67  inline double LogarithmOfNormalComplementCDF(const double x, const double sigma)
68  { return LogarithmOfNormalComplementCDF(x / sigma); }
69 
70 
71  inline double LogarithmOfNormalComplementCDF(const double x, const double mean, const double sigma)
72  { return LogarithmOfNormalComplementCDF(x - mean, sigma); }
73 
74 
75  inline double LogarithmOfNormalCDF(const double x)
76  { return LogarithmOfNormalComplementCDF(-x); }
77 
78 
79  inline double LogarithmOfNormalCDF(const double x, const double sigma)
80  { return LogarithmOfNormalCDF(x / sigma); }
81 
82 
83  inline double LogarithmOfNormalCDF(const double x, const double mean, const double sigma)
84  { return LogarithmOfNormalCDF(x - mean, sigma); }
85 
86 
91  double InverseNormalCDF(const double p);
92 
93 
94  inline double InverseNormalCDF(const double p, const double sigma)
95  { return sigma * InverseNormalCDF(p); }
96 
97 
98  inline double InverseNormalCDF(const double p, const double mean, const double sigma)
99  { return mean + InverseNormalCDF(p, sigma); }
100 
101 
102  double TruncatedNormalPDF(const double x, const double mu, const double sigma,
103  const double xmin, const double xmax);
104 
105 
106  // xmax = infinity
107  double TruncatedNormalPDF(const double x, const double mu, const double sigma,
108  double xmin);
109 
110 
112  inline double PositiveNormalPDF(double x, double mu, double sigma)
113  { return TruncatedNormalPDF(x, mu, sigma, 0); }
114 
115 }
116 
117 
118 #endif
double LogarithmOfNormalComplementCDF(const double x)
constexpr double kSqrt2Pi
Definition: MathConstants.h:32
constexpr T Sqr(const T &x)
double TruncatedNormalPDF(const double x, const double mu, const double sigma, const double xmin, const double xmax)
double PositiveNormalPDF(double x, double mu, double sigma)
Truncated normal distribution with xmin = 0 and xmax = infinity.
double LogarithmOfErfc(const double x)
double InverseNormalCDF(const double p)
Inverse of the comulative normal distribution. Taken from http://home.online.no/~pjacklam/notes/invno...
double NormalPDF(const double x)
double NormalCDF(const double x)
double LogarithmOfNormalCDF(const double x)
double LogarithmOfNormalPDF(const double x)

, generated on Tue Sep 26 2023.