openGPMP
Open Source Mathematics Package
pdfs.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 #include <algorithm>
33 #include <cmath>
34 #include <limits>
35 #include <numeric>
36 #include <openGPMP/core/utils.hpp>
37 #include <openGPMP/stats/pdfs.hpp>
38 #include <random>
39 #include <stdexcept>
40 #include <vector>
41 
42 // Bernoulli distribution
43 // Bernoulli distribution
44 double gpmp::stats::PDF::bernoulli(double x, double p) {
45  if (std::abs(x - 0) < std::numeric_limits<double>::epsilon())
46  return 1 - p;
47  else if (std::abs(x - 1) < std::numeric_limits<double>::epsilon())
48  return p;
49  else
50  return 0;
51 }
52 
53 // Beta distribution
54 double gpmp::stats::PDF::beta(double x, double alpha, double beta) {
55  if (x < 0 || x > 1)
56  return 0;
57  else
58  return (std::pow(x, alpha - 1) * std::pow(1 - x, beta - 1)) /
59  beta_function(alpha, beta);
60 }
61 
62 // Binomial distribution
63 double gpmp::stats::PDF::binomial(int k, int n, double p) {
64  if (k < 0 || k > n)
65  return 0;
66  else
67  return binomial_coefficient(n, k) * std::pow(p, k) *
68  std::pow(1 - p, n - k);
69 }
70 
71 // Cauchy distribution
72 double gpmp::stats::PDF::cauchy(double x, double x0, double gamma) {
73  return (1 / (M_PI * gamma)) *
74  (gamma * gamma / ((x - x0) * (x - x0) + gamma * gamma));
75 }
76 
77 // Chi-squared distribution
78 double gpmp::stats::PDF::chi_squared(double x, int k) {
79  if (x < 0)
80  return 0;
81  else
82  return (std::pow(x, k / 2.0 - 1) * std::exp(-x / 2.0)) /
83  (std::pow(2, k / 2.0) * std::tgamma(k / 2.0));
84 }
85 
86 // Exponential distribution
87 double gpmp::stats::PDF::exponential(double x, double lambda) {
88  if (x < 0)
89  return 0;
90  else
91  return lambda * std::exp(-lambda * x);
92 }
93 
94 // F distribution
95 double gpmp::stats::PDF::f_dist(double x, int df1, int df2) {
96  if (x < 0)
97  return 0;
98  else
99  return std::pow(df1, df1 / 2.0) * std::pow(df2, df2 / 2.0) *
100  std::pow(x, df1 / 2.0 - 1) /
101  (std::pow(df2 + df1 * x, (df1 + df2) / 2.0) *
102  std::tgamma(df1 / 2.0) * std::tgamma(df2 / 2.0));
103 }
104 
105 // Gamma distribution
106 double gpmp::stats::PDF::gamma(double x, double alpha, double beta) {
107  if (x < 0)
108  return 0;
109  else
110  return (std::pow(x, alpha - 1) * std::exp(-x / beta)) /
111  (std::pow(beta, alpha) * std::tgamma(alpha));
112 }
113 
114 // Inverse-Gamma distribution
115 double gpmp::stats::PDF::inverse_gamma(double x, double alpha, double beta) {
116  if (x <= 0)
117  return 0;
118  else
119  return (std::pow(beta, alpha) * std::pow(x, -alpha - 1) *
120  std::exp(-beta / x)) /
121  std::tgamma(alpha);
122 }
123 
124 // Inverse-Gaussian distribution
125 double gpmp::stats::PDF::inverse_gaussian(double x, double mu, double lambda) {
126  if (x <= 0)
127  return 0;
128  else
129  return std::sqrt(lambda / (2 * M_PI * x * x * x)) *
130  std::exp(-lambda * (x - mu) * (x - mu) / (2 * mu * mu * x));
131 }
132 
133 // Laplace distribution
134 double gpmp::stats::PDF::laplace(double x, double mu, double b) {
135  return 0.5 * std::exp(-std::abs(x - mu) / b) / b;
136 }
137 
138 // Logistic distribution
139 double gpmp::stats::PDF::logistic(double x, double mu, double s) {
140  double z = (x - mu) / s;
141  return std::exp(-z) / (s * std::pow(1 + std::exp(-z), 2));
142 }
143 
144 // Log-Normal distribution
145 double gpmp::stats::PDF::log_normal(double x, double mu, double sigma) {
146  if (x <= 0)
147  return 0;
148  else
149  return (1 / (x * sigma * std::sqrt(2 * M_PI))) *
150  std::exp(-0.5 * std::pow((std::log(x) - mu) / sigma, 2));
151 }
152 
153 // Normal (Gaussian) distribution
154 double gpmp::stats::PDF::gaussian(double x, double mu, double sigma) {
155  return (1 / (sigma * std::sqrt(2 * M_PI))) *
156  std::exp(-0.5 * std::pow((x - mu) / sigma, 2));
157 }
158 
159 // Poisson distribution
160 double gpmp::stats::PDF::poisson(int k, double lambda) {
161  if (k < 0)
162  return 0;
163  else
164  return std::exp(-lambda) * std::pow(lambda, k) /
166 }
167 
168 // Rademacher distribution
170  if (k == -1)
171  return 0.5;
172  else if (k == 1)
173  return 0.5;
174  else
175  return 0;
176 }
177 
178 // Student's t distribution
179 double gpmp::stats::PDF::student_t(double x, int df) {
180  double numerator = std::tgamma((df + 1) / 2.0);
181  double denominator = std::sqrt(df * M_PI) * std::tgamma(df / 2.0);
182  return std::pow(1 + x * x / df, -(df + 1) / 2.0) * numerator / denominator;
183 }
184 
185 // Uniform distribution
186 double gpmp::stats::PDF::uniform(double x, double a, double b) {
187  if (x >= a && x <= b)
188  return 1 / (b - a);
189  else
190  return 0;
191 }
192 
193 // Weibull distribution
194 double gpmp::stats::PDF::weibull(double x, double k, double lambda) {
195  if (x < 0)
196  return 0;
197  else
198  return (k / lambda) * std::pow(x / lambda, k - 1) *
199  std::exp(-std::pow(x / lambda, k));
200 }
201 
202 // Function to calculate binomial coefficient
204  double result = 1.0;
205  for (int i = 1; i <= k; ++i) {
206  result *= static_cast<double>(n - (k - i)) / i;
207  }
208  return result;
209 }
210 
211 // Function to calculate beta function
212 double gpmp::stats::PDF::beta_function(double alpha, double beta) {
213  return std::tgamma(alpha) * std::tgamma(beta) / std::tgamma(alpha + beta);
214 }
static T factorial(T n)
Calculates the factorial of the given integer or double.
Definition: utils.hpp:211
static double bernoulli(double x, double p)
Calculates the probability of success in a Bernoulli trial.
Definition: pdfs.cpp:44
static double gamma(double x, double alpha, double beta)
Calculates the probability density function (PDF) of the gamma distribution.
Definition: pdfs.cpp:106
static double beta(double x, double alpha, double beta)
Calculates the probability density function (PDF) of the Beta distribution.
Definition: pdfs.cpp:54
static double poisson(int k, double lambda)
Calculates the probability density function (PDF) of the Poisson distribution.
Definition: pdfs.cpp:160
static double gaussian(double x, double mu, double sigma)
Calculates the probability density function (PDF) of the Gaussian (normal) distribution.
Definition: pdfs.cpp:154
static double log_normal(double x, double mu, double sigma)
Calculates the probability density function (PDF) of the log-normal distribution.
Definition: pdfs.cpp:145
static double binomial(int k, int n, double p)
Calculates the probability of observing k successes in n independent Bernoulli trials.
Definition: pdfs.cpp:63
static double uniform(double x, double a, double b)
Calculates the probability density function (PDF) of the uniform distribution.
Definition: pdfs.cpp:186
static double f_dist(double x, int df1, int df2)
Calculates the probability density function (PDF) of the F distribution.
Definition: pdfs.cpp:95
static double exponential(double x, double lambda)
Calculates the probability density function (PDF) of the exponential distribution.
Definition: pdfs.cpp:87
static double inverse_gaussian(double x, double mu, double lambda)
Calculates the probability density function (PDF) of the inverse Gaussian distribution.
Definition: pdfs.cpp:125
static double inverse_gamma(double x, double alpha, double beta)
Calculates the probability density function (PDF) of the inverse gamma distribution.
Definition: pdfs.cpp:115
static double weibull(double x, double k, double lambda)
Calculates the probability density function (PDF) of the Weibull distribution.
Definition: pdfs.cpp:194
static double chi_squared(double x, int k)
Calculates the probability density function (PDF) of the chi-squared distribution.
Definition: pdfs.cpp:78
static double laplace(double x, double mu, double b)
Calculates the probability density function (PDF) of the Laplace distribution.
Definition: pdfs.cpp:134
static double cauchy(double x, double x0, double gamma)
Calculates the probability density function (PDF) of the Cauchy distribution.
Definition: pdfs.cpp:72
static double student_t(double x, int df)
Calculates the probability density function (PDF) of Student's t distribution.
Definition: pdfs.cpp:179
static double rademacher(int k)
Calculates the probability density function (PDF) of the Rademacher distribution.
Definition: pdfs.cpp:169
static double logistic(double x, double mu, double s)
Calculates the probability density function (PDF) of the logistic distribution.
Definition: pdfs.cpp:139
static double beta_function(double alpha, double beta)
Calculates the beta function.
Definition: pdfs.cpp:212
static double binomial_coefficient(int n, int k)
Calculates the binomial coefficient "n choose k".
Definition: pdfs.cpp:203
Miscellaneous utilities methods related to openGPMP.