math-functions-0.3.0.1: Collection of tools for numeric computations

Copyright (c) 2009 2011 2012 Bryan O'Sullivan BSD3 bos@serpentine.com experimental portable None Haskell2010

Numeric.SpecFunctions

Description

Special functions and factorials.

Synopsis

# Error function

Error function.

$\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} \exp(-t^2) dt$

Function limits are:

[ begin{aligned} &operatorname{erf}(-infty) &=& -1 -- &operatorname{erf}(0) &=& phantom{-},0 -- &operatorname{erf}(+infty) &=& phantom{-},1 -- end{aligned}

Complementary error function.

$\operatorname{erfc}(x) = 1 - \operatorname{erf}(x)$

Function limits are:

[ begin{aligned} &operatorname{erf}(-infty) &=&, 2 -- &operatorname{erf}(0) &=&, 1 -- &operatorname{erf}(+infty) &=&, 0 -- end{aligned}

Arguments

 :: Double p ∈ [-1,1] -> Double

Inverse of erf.

Arguments

 :: Double p ∈ [0,2] -> Double

Inverse of erfc.

# Gamma function

Compute the logarithm of the gamma function, Γ(x).

$\Gamma(x) = \int_0^{\infty}t^{x-1}e^{-t}\,dt = (x - 1)!$

This implementation uses Lanczos approximation. It gives 14 or more significant decimal digits, except around x = 1 and x = 2, where the function goes to zero.

Returns ∞ if the input is outside of the range (0 < x ≤ 1e305).

Synonym for logGamma. Retained for compatibility

Arguments

 :: Double z ∈ (0,∞) -> Double x ∈ (0,∞) -> Double

Compute the normalized lower incomplete gamma function γ(z,x). Normalization means that γ(z,∞)=1

$\gamma(z,x) = \frac{1}{\Gamma(z)}\int_0^{x}t^{z-1}e^{-t}\,dt$

Uses Algorithm AS 239 by Shea.

Arguments

 :: Double z ∈ (0,∞) -> Double p ∈ [0,1] -> Double

Inverse incomplete gamma function. It's approximately inverse of incompleteGamma for the same z. So following equality approximately holds:

invIncompleteGamma z . incompleteGamma z ≈ id

Compute ψ(x), the first logarithmic derivative of the gamma function.

$\psi(x) = \frac{d}{dx} \ln \left(\Gamma(x)\right) = \frac{\Gamma'(x)}{\Gamma(x)}$

Uses Algorithm AS 103 by Bernardo, based on Minka's C implementation.

# Beta function

Arguments

 :: Double a > 0 -> Double b > 0 -> Double

Compute the natural logarithm of the beta function.

$B(a,b) = \int_0^1 t^{a-1}(1-t)^{1-b}\,dt = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}$

Arguments

 :: Double a > 0 -> Double b > 0 -> Double x, must lie in [0,1] range -> Double

Regularized incomplete beta function.

$I(x;a,b) = \frac{1}{B(a,b)} \int_0^x t^{a-1}(1-t)^{1-b}\,dt$

Uses algorithm AS63 by Majumder and Bhattachrjee and quadrature approximation for large p and q.

Arguments

 :: Double logarithm of beta function for given p and q -> Double a > 0 -> Double b > 0 -> Double x, must lie in [0,1] range -> Double

Regularized incomplete beta function. Same as incompleteBeta but also takes logarithm of beta function as parameter.

Arguments

 :: Double a > 0 -> Double b > 0 -> Double x ∈ [0,1] -> Double

Compute inverse of regularized incomplete beta function. Uses initial approximation from AS109, AS64 and Halley method to solve equation.

# Sinc

Compute sinc function sin(x)/x

# Logarithm

Base starting from 4.9.0 (GHC 8.0) provides log1p and expm1 as method of class Floating. In this case we simply reexport these function. Otherwise we provide our own with more restrictive signature Double → Double.

log1p :: Floating a => a -> a #

log1p x computes log (1 + x), but provides more precise results for small (absolute) values of x if possible.

Since: base-4.9.0.0

Compute log(1+x)-x:

O(log n) Compute the logarithm in base 2 of the given value.

# Exponent

expm1 :: Floating a => a -> a #

expm1 x computes exp x - 1, but provides more precise results for small (absolute) values of x if possible.

Since: base-4.9.0.0

# Factorial

Compute the factorial function n!. Returns +∞ if the input is above 170 (above which the result cannot be represented by a 64-bit Double).

logFactorial :: Integral a => a -> Double Source #

Compute the natural logarithm of the factorial function. Gives 16 decimal digits of precision.

Calculate the error term of the Stirling approximation. This is only defined for non-negative values.

$\operatorname{stirlingError}(n) = \log(n!) - \log(\sqrt{2\pi n}\frac{n}{e}^n)$

# Combinatorics

Compute the binomial coefficient n choose k. For values of k > 50, this uses an approximation for performance reasons. The approximation is accurate to 12 decimal places in the worst case

Example:

7 choose 3 == 35

Compute logarithm of the binomial coefficient.

# References

• Bernardo, J. (1976) Algorithm AS 103: Psi (digamma) function. /Journal of the Royal Statistical Society. Series C (Applied Statistics)/ 25(3):315-317. http://www.jstor.org/stable/2347257
• Cran, G.W., Martin, K.J., Thomas, G.E. (1977) Remark AS R19 and Algorithm AS 109: A Remark on Algorithms: AS 63: The Incomplete Beta Integral AS 64: Inverse of the Incomplete Beta Function Ratio. /Journal of the Royal Statistical Society. Series C (Applied Statistics)/ Vol. 26, No. 1 (1977), pp. 111-114 http://www.jstor.org/pss/2346887
• Lanczos, C. (1964) A precision approximation of the gamma function. SIAM Journal on Numerical Analysis B 1:86–96. http://www.jstor.org/stable/2949767
• Loader, C. (2000) Fast and Accurate Computation of Binomial Probabilities. http://projects.scipy.org/scipy/raw-attachment/ticket/620/loader2000Fast.pdf
• Macleod, A.J. (1989) Algorithm AS 245: A robust and reliable algorithm for the logarithm of the gamma function. Journal of the Royal Statistical Society, Series C (Applied Statistics) 38(2):397–402. http://www.jstor.org/stable/2348078
• Majumder, K.L., Bhattacharjee, G.P. (1973) Algorithm AS 63: The Incomplete Beta Integral. /Journal of the Royal Statistical Society. Series C (Applied Statistics)/ Vol. 22, No. 3 (1973), pp. 409-411. http://www.jstor.org/pss/2346797
• Majumder, K.L., Bhattacharjee, G.P. (1973) Algorithm AS 64: Inverse of the Incomplete Beta Function Ratio. /Journal of the Royal Statistical Society. Series C (Applied Statistics)/ Vol. 22, No. 3 (1973), pp. 411-414 http://www.jstor.org/pss/2346798
• Temme, N.M. (1992) Asymptotic inversion of the incomplete beta function. /Journal of Computational and Applied Mathematics 41(1992) 145-157.
• Temme, N.M. (1994) A set of algorithms for the incomplete gamma functions. /Probability in the Engineering and Informational Sciences/, 8, 1994, 291-307. Printed in the U.S.A.