statistics-0.10.0.1: A library of statistical types, data, and functions

Portability | portable
Stability | experimental
Maintainer | bos@serpentine.com

Statistics.Math

Contents

- Functions
  - Beta function
  - Chebyshev polynomials
  - Factorial
  - Gamma function
  - Logarithm
  - Stirling's approximation
- References

Description

Mathematical functions for statistics.

Synopsis

- choose :: Int -> Int -> Double
- logBeta :: Double -> Double -> Double
- incompleteBeta :: Double -> Double -> Double -> Double
- incompleteBeta_ :: Double -> Double -> Double -> Double -> Double
- invIncompleteBeta :: Double -> Double -> Double -> Double
- chebyshev :: Vector v Double => Double -> v Double -> Double
- chebyshevBroucke :: Vector v Double => Double -> v Double -> Double
- factorial :: Int -> Double
- logFactorial :: Int -> Double
- logGamma :: Double -> Double
- logGammaL :: Double -> Double
- incompleteGamma :: Double -> Double -> Double
- invIncompleteGamma :: Double -> Double -> Double
- log1p :: Double -> Double
- log2 :: Int -> Int
- stirlingError :: Double -> Double
- bd0 :: Double -> Double -> Double

# Functions

choose :: Int -> Int -> Double

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

Example:

     7 `choose` 3 == 35

## Beta function

logBeta :: Double -> Double -> Double

Compute the natural logarithm of the beta function.

incompleteBeta

Arguments

:: Double | p > 0
-> Double | q > 0
-> Double | x, must lie in [0,1] range
-> Double |

Regularized incomplete beta function. Uses algorithm AS63 by Majumder abd Bhattachrjee.

incompleteBeta_

Arguments

:: Double | logarithm of beta function
-> Double | p > 0
-> Double | q > 0
-> Double | x, must lie in [0,1] range
-> Double |

Regularized incomplete beta function. Same as `incompleteBeta` but also takes value of lo

invIncompleteBeta

Arguments

:: Double | p
-> Double | q
-> Double | a
-> Double |

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

## Chebyshev polynomials

A Chebyshev polynomial of the first kind is defined by the following recurrence:

     t 0 _ = 1
     t 1 x = x
     t n x = 2 * x * t (n-1) x - t (n-2) x

chebyshev

Arguments

:: Vector v Double |
=> Double | Parameter of each function.
-> v Double | Coefficients of each polynomial term, in increasing order.
-> Double |

Evaluate a Chebyshev polynomial of the first kind. Uses Clenshaw's algorithm.

chebyshevBroucke

Arguments

:: Vector v Double |
=> Double | Parameter of each function.
-> v Double | Coefficients of each polynomial term, in increasing order.
-> Double |

Evaluate a Chebyshev polynomial of the first kind. Uses Broucke's ECHEB algorithm, and his convention for coefficient handling, and so gives different results than `chebyshev` for the same inputs.

## Factorial

factorial :: Int -> Double

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 :: Int -> Double

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

## Gamma function

logGamma :: Double -> Double

Compute the logarithm of the gamma function Γ(x). Uses Algorithm AS 245 by Macleod.

Gives an accuracy of 10–12 significant decimal digits, except for small regions around x = 1 and x = 2, where the function goes to zero. For greater accuracy, use `logGammaL`.

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

logGammaL :: Double -> Double

Compute the logarithm of the gamma function, Γ(x). Uses a Lanczos approximation.

This function is slower than `logGamma`, but gives 14 or more significant decimal digits of accuracy, 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).

incompleteGamma

Arguments

:: Double | s
-> Double | x
-> Double |

Compute the normalized lower incomplete gamma function γ(s,x). Normalization means that γ(s,∞)=1. Uses Algorithm AS 239 by Shea.

invIncompleteGamma :: Double -> Double -> Double

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

     invIncompleteGamma s . incompleteGamma s = id

For `invIncompleteGamma s p` s must be positive and p must be in [0,1] range.

## Logarithm

log1p :: Double -> Double

Compute the natural logarithm of 1 + `x`. This is accurate even for values of `x` near zero, where use of `log(1+x)` would lose precision.

log2 :: Int -> Int

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

## Stirling's approximation

stirlingError :: Double -> Double

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

     stirlingError @n@ = @log(n!) - log(sqrt(2*pi*n)*(n/e)^n)

bd0

Arguments

:: Double

    x

-> Double

    np

-> Double |

Evaluate the deviance term `x log(x/np) + np - x`.

# References

- Broucke, R. (1973) Algorithm 446: Ten subroutines for the manipulation of Chebyshev series. Communications of the ACM 16(4):254–256. http://doi.acm.org/10.1145/362003.362037
- Clenshaw, C.W. (1962) Chebyshev series for mathematical functions. National Physical Laboratory Mathematical Tables 5, Her Majesty's Stationery Office, London.
- 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
- Shea, B. (1988) Algorithm AS 239: Chi-squared and incomplete gamma integral. Applied Statistics 37(3):466–473. http://www.jstor.org/stable/2347328
- K. L. Majumder, G. P. Bhattacharjee (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
- K. L. Majumder, G. P. Bhattacharjee (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
- G. W. Cran, K. J. Martin and G. E. Thomas (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