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

Portability | portable
Stability | experimental
Maintainer | bos@serpentine.com
Safe Haskell | None

Statistics.Distribution

Contents

- Type classes
  - Distribution statistics
  - Random number generation
- Helper functions

Description

Types classes for probability distrubutions

Synopsis

- class Distribution d where
  - cumulative :: d -> Double -> Double
  - complCumulative :: d -> Double -> Double
- class Distribution d => DiscreteDistr d where
  - probability :: d -> Int -> Double
- class Distribution d => ContDistr d where
  - density :: d -> Double -> Double
  - quantile :: d -> Double -> Double
- class Distribution d => MaybeMean d where
  - maybeMean :: d -> Maybe Double
- class MaybeMean d => Mean d where
  - mean :: d -> Double
- class MaybeMean d => MaybeVariance d where
  - maybeVariance :: d -> Maybe Double
  - maybeStdDev :: d -> Maybe Double
- class (Mean d, MaybeVariance d) => Variance d where
  - variance :: d -> Double
  - stdDev :: d -> Double
- class Distribution d => ContGen d where
  - genContVar :: PrimMonad m => d -> Gen (PrimState m) -> m Double
- class (DiscreteDistr d, ContGen d) => DiscreteGen d where
  - genDiscreteVar :: PrimMonad m => d -> Gen (PrimState m) -> m Int
- genContinous :: (ContDistr d, PrimMonad m) => d -> Gen (PrimState m) -> m Double
- findRoot :: ContDistr d => d -> Double -> Double -> Double -> Double -> Double
- sumProbabilities :: DiscreteDistr d => d -> Int -> Int -> Double

# Type classes

class Distribution d where Source

Type class common to all distributions. Only c.d.f. could be defined for both discrete and continous distributions.

Methods

cumulative :: d -> Double -> Double Source

Cumulative distribution function. The probability that a random variable X is less or equal than x, i.e. P(X≤x).

complCumulative :: d -> Double -> Double Source

One's complement of cumulative distibution:

     complCumulative d x = 1 - cumulative d x

It's useful when one is interested in P(X≥x) and expression on the right side begin to lose precision. This function have default implementation but implementors are encouraged to provide more precise implementation

Instances

Distribution BetaDistribution |
Distribution BinomialDistribution |
Distribution CauchyDistribution |
Distribution ChiSquared |
Distribution FDistribution |
Distribution GammaDistribution |
Distribution PoissonDistribution |
Distribution GeometricDistribution |
Distribution HypergeometricDistribution |
Distribution StudentT |
Distribution UniformDistribution |
Distribution ExponentialDistribution |
Distribution NormalDistribution |

class Distribution d => DiscreteDistr d where Source

Discrete probability distribution.

Methods

probability :: d -> Int -> Double Source

Probability of n-th outcome.

Instances

DiscreteDistr BinomialDistribution |
DiscreteDistr PoissonDistribution |
DiscreteDistr GeometricDistribution |
DiscreteDistr HypergeometricDistribution |

class Distribution d => ContDistr d where Source

Continuous probability distributuion

Methods

density :: d -> Double -> Double Source

Probability density function. Probability that random variable X lies in the infinitesimal interval [x,x+δx) equal to density(x)⋅δx

quantile :: d -> Double -> Double Source

Inverse of the cumulative distribution function. The value x for which P(X≤x) = p. If probability is outside of [0,1] range function should call `error`

Instances

ContDistr BetaDistribution |
ContDistr CauchyDistribution |
ContDistr ChiSquared |
ContDistr FDistribution |
ContDistr GammaDistribution |
ContDistr StudentT |
ContDistr UniformDistribution |
ContDistr ExponentialDistribution |
ContDistr NormalDistribution |

## Distribution statistics

class Distribution d => MaybeMean d where Source

Type class for distributions with mean. `maybeMean` should return `Nothing` if it's undefined for current value of data

Methods

maybeMean :: d -> Maybe Double Source

Instances

MaybeMean BetaDistribution |
MaybeMean BinomialDistribution |
MaybeMean ChiSquared |
MaybeMean FDistribution |
MaybeMean GammaDistribution |
MaybeMean PoissonDistribution |
MaybeMean GeometricDistribution |
MaybeMean HypergeometricDistribution |
MaybeMean StudentT |
MaybeMean UniformDistribution |
MaybeMean ExponentialDistribution |
MaybeMean NormalDistribution |

class MaybeMean d => Mean d where Source

Type class for distributions with mean. If distribution have finite mean for all valid values of parameters it should be instance of this type class.

Methods

mean :: d -> Double Source

Instances

Mean BetaDistribution |
Mean BinomialDistribution |
Mean ChiSquared |
Mean GammaDistribution |
Mean PoissonDistribution |
Mean GeometricDistribution |
Mean HypergeometricDistribution |
Mean UniformDistribution |
Mean ExponentialDistribution |
Mean NormalDistribution |

class MaybeMean d => MaybeVariance d where Source

Type class for distributions with variance. If variance is undefined for some parameter values both `maybeVariance` and `maybeStdDev` should return Nothing.

Minimal complete definition is `maybeVariance` or `maybeStdDev`

Methods

maybeVariance :: d -> Maybe Double Source

maybeStdDev :: d -> Maybe Double Source

Instances

MaybeVariance BetaDistribution |
MaybeVariance BinomialDistribution |
MaybeVariance ChiSquared |
MaybeVariance FDistribution |
MaybeVariance GammaDistribution |
MaybeVariance PoissonDistribution |
MaybeVariance GeometricDistribution |
MaybeVariance HypergeometricDistribution |
MaybeVariance StudentT |
MaybeVariance UniformDistribution |
MaybeVariance ExponentialDistribution |
MaybeVariance NormalDistribution |

class (Mean d, MaybeVariance d) => Variance d where Source

Type class for distributions with variance. If distibution have finite variance for all valid parameter values it should be instance of this type class.

Minimal complete definition is `variance` or `stdDev`

Methods

variance :: d -> Double Source

stdDev :: d -> Double Source

Instances

Variance BetaDistribution |
Variance BinomialDistribution |
Variance ChiSquared |
Variance GammaDistribution |
Variance PoissonDistribution |
Variance GeometricDistribution |
Variance HypergeometricDistribution |
Variance UniformDistribution |
Variance ExponentialDistribution |
Variance NormalDistribution |

## Random number generation

class Distribution d => ContGen d where Source

Generate discrete random variates which have given distribution.

Methods

genContVar :: PrimMonad m => d -> Gen (PrimState m) -> m Double Source

Instances

ContGen BetaDistribution |
ContGen CauchyDistribution |
ContGen ChiSquared |
ContGen FDistribution |
ContGen GammaDistribution |
ContGen StudentT |
ContGen UniformDistribution |
ContGen ExponentialDistribution |
ContGen NormalDistribution |

class (DiscreteDistr d, ContGen d) => DiscreteGen d where Source

Generate discrete random variates which have given distribution. `ContGen` is superclass because it's always possible to generate real-valued variates from integer values

Methods

genDiscreteVar :: PrimMonad m => d -> Gen (PrimState m) -> m Int Source

genContinous :: (ContDistr d, PrimMonad m) => d -> Gen (PrimState m) -> m Double Source

Generate variates from continous distribution using inverse transform rule.

# Helper functions

findRoot Source

Arguments

:: ContDistr d |
=> d | Distribution
-> Double | Probability p
-> Double | Initial guess
-> Double | Lower bound on interval
-> Double | Upper bound on interval
-> Double |

Approximate the value of X for which P(x>X)=p.

This method uses a combination of Newton-Raphson iteration and bisection with the given guess as a starting point. The upper and lower bounds specify the interval in which the probability distribution reaches the value p.

sumProbabilities :: DiscreteDistr d => d -> Int -> Int -> Double Source

Sum probabilities in inclusive interval.