6 #ifndef THEORETICA_DISTRIBUTIONS
7 #define THEORETICA_DISTRIBUTIONS
9 #include "../core/real_analysis.h"
10 #include "../algebra/vec.h"
12 #include "../core/function.h"
13 #include "../core/special.h"
31 for (
unsigned int i = 0; i < X.
size(); ++i)
32 res *= f(X[i], theta);
48 for (
unsigned int i = 0; i < X.
size(); ++i)
49 res +=
ln(f(X[i], theta));
57 namespace distribution {
71 if(theta.
size() != 2) {
76 return gaussian(x, theta[0], theta[1]);
83 return pow(p, k) *
pow(1 - p, 1 - k);
90 if(theta.
size() != 1) {
95 return bernoulli(
static_cast<unsigned int>(k), theta[0]);
110 if(theta.
size() != 1) {
115 return poisson(
static_cast<unsigned int>(k), theta[0]);
123 pow(p, nu) *
pow(1 - p, n - nu);
130 if(theta.
size() != 2) {
135 return binomial(
static_cast<unsigned int>(nu),
136 static_cast<unsigned int>(theta[0]), theta[1]);
142 std::vector<unsigned int> x,
145 std::vector<real> p) {
147 if(x.size() != p.size() || x.size() != k) {
148 TH_MATH_ERROR(
"distribution::multinomial", x.size(), INVALID_ARGUMENT);
154 for (
unsigned int i = 0; i < k; ++i)
155 res *=
pow(p[i], x[i]) /
fact(x[i]);
170 return pow(x,
int(k / 2) - 1) *
exp(-x / 2.0)
181 if(theta.
size() != 1) {
186 return chi_squared(x,
static_cast<unsigned int>(theta[0]));
202 if(theta.
size() != 2) {
207 return gamma(x, theta[0], theta[1]);
223 if(theta.
size() != 2) {
228 return beta(x, theta[0], theta[1]);
235 const real a = 1 + (x * x / nu);
245 if(theta.
size() != 1) {
250 return student(x,
static_cast<unsigned int>(theta[0]));
257 return 1.0 / (2.50662827463 * sigma * x) *
265 if(theta.
size() != 2) {
280 return lambda *
exp(-lambda * x);
287 if(theta.
size() != 1) {
314 if(theta.
size() != 1) {
326 return 1.0 / (
PI * alpha * (1 + (
square(x - mu) /
square(alpha))));
333 if(theta.
size() != 2) {
338 return cauchy(x, theta[0], theta[1]);
352 if(theta.
size() != 2) {
371 if(theta.
size() != 1) {
383 return (1.0 / (2.0 * b)) *
exp(-
abs(x - mu) / b);
390 if(theta.
size() != 2) {
395 return (1.0 / (2.0 * theta[1])) *
exp(-
abs(x - theta[0]) / theta[1]);
410 return alpha *
powf(x_m, alpha) /
powf(x, alpha + 1);
417 if(theta.
size() != 2) {
422 return pareto(x, theta[0], theta[1]);
434 return pow(lambda, k) *
pow(x, k - 1)
435 *
exp(-lambda * x) /
fact(k - 1);
442 if(theta.
size() != 2) {
447 return erlang(x,
static_cast<unsigned int>(theta[0]), theta[1]);
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition: vec.h:402
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
real maxwell(real x, real a)
Maxwell-Boltzmann distribution.
Definition: distributions.h:362
real binomial(unsigned int nu, unsigned int n, real p)
Binomial distribution function.
Definition: distributions.h:120
real exponential(real x, real lambda)
Exponential distribution.
Definition: distributions.h:275
real poisson(unsigned int k, real lambda)
Poisson distribution.
Definition: distributions.h:101
real pareto(real x, real x_m, real alpha)
Pareto distribution.
Definition: distributions.h:400
real bernoulli(unsigned int k, real p)
Bernoulli distribution.
Definition: distributions.h:81
real beta(real x, real alpha, real beta)
Beta distribution density function with parameters alpha (shape) and beta (shape)
Definition: distributions.h:213
real breit_wigner(real x, real M, real Gamma)
Breit Wigner distribution.
Definition: distributions.h:343
real chi_squared(real x, unsigned int k)
Chi-squared distribution.
Definition: distributions.h:162
real erlang(real x, unsigned int k, real lambda)
Erlang distribution.
Definition: distributions.h:427
real laplace(real x, real mu, real b)
Laplace distribution.
Definition: distributions.h:381
real student(real x, unsigned int nu)
Student's t distribution.
Definition: distributions.h:233
real log_normal(real x, real mu, real sigma)
Log-normal distribution.
Definition: distributions.h:255
real multinomial(std::vector< unsigned int > x, unsigned int n, unsigned int k, std::vector< real > p)
Multinomial distribution.
Definition: distributions.h:141
real gamma(real x, real alpha, real beta)
Gamma distribution density function with parameters alpha (shape) and beta (rate)
Definition: distributions.h:192
real cauchy(real x, real mu, real alpha)
Cauchy distribution.
Definition: distributions.h:324
real gaussian(real x, real X, real sigma)
Gaussian distribution function.
Definition: distributions.h:61
real rayleigh(real x, real sigma)
Rayleigh distribution.
Definition: distributions.h:297
real beta(real x1, real x2)
Beta special function of real argument.
Definition: special.h:141
real half_gamma(unsigned int k)
Half Gamma special function, defined as HG(n) = Gamma(n / 2) for any positive integer n.
Definition: special.h:41
real gamma(unsigned int k)
Gamma special function of positive integer argument.
Definition: special.h:23
real likelihood(const vec< real > &X, const vec< real > &theta, stat_function f)
Compute the likelihood of a distribution <f> with the given parameters <theta> and measures <X>
Definition: distributions.h:27
real log_likelihood(const vec< real > &X, const vec< real > &theta, stat_function f)
Compute the log likelihood of a distribution <f> with the given parameters <theta> and measures <X>
Definition: distributions.h:44
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
TH_CONSTEXPR IntType binomial_coeff(unsigned int n, unsigned int m)
Compute the binomial coefficient.
Definition: real_analysis.h:1230
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:139
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
constexpr real SQRTPI
The square root of Pi.
Definition: constants.h:224
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition: dual2_functions.h:130
real powf(real x, real a)
Approximate x elevated to a real exponent.
Definition: real_analysis.h:796
TH_CONSTEXPR IntType fact(unsigned int n)
Compute the factorial of n.
Definition: real_analysis.h:671
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
std::function< real(real, const vec< real > &)> stat_function
Function pointer to a probability distribution function where the first argument is the variable and ...
Definition: function.h:30
constexpr real SQRT2
The square root of 2.
Definition: constants.h:251
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition: dual2_functions.h:41
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition: dual2_functions.h:29