module Statistics.Distribution.Gamma
(
GammaDistribution
, 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)
instance D.Distribution GammaDistribution where
probability = probability
cumulative = cumulative
inverse = inverse
instance D.Variance GammaDistribution where
variance (GD a l) = a / (l * l)
instance D.Mean GammaDistribution where
mean (GD a l) = a / l
probability :: GammaDistribution -> Double -> Double
probability (GD a l) x = x ** (a1) * exp (x/l) / (exp (logGamma a) * l ** a)
cumulative :: GammaDistribution -> Double -> Double
cumulative (GD a l) x = incompleteGamma a (x/l) / exp (logGamma a)
inverse :: GammaDistribution -> Double -> Double
inverse d p
| p == 0 = 1/0
| p == 1 = 1/0
| otherwise = D.findRoot d p (gdShape d) 0 m_huge