module Statistics.Distribution.Gamma
(
GammaDistribution
, gammaDistr
, gdShape
, gdScale
) where
import Data.Typeable (Typeable)
import Statistics.Constants (m_huge)
import Statistics.Math (incompleteGamma, logGamma)
import qualified Statistics.Distribution as D
data GammaDistribution = GD {
gdShape :: !Double
, gdScale :: !Double
} deriving (Eq, Read, Show, Typeable)
gammaDistr :: Double -> Double -> GammaDistribution
gammaDistr = GD
instance D.Distribution GammaDistribution where
cumulative = cumulative
instance D.ContDistr GammaDistribution where
density = density
quantile = quantile
instance D.Variance GammaDistribution where
variance (GD a l) = a / (l * l)
instance D.Mean GammaDistribution where
mean (GD a l) = a / l
density :: GammaDistribution -> Double -> Double
density (GD a l) x
| x <= 0 = 0
| otherwise = x ** (a1) * exp (x/l) / (exp (logGamma a) * l ** a)
cumulative :: GammaDistribution -> Double -> Double
cumulative (GD k l) x
| x <= 0 = 0
| otherwise = incompleteGamma k (x/l)
quantile :: GammaDistribution -> Double -> Double
quantile d p
| p == 0 = 1/0
| p == 1 = 1/0
| otherwise = D.findRoot d p (gdShape d) 0 m_huge