Statistics.Distribution

statistics-0.13.3.0: 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

Type classes for probability distributions

Synopsis

- class Distribution d where
- class Distribution d => DiscreteDistr d where
- class Distribution d => ContDistr d where
- class Distribution d => MaybeMean d where
- class MaybeMean d => Mean d where
- class MaybeMean d => MaybeVariance d where
- class (Mean d, MaybeVariance d) => Variance d where
- class Distribution d => MaybeEntropy d where
- class MaybeEntropy d => Entropy d where
- class Distribution d => ContGen d where
- class (DiscreteDistr d, ContGen d) => DiscreteGen d where
- 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 #

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 #

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 #

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 # |
Methods cumulative :: BetaDistribution -> Double -> Double # complCumulative :: BetaDistribution -> Double -> Double #
Distribution BinomialDistribution # |
Methods cumulative :: BinomialDistribution -> Double -> Double # complCumulative :: BinomialDistribution -> Double -> Double #
Distribution PoissonDistribution # |
Methods cumulative :: PoissonDistribution -> Double -> Double # complCumulative :: PoissonDistribution -> Double -> Double #
Distribution CauchyDistribution # |
Methods cumulative :: CauchyDistribution -> Double -> Double # complCumulative :: CauchyDistribution -> Double -> Double #
Distribution ChiSquared # |
Methods cumulative :: ChiSquared -> Double -> Double # complCumulative :: ChiSquared -> Double -> Double #
Distribution ExponentialDistribution # |
Methods cumulative :: ExponentialDistribution -> Double -> Double # complCumulative :: ExponentialDistribution -> Double -> Double #
Distribution GammaDistribution # |
Methods cumulative :: GammaDistribution -> Double -> Double # complCumulative :: GammaDistribution -> Double -> Double #
Distribution GeometricDistribution0 # |
Methods cumulative :: GeometricDistribution0 -> Double -> Double # complCumulative :: GeometricDistribution0 -> Double -> Double #
Distribution GeometricDistribution # |
Methods cumulative :: GeometricDistribution -> Double -> Double # complCumulative :: GeometricDistribution -> Double -> Double #
Distribution HypergeometricDistribution # |
Methods cumulative :: HypergeometricDistribution -> Double -> Double # complCumulative :: HypergeometricDistribution -> Double -> Double #
Distribution NormalDistribution # |
Methods cumulative :: NormalDistribution -> Double -> Double # complCumulative :: NormalDistribution -> Double -> Double #
Distribution StudentT # |
Methods cumulative :: StudentT -> Double -> Double # complCumulative :: StudentT -> Double -> Double #
Distribution UniformDistribution # |
Methods cumulative :: UniformDistribution -> Double -> Double # complCumulative :: UniformDistribution -> Double -> Double #
Distribution FDistribution # |
Methods cumulative :: FDistribution -> Double -> Double # complCumulative :: FDistribution -> Double -> Double #
Distribution LaplaceDistribution # |
Methods cumulative :: LaplaceDistribution -> Double -> Double # complCumulative :: LaplaceDistribution -> Double -> Double #
Distribution d => Distribution (LinearTransform d) # |
Methods cumulative :: LinearTransform d -> Double -> Double # complCumulative :: LinearTransform d -> Double -> Double #

class Distribution d => DiscreteDistr d where #

Discrete probability distribution.

Methods

probability :: d -> Int -> Double #

Probability of n-th outcome.

logProbability :: d -> Int -> Double #

Logarithm of probability of n-th outcome

Instances

DiscreteDistr BinomialDistribution # |
Methods probability :: BinomialDistribution -> Int -> Double # logProbability :: BinomialDistribution -> Int -> Double #
DiscreteDistr PoissonDistribution # |
Methods probability :: PoissonDistribution -> Int -> Double # logProbability :: PoissonDistribution -> Int -> Double #
DiscreteDistr GeometricDistribution0 # |
Methods probability :: GeometricDistribution0 -> Int -> Double # logProbability :: GeometricDistribution0 -> Int -> Double #
DiscreteDistr GeometricDistribution # |
Methods probability :: GeometricDistribution -> Int -> Double # logProbability :: GeometricDistribution -> Int -> Double #
DiscreteDistr HypergeometricDistribution # |
Methods probability :: HypergeometricDistribution -> Int -> Double # logProbability :: HypergeometricDistribution -> Int -> Double #

class Distribution d => ContDistr d where #

Continuous probability distributuion.

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

Minimal complete definition

quantile

Methods

density :: d -> Double -> Double #

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 #

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 #

Natural logarithm of density.

Instances

ContDistr BetaDistribution # |
Methods density :: BetaDistribution -> Double -> Double # quantile :: BetaDistribution -> Double -> Double # logDensity :: BetaDistribution -> Double -> Double #
ContDistr CauchyDistribution # |
Methods density :: CauchyDistribution -> Double -> Double # quantile :: CauchyDistribution -> Double -> Double # logDensity :: CauchyDistribution -> Double -> Double #
ContDistr ChiSquared # |
Methods density :: ChiSquared -> Double -> Double # quantile :: ChiSquared -> Double -> Double # logDensity :: ChiSquared -> Double -> Double #
ContDistr ExponentialDistribution # |
Methods density :: ExponentialDistribution -> Double -> Double # quantile :: ExponentialDistribution -> Double -> Double # logDensity :: ExponentialDistribution -> Double -> Double #
ContDistr GammaDistribution # |
Methods density :: GammaDistribution -> Double -> Double # quantile :: GammaDistribution -> Double -> Double # logDensity :: GammaDistribution -> Double -> Double #
ContDistr NormalDistribution # |
Methods density :: NormalDistribution -> Double -> Double # quantile :: NormalDistribution -> Double -> Double # logDensity :: NormalDistribution -> Double -> Double #
ContDistr StudentT # |
Methods density :: StudentT -> Double -> Double # quantile :: StudentT -> Double -> Double # logDensity :: StudentT -> Double -> Double #
ContDistr UniformDistribution # |
Methods density :: UniformDistribution -> Double -> Double # quantile :: UniformDistribution -> Double -> Double # logDensity :: UniformDistribution -> Double -> Double #
ContDistr FDistribution # |
Methods density :: FDistribution -> Double -> Double # quantile :: FDistribution -> Double -> Double # logDensity :: FDistribution -> Double -> Double #
ContDistr LaplaceDistribution # |
Methods density :: LaplaceDistribution -> Double -> Double # quantile :: LaplaceDistribution -> Double -> Double # logDensity :: LaplaceDistribution -> Double -> Double #
ContDistr d => ContDistr (LinearTransform d) # |
Methods density :: LinearTransform d -> Double -> Double # quantile :: LinearTransform d -> Double -> Double # logDensity :: LinearTransform d -> Double -> Double #

## Distribution statistics

class Distribution d => MaybeMean d where #

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

Minimal complete definition

maybeMean

Methods

maybeMean :: d -> Maybe Double #

Instances

MaybeMean BetaDistribution # |
Methods maybeMean :: BetaDistribution -> Maybe Double #
MaybeMean BinomialDistribution # |
Methods maybeMean :: BinomialDistribution -> Maybe Double #
MaybeMean PoissonDistribution # |
Methods maybeMean :: PoissonDistribution -> Maybe Double #
MaybeMean ChiSquared # |
Methods maybeMean :: ChiSquared -> Maybe Double #
MaybeMean ExponentialDistribution # |
Methods maybeMean :: ExponentialDistribution -> Maybe Double #
MaybeMean GammaDistribution # |
Methods maybeMean :: GammaDistribution -> Maybe Double #
MaybeMean GeometricDistribution0 # |
Methods maybeMean :: GeometricDistribution0 -> Maybe Double #
MaybeMean GeometricDistribution # |
Methods maybeMean :: GeometricDistribution -> Maybe Double #
MaybeMean HypergeometricDistribution # |
Methods maybeMean :: HypergeometricDistribution -> Maybe Double #
MaybeMean NormalDistribution # |
Methods maybeMean :: NormalDistribution -> Maybe Double #
MaybeMean StudentT # |
Methods maybeMean :: StudentT -> Maybe Double #
MaybeMean UniformDistribution # |
Methods maybeMean :: UniformDistribution -> Maybe Double #
MaybeMean FDistribution # |
Methods maybeMean :: FDistribution -> Maybe Double #
MaybeMean LaplaceDistribution # |
Methods maybeMean :: LaplaceDistribution -> Maybe Double #
MaybeMean d => MaybeMean (LinearTransform d) # |
Methods maybeMean :: LinearTransform d -> Maybe Double #

class MaybeMean d => Mean d where #

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.

Minimal complete definition

mean

Methods

mean :: d -> Double #

Instances

Mean BetaDistribution # |
Methods mean :: BetaDistribution -> Double #
Mean BinomialDistribution # |
Methods mean :: BinomialDistribution -> Double #
Mean PoissonDistribution # |
Methods mean :: PoissonDistribution -> Double #
Mean ChiSquared # |
Methods mean :: ChiSquared -> Double #
Mean ExponentialDistribution # |
Methods mean :: ExponentialDistribution -> Double #
Mean GammaDistribution # |
Methods mean :: GammaDistribution -> Double #
Mean GeometricDistribution0 # |
Methods mean :: GeometricDistribution0 -> Double #
Mean GeometricDistribution # |
Methods mean :: GeometricDistribution -> Double #
Mean HypergeometricDistribution # |
Methods mean :: HypergeometricDistribution -> Double #
Mean NormalDistribution # |
Methods mean :: NormalDistribution -> Double #
Mean UniformDistribution # |
Methods mean :: UniformDistribution -> Double #
Mean LaplaceDistribution # |
Methods mean :: LaplaceDistribution -> Double #
Mean d => Mean (LinearTransform d) # |
Methods mean :: LinearTransform d -> Double #

class MaybeMean d => MaybeVariance d where #

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 #

maybeStdDev :: d -> Maybe Double #

Instances

MaybeVariance BetaDistribution # |
Methods maybeVariance :: BetaDistribution -> Maybe Double # maybeStdDev :: BetaDistribution -> Maybe Double #
MaybeVariance BinomialDistribution # |
Methods maybeVariance :: BinomialDistribution -> Maybe Double # maybeStdDev :: BinomialDistribution -> Maybe Double #
MaybeVariance PoissonDistribution # |
Methods maybeVariance :: PoissonDistribution -> Maybe Double # maybeStdDev :: PoissonDistribution -> Maybe Double #
MaybeVariance ChiSquared # |
Methods maybeVariance :: ChiSquared -> Maybe Double # maybeStdDev :: ChiSquared -> Maybe Double #
MaybeVariance ExponentialDistribution # |
Methods maybeVariance :: ExponentialDistribution -> Maybe Double # maybeStdDev :: ExponentialDistribution -> Maybe Double #
MaybeVariance GammaDistribution # |
Methods maybeVariance :: GammaDistribution -> Maybe Double # maybeStdDev :: GammaDistribution -> Maybe Double #
MaybeVariance GeometricDistribution0 # |
Methods maybeVariance :: GeometricDistribution0 -> Maybe Double # maybeStdDev :: GeometricDistribution0 -> Maybe Double #
MaybeVariance GeometricDistribution # |
Methods maybeVariance :: GeometricDistribution -> Maybe Double # maybeStdDev :: GeometricDistribution -> Maybe Double #
MaybeVariance HypergeometricDistribution # |
Methods maybeVariance :: HypergeometricDistribution -> Maybe Double # maybeStdDev :: HypergeometricDistribution -> Maybe Double #
MaybeVariance NormalDistribution # |
Methods maybeVariance :: NormalDistribution -> Maybe Double # maybeStdDev :: NormalDistribution -> Maybe Double #
MaybeVariance StudentT # |
Methods maybeVariance :: StudentT -> Maybe Double # maybeStdDev :: StudentT -> Maybe Double #
MaybeVariance UniformDistribution # |
Methods maybeVariance :: UniformDistribution -> Maybe Double # maybeStdDev :: UniformDistribution -> Maybe Double #
MaybeVariance FDistribution # |
Methods maybeVariance :: FDistribution -> Maybe Double # maybeStdDev :: FDistribution -> Maybe Double #
MaybeVariance LaplaceDistribution # |
Methods maybeVariance :: LaplaceDistribution -> Maybe Double # maybeStdDev :: LaplaceDistribution -> Maybe Double #
MaybeVariance d => MaybeVariance (LinearTransform d) # |
Methods maybeVariance :: LinearTransform d -> Maybe Double # maybeStdDev :: LinearTransform d -> Maybe Double #

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

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 #

stdDev :: d -> Double #

Instances

Variance BetaDistribution # |
Methods variance :: BetaDistribution -> Double # stdDev :: BetaDistribution -> Double #
Variance BinomialDistribution # |
Methods variance :: BinomialDistribution -> Double # stdDev :: BinomialDistribution -> Double #
Variance PoissonDistribution # |
Methods variance :: PoissonDistribution -> Double # stdDev :: PoissonDistribution -> Double #
Variance ChiSquared # |
Methods variance :: ChiSquared -> Double # stdDev :: ChiSquared -> Double #
Variance ExponentialDistribution # |
Methods variance :: ExponentialDistribution -> Double # stdDev :: ExponentialDistribution -> Double #
Variance GammaDistribution # |
Methods variance :: GammaDistribution -> Double # stdDev :: GammaDistribution -> Double #
Variance GeometricDistribution0 # |
Methods variance :: GeometricDistribution0 -> Double # stdDev :: GeometricDistribution0 -> Double #
Variance GeometricDistribution # |
Methods variance :: GeometricDistribution -> Double # stdDev :: GeometricDistribution -> Double #
Variance HypergeometricDistribution # |
Methods variance :: HypergeometricDistribution -> Double # stdDev :: HypergeometricDistribution -> Double #
Variance NormalDistribution # |
Methods variance :: NormalDistribution -> Double # stdDev :: NormalDistribution -> Double #
Variance UniformDistribution # |
Methods variance :: UniformDistribution -> Double # stdDev :: UniformDistribution -> Double #
Variance LaplaceDistribution # |
Methods variance :: LaplaceDistribution -> Double # stdDev :: LaplaceDistribution -> Double #
Variance d => Variance (LinearTransform d) # |
Methods variance :: LinearTransform d -> Double # stdDev :: LinearTransform d -> Double #

class Distribution d => MaybeEntropy d where #

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.

Minimal complete definition

maybeEntropy

Methods

maybeEntropy :: d -> Maybe Double #

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

Instances

MaybeEntropy BetaDistribution # |
Methods maybeEntropy :: BetaDistribution -> Maybe Double #
MaybeEntropy BinomialDistribution # |
Methods maybeEntropy :: BinomialDistribution -> Maybe Double #
MaybeEntropy PoissonDistribution # |
Methods maybeEntropy :: PoissonDistribution -> Maybe Double #
MaybeEntropy CauchyDistribution # |
Methods maybeEntropy :: CauchyDistribution -> Maybe Double #
MaybeEntropy ChiSquared # |
Methods maybeEntropy :: ChiSquared -> Maybe Double #
MaybeEntropy ExponentialDistribution # |
Methods maybeEntropy :: ExponentialDistribution -> Maybe Double #
MaybeEntropy GammaDistribution # |
Methods maybeEntropy :: GammaDistribution -> Maybe Double #
MaybeEntropy GeometricDistribution0 # |
Methods maybeEntropy :: GeometricDistribution0 -> Maybe Double #
MaybeEntropy GeometricDistribution # |
Methods maybeEntropy :: GeometricDistribution -> Maybe Double #
MaybeEntropy HypergeometricDistribution # |
Methods maybeEntropy :: HypergeometricDistribution -> Maybe Double #
MaybeEntropy NormalDistribution # |
Methods maybeEntropy :: NormalDistribution -> Maybe Double #
MaybeEntropy StudentT # |
Methods maybeEntropy :: StudentT -> Maybe Double #
MaybeEntropy UniformDistribution # |
Methods maybeEntropy :: UniformDistribution -> Maybe Double #
MaybeEntropy FDistribution # |
Methods maybeEntropy :: FDistribution -> Maybe Double #
MaybeEntropy LaplaceDistribution # |
Methods maybeEntropy :: LaplaceDistribution -> Maybe Double #
(MaybeEntropy d, DiscreteDistr d) => MaybeEntropy (LinearTransform d) # |
Methods maybeEntropy :: LinearTransform d -> Maybe Double #

class MaybeEntropy d => Entropy d where #

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.

Minimal complete definition

entropy

Methods

entropy :: d -> Double #

Returns the entropy of a distribution, in nats.

Instances

Entropy BetaDistribution # |
Methods entropy :: BetaDistribution -> Double #
Entropy BinomialDistribution # |
Methods entropy :: BinomialDistribution -> Double #
Entropy PoissonDistribution # |
Methods entropy :: PoissonDistribution -> Double #
Entropy CauchyDistribution # |
Methods entropy :: CauchyDistribution -> Double #
Entropy ChiSquared # |
Methods entropy :: ChiSquared -> Double #
Entropy ExponentialDistribution # |
Methods entropy :: ExponentialDistribution -> Double #
Entropy GeometricDistribution0 # |
Methods entropy :: GeometricDistribution0 -> Double #
Entropy GeometricDistribution # |
Methods entropy :: GeometricDistribution -> Double #
Entropy HypergeometricDistribution # |
Methods entropy :: HypergeometricDistribution -> Double #
Entropy NormalDistribution # |
Methods entropy :: NormalDistribution -> Double #
Entropy StudentT # |
Methods entropy :: StudentT -> Double #
Entropy UniformDistribution # |
Methods entropy :: UniformDistribution -> Double #
Entropy FDistribution # |
Methods entropy :: FDistribution -> Double #
Entropy LaplaceDistribution # |
Methods entropy :: LaplaceDistribution -> Double #
(Entropy d, DiscreteDistr d) => Entropy (LinearTransform d) # |
Methods entropy :: LinearTransform d -> Double #

## Random number generation

class Distribution d => ContGen d where #

Generate discrete random variates which have given distribution.

Minimal complete definition

genContVar

Methods

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

Instances

ContGen BetaDistribution # |
Methods genContVar :: PrimMonad m => BetaDistribution -> Gen (PrimState m) -> m Double #
ContGen CauchyDistribution # |
Methods genContVar :: PrimMonad m => CauchyDistribution -> Gen (PrimState m) -> m Double #
ContGen ChiSquared # |
Methods genContVar :: PrimMonad m => ChiSquared -> Gen (PrimState m) -> m Double #
ContGen ExponentialDistribution # |
Methods genContVar :: PrimMonad m => ExponentialDistribution -> Gen (PrimState m) -> m Double #
ContGen GammaDistribution # |
Methods genContVar :: PrimMonad m => GammaDistribution -> Gen (PrimState m) -> m Double #
ContGen GeometricDistribution0 # |
Methods genContVar :: PrimMonad m => GeometricDistribution0 -> Gen (PrimState m) -> m Double #
ContGen GeometricDistribution # |
Methods genContVar :: PrimMonad m => GeometricDistribution -> Gen (PrimState m) -> m Double #
ContGen NormalDistribution # |
Methods genContVar :: PrimMonad m => NormalDistribution -> Gen (PrimState m) -> m Double #
ContGen StudentT # |
Methods genContVar :: PrimMonad m => StudentT -> Gen (PrimState m) -> m Double #
ContGen UniformDistribution # |
Methods genContVar :: PrimMonad m => UniformDistribution -> Gen (PrimState m) -> m Double #
ContGen FDistribution # |
Methods genContVar :: PrimMonad m => FDistribution -> Gen (PrimState m) -> m Double #
ContGen LaplaceDistribution # |
Methods genContVar :: PrimMonad m => LaplaceDistribution -> Gen (PrimState m) -> m Double #
ContGen d => ContGen (LinearTransform d) # |
Methods genContVar :: PrimMonad m => LinearTransform d -> Gen (PrimState m) -> m Double #

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

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

Minimal complete definition

genDiscreteVar

Methods

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

Instances

DiscreteGen GeometricDistribution0 # |
Methods genDiscreteVar :: PrimMonad m => GeometricDistribution0 -> Gen (PrimState m) -> m Int #
DiscreteGen GeometricDistribution # |
Methods genDiscreteVar :: PrimMonad m => GeometricDistribution -> Gen (PrimState m) -> m Int #

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

Generate variates from continous distribution using inverse transform rule.

# Helper functions

findRoot #

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 #

Sum probabilities in inclusive interval.