Copyright | (c) 2009 Bryan O'Sullivan |
---|---|
License | BSD3 |
Maintainer | bos@serpentine.com |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Type classes for probability distributions
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
- class Distribution d => MaybeMean d where
- class MaybeMean d => Mean d where
- class MaybeMean d => MaybeVariance d where
- maybeVariance :: d -> Maybe Double
- maybeStdDev :: d -> Maybe Double
- class (Mean d, MaybeVariance d) => Variance d where
- class Distribution d => MaybeEntropy d where
- maybeEntropy :: d -> Maybe Double
- class MaybeEntropy d => Entropy d where
- class FromSample d a where
- fromSample :: Vector v a => v a -> Maybe d
- class Distribution d => ContGen d where
- genContVar :: StatefulGen g m => d -> g -> m Double
- class (DiscreteDistr d, ContGen d) => DiscreteGen d where
- genDiscreteVar :: StatefulGen g m => d -> g -> m Int
- genContinuous :: (ContDistr d, StatefulGen g m) => d -> g -> 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 continuous distributions.
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 distribution:
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
class Distribution d => DiscreteDistr d where Source #
Discrete probability distribution.
probability :: d -> Int -> Double Source #
Probability of n-th outcome.
logProbability :: d -> Int -> Double Source #
Logarithm of probability of n-th outcome
Instances
class Distribution d => ContDistr d where Source #
Continuous probability distribution.
Minimal complete definition is quantile
and either density
or
logDensity
.
(density | logDensity), (quantile | complQuantile)
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
logDensity :: d -> Double -> Double Source #
Natural logarithm of density.
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
complQuantile :: d -> Double -> Double Source #
1-complement of quantile
:
complQuantile x ≡ quantile (1 - x)
Instances
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
Instances
class MaybeMean d => Mean d where Source #
Type class for distributions with mean. If a distribution has finite mean for all valid values of parameters it should be instance of this type class.
Instances
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
maybeVariance :: d -> Maybe Double Source #
maybeStdDev :: d -> Maybe Double Source #
Instances
class (Mean d, MaybeVariance d) => Variance d where Source #
Type class for distributions with variance. If distribution have finite variance for all valid parameter values it should be instance of this type class.
Instances
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.
maybeEntropy :: d -> Maybe Double Source #
Returns the entropy of a distribution, in nats, if such is defined.
Instances
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.
Instances
class FromSample d a where Source #
Estimate distribution from sample. First parameter in sample is distribution type and second is element type.
fromSample :: Vector v a => v a -> Maybe d Source #
Estimate distribution from sample. Returns Nothing
if there is
not enough data, or if no usable fit results from the method
used, e.g., the estimated distribution parameters would be
invalid or inaccurate.
Instances
FromSample ExponentialDistribution Double Source # | Create exponential distribution from sample. Estimates the rate
with the maximum likelihood estimator, which is biased. Returns
|
Defined in Statistics.Distribution.Exponential fromSample :: Vector v Double => v Double -> Maybe ExponentialDistribution Source # | |
FromSample LaplaceDistribution Double Source # | Create Laplace distribution from sample. The location is estimated as the median of the sample, and the scale as the mean absolute deviation of the median. |
Defined in Statistics.Distribution.Laplace fromSample :: Vector v Double => v Double -> Maybe LaplaceDistribution Source # | |
FromSample LognormalDistribution Double Source # | Variance is estimated using maximum likelihood method (biased estimation) over the log of the data. Returns |
Defined in Statistics.Distribution.Lognormal fromSample :: Vector v Double => v Double -> Maybe LognormalDistribution Source # | |
FromSample NormalDistribution Double Source # | Variance is estimated using maximum likelihood method (biased estimation). Returns |
Defined in Statistics.Distribution.Normal fromSample :: Vector v Double => v Double -> Maybe NormalDistribution Source # | |
FromSample WeibullDistribution Double Source # | Uses an approximation based on the mean and standard deviation in
Returns |
Defined in Statistics.Distribution.Weibull fromSample :: Vector v Double => v Double -> Maybe WeibullDistribution Source # |
Random number generation
class Distribution d => ContGen d where Source #
Generate discrete random variates which have given distribution.
genContVar :: StatefulGen g m => d -> g -> m Double Source #
Instances
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
genDiscreteVar :: StatefulGen g m => d -> g -> m Int Source #
Instances
DiscreteGen DiscreteUniform Source # | |
Defined in Statistics.Distribution.DiscreteUniform genDiscreteVar :: StatefulGen g m => DiscreteUniform -> g -> m Int Source # | |
DiscreteGen GeometricDistribution Source # | |
Defined in Statistics.Distribution.Geometric genDiscreteVar :: StatefulGen g m => GeometricDistribution -> g -> m Int Source # | |
DiscreteGen GeometricDistribution0 Source # | |
Defined in Statistics.Distribution.Geometric genDiscreteVar :: StatefulGen g m => GeometricDistribution0 -> g -> m Int Source # |
genContinuous :: (ContDistr d, StatefulGen g m) => d -> g -> m Double Source #
Generate variates from continuous distribution using inverse transform rule.
Helper functions
:: 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.