From 65096bee14b66788eba3694fc7d317aa8854d845 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 13 Aug 2024 10:01:08 -0400 Subject: [PATCH] Add formulas for logcdf and logpdf to the tables --- doc/distributions/exponential.qbk | 2 ++ doc/distributions/extreme_value.qbk | 2 ++ doc/distributions/gamma.qbk | 1 + doc/distributions/geometric.qbk | 1 + doc/distributions/inverse_gamma.qbk | 3 ++- doc/distributions/inverse_gaussian.qbk | 1 + doc/distributions/laplace.qbk | 8 ++++++++ doc/distributions/logistic.qbk | 1 + doc/distributions/normal.qbk | 1 + doc/distributions/pareto.qbk | 1 + doc/distributions/poisson.qbk | 1 + doc/distributions/rayleigh.qbk | 2 ++ doc/distributions/weibull.qbk | 2 ++ include/boost/math/distributions/laplace.hpp | 2 +- 14 files changed, 26 insertions(+), 2 deletions(-) diff --git a/doc/distributions/exponential.qbk b/doc/distributions/exponential.qbk index 0b7ea5cbb1..b47f0b8bcd 100644 --- a/doc/distributions/exponential.qbk +++ b/doc/distributions/exponential.qbk @@ -74,7 +74,9 @@ In the following table [lambda] is the parameter lambda of the distribution, [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = [lambda] * exp(-[lambda] * x) ]] +[[logpdf][log(pdf) = -expm1(-x * [lambda]) ]] [[cdf][Using the relation: p = 1 - exp(-x * [lambda]) = -expm1(-x * [lambda]) ]] +[[logcdf][log(cdf) = log1p(-exp(-x * [lambda])) ]] [[cdf complement][Using the relation: q = exp(-x * [lambda]) ]] [[quantile][Using the relation: x = -log(1-p) / [lambda] = -log1p(-p) / [lambda]]] [[quantile from the complement][Using the relation: x = -log(q) / [lambda]]] diff --git a/doc/distributions/extreme_value.qbk b/doc/distributions/extreme_value.qbk index 56c4fb5dfd..43f68546f9 100644 --- a/doc/distributions/extreme_value.qbk +++ b/doc/distributions/extreme_value.qbk @@ -99,7 +99,9 @@ In the following table: [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = exp((a-x)/b) * exp(-exp((a-x)/b)) / b ]] +[[logpdf][log(pdf) = log(1/b) + e - exp(e) ]] [[cdf][Using the relation: p = exp(-exp((a-x)/b)) ]] +[[logcdf][log(cdf) = -exp((a-x)/b) ]] [[cdf complement][Using the relation: q = -expm1(-exp((a-x)/b)) ]] [[quantile][Using the relation: a - log(-log(p)) * b]] [[quantile from the complement][Using the relation: a - log(-log1p(-q)) * b]] diff --git a/doc/distributions/gamma.qbk b/doc/distributions/gamma.qbk index 8e53ec6ee4..56be002194 100644 --- a/doc/distributions/gamma.qbk +++ b/doc/distributions/gamma.qbk @@ -118,6 +118,7 @@ and /q = 1-p/. [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = __gamma_p_derivative(k, x / [theta]) / [theta] ]] +[[logpdf][log(pdf) = -k*log([theta]) + (k-1)*log(x) - lgamma(k) - (x/[theta]) ]] [[cdf][Using the relation: p = __gamma_p(k, x / [theta]) ]] [[cdf complement][Using the relation: q = __gamma_q(k, x / [theta]) ]] [[quantile][Using the relation: x = [theta] * __gamma_p_inv(k, p) ]] diff --git a/doc/distributions/geometric.qbk b/doc/distributions/geometric.qbk index 9992b016de..cf1e2bff08 100644 --- a/doc/distributions/geometric.qbk +++ b/doc/distributions/geometric.qbk @@ -325,6 +325,7 @@ the expected number of failures using the quantile. [[Function][Implementation Notes]] [[pdf][pdf = p * pow(q, k)]] [[cdf][cdf = 1 - q[super k=1]]] +[[logcdf][log(cdf) = log1p(-exp(log1p(-p) * (k+1)))]] [[cdf complement][exp(log1p(-p) * (k+1))]] [[quantile][k = log1p(-x) / log1p(-p) -1]] [[quantile from the complement][k = log(x) / log1p(-p) -1]] diff --git a/doc/distributions/inverse_gamma.qbk b/doc/distributions/inverse_gamma.qbk index 8fb64dde84..51d6538846 100644 --- a/doc/distributions/inverse_gamma.qbk +++ b/doc/distributions/inverse_gamma.qbk @@ -102,12 +102,13 @@ But in general, inverse_gamma results are accurate to a few epsilon, [h4 Implementation] In the following table [alpha] is the shape parameter of the distribution, -[alpha] is its scale parameter, /x/ is the random variate, /p/ is the probability +[beta] is its scale parameter, /x/ is the random variate, /p/ is the probability and /q = 1-p/. [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = __gamma_p_derivative([alpha], [beta]/ x, [beta]) / x * x ]] +[[logpdf][log(pdf) = [alpha] * log([beta]) + (-[alpha]-1)*log(x) - lgamma([alpha]) - ([beta]/x) ]] [[cdf][Using the relation: p = __gamma_q([alpha], [beta] / x) ]] [[cdf complement][Using the relation: q = __gamma_p([alpha], [beta] / x) ]] [[quantile][Using the relation: x = [beta]/ __gamma_q_inv([alpha], p) ]] diff --git a/doc/distributions/inverse_gaussian.qbk b/doc/distributions/inverse_gaussian.qbk index 776a377726..6187a3835b 100644 --- a/doc/distributions/inverse_gaussian.qbk +++ b/doc/distributions/inverse_gaussian.qbk @@ -137,6 +137,7 @@ are used for the inverse gaussian function. [table [[Function] [Implementation Notes] ] [[pdf] [ [sqrt]([lambda]/ 2[pi]x[super 3]) e[super -[lambda](x - [mu])[sup2]/ 2[mu][sup2]x]]] +[[logpdf] [log(pdf) = (-[lambda]*pow([mu]-x, 2)/(x*[mu][super 2]) + log([lambda]) - 3*log(x) - log(2*[pi])) / 2 ]] [[cdf][ [Phi]{[sqrt]([lambda]/x) (x/[mu]-1)} + e[super 2[mu]/[lambda]] [Phi]{-[sqrt]([lambda]/[mu]) (1+x/[mu])} ]] [[cdf complement] [using complement of [Phi] above.] ] [[quantile][No closed form known. Estimated using a guess refined by Newton-Raphson iteration.]] diff --git a/doc/distributions/laplace.qbk b/doc/distributions/laplace.qbk index 998072811f..1afa7f4e1e 100644 --- a/doc/distributions/laplace.qbk +++ b/doc/distributions/laplace.qbk @@ -93,11 +93,19 @@ and its complement /q = 1-p/. [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = e[super -abs(x-[mu]) \/ [sigma]] \/ (2 * [sigma]) ]] +[[logpdf][log(pdf) = -abs(x-[mu])/[sigma] - log([sigma]) - log(2) ]] [[cdf][Using the relations: x < [mu] : p = e[super (x-[mu])/[sigma] ] \/ [sigma] x >= [mu] : p = 1 - e[super ([mu]-x)/[sigma] ] \/ [sigma] +]] +[[logcdf][log(cdf) = + +x < [mu] : p = ((x - [mu]) / [sigma]) - ln(2) + +x >= [mu] : p = log1p(-exp(([mu]-x) / [sigma]) / 2) + ]] [[cdf complement][Using the relation: diff --git a/doc/distributions/logistic.qbk b/doc/distributions/logistic.qbk index ba1b29a17e..84b3a24f1d 100644 --- a/doc/distributions/logistic.qbk +++ b/doc/distributions/logistic.qbk @@ -85,6 +85,7 @@ in such cases, only a low /absolute error/ can be guaranteed. [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = e[super -(x-u)/s] / (s*(1+e[super -(x-u)/s])[super 2])]] [[cdf][Using the relation: p = 1/(1+e[super -(x-u)/s])]] +[[logcdf][log(cdf) = -log1p(exp((u-x)/s)) ]] [[cdf complement][Using the relation: q = 1/(1+e[super (x-u)/s])]] [[quantile][Using the relation: x = u - s*log(1/p-1)]] [[quantile from the complement][Using the relation: x = u + s*log(p/1-p)]] diff --git a/doc/distributions/normal.qbk b/doc/distributions/normal.qbk index 52ac44e96b..eefe4e967b 100644 --- a/doc/distributions/normal.qbk +++ b/doc/distributions/normal.qbk @@ -97,6 +97,7 @@ and /s/ is its standard deviation. [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = e[super -(x-m)[super 2]\/(2s[super 2])] \/ (s * sqrt(2*pi)) ]] +[[logpdf][log(pdf) = -log(s) - log(2*[pi])/2 - (x-mean)[super 2]/(2*s[super 2]) ]] [[cdf][Using the relation: p = 0.5 * __erfc(-(x-m)/(s*sqrt(2))) ]] [[cdf complement][Using the relation: q = 0.5 * __erfc((x-m)/(s*sqrt(2))) ]] [[quantile][Using the relation: x = m - s * sqrt(2) * __erfc_inv(2*p)]] diff --git a/doc/distributions/pareto.qbk b/doc/distributions/pareto.qbk index 7938ed86d8..6a89b0dbc5 100644 --- a/doc/distributions/pareto.qbk +++ b/doc/distributions/pareto.qbk @@ -94,6 +94,7 @@ and its complement /q = 1-p/. [[Function][Implementation Notes]] [[pdf][Using the relation: pdf p = [alpha][beta][super [alpha]]/x[super [alpha] +1] ]] [[cdf][Using the relation: cdf p = 1 - ([beta] / x)[super [alpha]] ]] +[[logcdf][log(cdf) = log1p(-pow([beta]/x, [alpha])) ]] [[cdf complement][Using the relation: q = 1 - p = -([beta] / x)[super [alpha]] ]] [[quantile][Using the relation: x = [beta] / (1 - p)[super 1/[alpha]] ]] [[quantile from the complement][Using the relation: x = [beta] / (q)[super 1/[alpha]] ]] diff --git a/doc/distributions/poisson.qbk b/doc/distributions/poisson.qbk index a1ccc1b8b7..01cfb13cc5 100644 --- a/doc/distributions/poisson.qbk +++ b/doc/distributions/poisson.qbk @@ -84,6 +84,7 @@ In the following table [lambda] is the mean of the distribution, [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = e[super -[lambda]] [lambda][super k] \/ k! ]] +[[logpdf][log(pdf) = -lgamma(k+1) + k*log([lambda]) - [lambda] if k > 0 and [lambda] > 0 ]] [[cdf][Using the relation: p = [Gamma](k+1, [lambda]) \/ k! = __gamma_q(k+1, [lambda])]] [[cdf complement][Using the relation: q = __gamma_p(k+1, [lambda]) ]] [[quantile][Using the relation: k = __gamma_q_inva([lambda], p) - 1]] diff --git a/doc/distributions/rayleigh.qbk b/doc/distributions/rayleigh.qbk index 89054ac0e3..609b654da7 100644 --- a/doc/distributions/rayleigh.qbk +++ b/doc/distributions/rayleigh.qbk @@ -95,7 +95,9 @@ In the following table [sigma] is the sigma parameter of the distribution, [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = x * exp(-x[super 2])/2 [sigma][super 2] ]] +[[logpdf][log(pdf) = -(x[super 2])/(2*[sigma][super 2]) - 2*log([sigma]) + log(x) ]] [[cdf][Using the relation: p = 1 - exp(-x[super 2]/2) [sigma][super 2]= -__expm1(-x[super 2]/2) [sigma][super 2]]] +[[logcdf][log(cdf) = log1p(-exp(-x[super 2] / (2*[sigma][super 2]))) ]] [[cdf complement][Using the relation: q = exp(-x[super 2]/ 2) * [sigma][super 2] ]] [[quantile][Using the relation: x = sqrt(-2 * [sigma] [super 2]) * log(1 - p)) = sqrt(-2 * [sigma] [super 2]) * __log1p(-p))]] [[quantile from the complement][Using the relation: x = sqrt(-2 * [sigma] [super 2]) * log(q)) ]] diff --git a/doc/distributions/weibull.qbk b/doc/distributions/weibull.qbk index d01ceded9e..3ec18b18d9 100644 --- a/doc/distributions/weibull.qbk +++ b/doc/distributions/weibull.qbk @@ -107,7 +107,9 @@ and /q = 1-p/. [table [[Function][Implementation Notes]] [[pdf][Using the relation: pdf = [alpha][beta][super -[alpha] ]x[super [alpha] - 1] e[super -(x/beta)[super alpha]] ]] +[[logpdf][log(pdf) = log([alpha]) - [alpha] * log([beta]) + log(x) * ([alpha]-1) - pow(x/[beta], [alpha]) ]] [[cdf][Using the relation: p = -__expm1(-(x\/[beta])[super [alpha]]) ]] +[[logcdf][log(cdf) = log1p(-exp(-pow(x / [beta], [alpha]))) ]] [[cdf complement][Using the relation: q = e[super -(x\/[beta])[super [alpha]]] ]] [[quantile][Using the relation: x = [beta] * (-__log1p(-p))[super 1\/[alpha]] ]] [[quantile from the complement][Using the relation: x = [beta] * (-log(q))[super 1\/[alpha]] ]] diff --git a/include/boost/math/distributions/laplace.hpp b/include/boost/math/distributions/laplace.hpp index 55e2236f99..4d1fc46569 100644 --- a/include/boost/math/distributions/laplace.hpp +++ b/include/boost/math/distributions/laplace.hpp @@ -182,7 +182,7 @@ BOOST_MATH_GPU_ENABLED inline RealType logpdf(const laplace_distribution::epsilon()) { result = log(pdf(dist, x));