module Statistics.Distribution.Exponential
(
ExponentialDistribution
, exponential
, exponentialFromSample
, edLambda
) where
import Data.Typeable (Typeable)
import qualified Statistics.Distribution as D
import qualified Statistics.Sample as S
import Statistics.Types (Sample)
newtype ExponentialDistribution = ED {
edLambda :: Double
} deriving (Eq, Read, Show, Typeable)
instance D.Distribution ExponentialDistribution where
cumulative = cumulative
complCumulative = complCumulative
instance D.ContDistr ExponentialDistribution where
density = density
quantile = quantile
instance D.Mean ExponentialDistribution where
mean (ED l) = 1 / l
instance D.Variance ExponentialDistribution where
variance (ED l) = 1 / (l * l)
instance D.MaybeMean ExponentialDistribution where
maybeMean = Just . D.mean
instance D.MaybeVariance ExponentialDistribution where
maybeStdDev = Just . D.stdDev
maybeVariance = Just . D.variance
cumulative :: ExponentialDistribution -> Double -> Double
cumulative (ED l) x | x <= 0 = 0
| otherwise = 1 exp (l * x)
complCumulative :: ExponentialDistribution -> Double -> Double
complCumulative (ED l) x | x <= 0 = 1
| otherwise = exp (l * x)
density :: ExponentialDistribution -> Double -> Double
density (ED l) x | x < 0 = 0
| otherwise = l * exp (l * x)
quantile :: ExponentialDistribution -> Double -> Double
quantile (ED l) p
| p == 1 = 1 / 0
| p >= 0 && p < 1 = log (1 p) / l
| otherwise =
error $ "Statistics.Distribution.Exponential.quantile: p must be in [0,1] range. Got: "++show p
exponential :: Double
-> ExponentialDistribution
exponential l
| l <= 0 =
error $ "Statistics.Distribution.Exponential.exponential: scale parameter must be positive. Got " ++ show l
| otherwise = ED l
exponentialFromSample :: Sample -> ExponentialDistribution
exponentialFromSample = ED . S.mean