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

Copyright | (c) 2009 Bryan O'Sullivan
License | BSD3
Maintainer | bos@serpentine.com
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell98

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
  - logProbability :: d -> Int -> Double
- class Distribution d => ContDistr d where
  - density :: d -> Double -> Double
  - quantile :: d -> Double -> Double
  - logDensity :: 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 => MaybeEntropy d where
  - maybeEntropy :: d -> Maybe Double
- class MaybeEntropy d => Entropy d where
  - entropy :: 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.

Minimal complete definition

cumulative

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). Cumulative should be defined for infinities as well:

    cumulative d +∞ = 1
    cumulative d -∞ = 0

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 Source |
Distribution BinomialDistribution Source |
Distribution PoissonDistribution Source |
Distribution CauchyDistribution Source |
Distribution ChiSquared Source |
Distribution ExponentialDistribution Source |
Distribution GammaDistribution Source |
Distribution GeometricDistribution0 Source |
Distribution GeometricDistribution Source |
Distribution HypergeometricDistribution Source |
Distribution NormalDistribution Source |
Distribution StudentT Source |
Distribution UniformDistribution Source |
Distribution FDistribution Source |
Distribution d => Distribution (LinearTransform d) Source |

class Distribution d => DiscreteDistr d where Source

Discrete probability distribution.

Minimal complete definition

Nothing

Methods

probability :: d -> Int -> Double Source

Probability of n-th outcome.

logProbability :: d -> Int -> Double Source

Logarithm of probability of n-th outcome

Instances

DiscreteDistr BinomialDistribution Source |
DiscreteDistr PoissonDistribution Source |
DiscreteDistr GeometricDistribution0 Source |
DiscreteDistr GeometricDistribution Source |
DiscreteDistr HypergeometricDistribution Source |

class Distribution d => ContDistr d where Source

Continuous probability distributuion.

Minimal complete definition is `quantile` and either `density` or `logDensity`.

Minimal complete definition

quantile

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`

logDensity :: d -> Double -> Double Source

Natural logarithm of density.

Instances

ContDistr BetaDistribution Source |
ContDistr CauchyDistribution Source |
ContDistr ChiSquared Source |
ContDistr ExponentialDistribution Source |
ContDistr GammaDistribution Source |
ContDistr NormalDistribution Source |
ContDistr StudentT Source |
ContDistr UniformDistribution Source |
ContDistr FDistribution Source |
ContDistr d => ContDistr (LinearTransform d) Source |

## 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 Source |
MaybeMean BinomialDistribution Source |
MaybeMean PoissonDistribution Source |
MaybeMean ChiSquared Source |
MaybeMean ExponentialDistribution Source |
MaybeMean GammaDistribution Source |
MaybeMean GeometricDistribution0 Source |
MaybeMean GeometricDistribution Source |
MaybeMean HypergeometricDistribution Source |
MaybeMean NormalDistribution Source |
MaybeMean StudentT Source |
MaybeMean UniformDistribution Source |
MaybeMean FDistribution Source |
MaybeMean d => MaybeMean (LinearTransform d) Source |

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 Source |
Mean BinomialDistribution Source |
Mean PoissonDistribution Source |
Mean ChiSquared Source |
Mean ExponentialDistribution Source |
Mean GammaDistribution Source |
Mean GeometricDistribution0 Source |
Mean GeometricDistribution Source |
Mean HypergeometricDistribution Source |
Mean NormalDistribution Source |
Mean UniformDistribution Source |
Mean d => Mean (LinearTransform d) Source |

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`

Minimal complete definition

Nothing

Methods

maybeVariance :: d -> Maybe Double Source

maybeStdDev :: d -> Maybe Double Source

Instances

MaybeVariance BetaDistribution Source |
MaybeVariance BinomialDistribution Source |
MaybeVariance PoissonDistribution Source |
MaybeVariance ChiSquared Source |
MaybeVariance ExponentialDistribution Source |
MaybeVariance GammaDistribution Source |
MaybeVariance GeometricDistribution0 Source |
MaybeVariance GeometricDistribution Source |
MaybeVariance HypergeometricDistribution Source |
MaybeVariance NormalDistribution Source |
MaybeVariance StudentT Source |
MaybeVariance UniformDistribution Source |
MaybeVariance FDistribution Source |
MaybeVariance d => MaybeVariance (LinearTransform d) Source |

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`

Minimal complete definition

Nothing

Methods

variance :: d -> Double Source

stdDev :: d -> Double Source

Instances

Variance BetaDistribution Source |
Variance BinomialDistribution Source |
Variance PoissonDistribution Source |
Variance ChiSquared Source |
Variance ExponentialDistribution Source |
Variance GammaDistribution Source |
Variance GeometricDistribution0 Source |
Variance GeometricDistribution Source |
Variance HypergeometricDistribution Source |
Variance NormalDistribution Source |
Variance UniformDistribution Source |
Variance d => Variance (LinearTransform d) Source |

class Distribution d => MaybeEntropy d where Source

Type class for distributions with entropy, meaning Shannon entropy in the case of a discrete distribution, or differential entropy in the case of a continuous one. `maybeEntropy` should return `Nothing` if entropy is undefined for the chosen parameter values.

Methods

maybeEntropy :: d -> Maybe Double Source

Returns the entropy of a distribution, in nats, if such is defined.

Instances

MaybeEntropy BetaDistribution Source |
MaybeEntropy BinomialDistribution Source |
MaybeEntropy PoissonDistribution Source |
MaybeEntropy CauchyDistribution Source |
MaybeEntropy ChiSquared Source |
MaybeEntropy ExponentialDistribution Source |
MaybeEntropy GammaDistribution Source |
MaybeEntropy GeometricDistribution0 Source |
MaybeEntropy GeometricDistribution Source |
MaybeEntropy HypergeometricDistribution Source |
MaybeEntropy NormalDistribution Source |
MaybeEntropy StudentT Source |
MaybeEntropy UniformDistribution Source |
MaybeEntropy FDistribution Source |
(MaybeEntropy d, DiscreteDistr d) => MaybeEntropy (LinearTransform d) Source |

class MaybeEntropy d => Entropy d where Source

Type class for distributions with entropy, meaning Shannon entropy in the case of a discrete distribution, or differential entropy in the case of a continuous one. If the distribution has well-defined entropy for all valid parameter values then it should be an instance of this type class.

Methods

entropy :: d -> Double Source

Returns the entropy of a distribution, in nats.

Instances

Entropy BetaDistribution Source |
Entropy BinomialDistribution Source |
Entropy PoissonDistribution Source |
Entropy CauchyDistribution Source |
Entropy ChiSquared Source |
Entropy ExponentialDistribution Source |
Entropy GeometricDistribution0 Source |
Entropy GeometricDistribution Source |
Entropy HypergeometricDistribution Source |
Entropy NormalDistribution Source |
Entropy StudentT Source |
Entropy UniformDistribution Source |
Entropy FDistribution Source |
(Entropy d, DiscreteDistr d) => Entropy (LinearTransform d) Source |

## 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 Source |
ContGen CauchyDistribution Source |
ContGen ChiSquared Source |
ContGen ExponentialDistribution Source |
ContGen GammaDistribution Source |
ContGen GeometricDistribution0 Source |
ContGen GeometricDistribution Source |
ContGen NormalDistribution Source |
ContGen StudentT Source |
ContGen UniformDistribution Source |
ContGen FDistribution Source |
ContGen d => ContGen (LinearTransform d) Source |

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

Instances

DiscreteGen GeometricDistribution0 Source |
DiscreteGen GeometricDistribution 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.