<commit_before>{-# LANGUAGE DeriveDataTypeable #-}
-- |
-- Module    : Statistics.Distribution.Gamma
-- Copyright : (c) 2009 <NAME>
-- License   : BSD3
--
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : portable
--
-- The gamma distribution.  This is a continuous probability
-- distribution with two parameters, /k/ and &#977;. If /k/ is
-- integral, the distribution represents the sum of /k/ independent
-- exponentially distributed random variables, each of which has a
-- mean of &#977;.

module Statistics.Distribution.Gamma
    (
      GammaDistribution
    -- * Constructors
    , gammaDistr
    -- * Accessors
    , gdShape
    , gdScale
    ) where

import Data.Typeable (Typeable)
import Statistics.Constants (m_huge)
import Statistics.Math (incompleteGamma, logGamma)
import qualified Statistics.Distribution as D

-- | The gamma distribution.
data GammaDistribution = GD {
      gdShape :: {-# UNPACK #-} !Double -- ^ Shape parameter, /k/.
    , gdScale :: {-# UNPACK #-} !Double -- ^ Scale parameter, &#977;.
    } deriving (Eq, Read, Show, Typeable)

gammaDistr :: Double -> Double -> GammaDistribution
gammaDistr = GD
{-# INLINE gammaDistr #-}

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)
    {-# INLINE variance #-}

instance D.Mean GammaDistribution where
    mean (GD a l) = a / l
    {-# INLINE mean #-}

density :: GammaDistribution -> Double -> Double
density (GD a l) x = x ** (a-1) * exp (-x/l) / (exp (logGamma a) * l ** a)
{-# INLINE density #-}

cumulative :: GammaDistribution -> Double -> Double
cumulative (GD a l) x = incompleteGamma a (x/l) / exp (logGamma a)
{-# INLINE cumulative #-}

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
{-# INLINE quantile #-}<commit_msg>Fix distibution for Gamma  * incompleteGamma is already normalized. There is no need to divide by gamma twice  * Check when x <= 0 in cumulative and density<commit_after>{-# LANGUAGE DeriveDataTypeable #-}
-- |
-- Module    : Statistics.Distribution.Gamma
-- Copyright : (c) 2009 <NAME>
-- License   : BSD3
--
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : portable
--
-- The gamma distribution.  This is a continuous probability
-- distribution with two parameters, /k/ and &#977;. If /k/ is
-- integral, the distribution represents the sum of /k/ independent
-- exponentially distributed random variables, each of which has a
-- mean of &#977;.

module Statistics.Distribution.Gamma
    (
      GammaDistribution
    -- * Constructors
    , gammaDistr
    -- * Accessors
    , gdShape
    , gdScale
    ) where

import Data.Typeable (Typeable)
import Statistics.Constants (m_huge)
import Statistics.Math (incompleteGamma, logGamma)
import qualified Statistics.Distribution as D

-- | The gamma distribution.
data GammaDistribution = GD {
      gdShape :: {-# UNPACK #-} !Double -- ^ Shape parameter, /k/.
    , gdScale :: {-# UNPACK #-} !Double -- ^ Scale parameter, &#977;.
    } deriving (Eq, Read, Show, Typeable)

gammaDistr :: Double -> Double -> GammaDistribution
gammaDistr = GD
{-# INLINE gammaDistr #-}

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)
    {-# INLINE variance #-}

instance D.Mean GammaDistribution where
    mean (GD a l) = a / l
    {-# INLINE mean #-}

density :: GammaDistribution -> Double -> Double
density (GD a l) x
  | x <= 0    = 0
  | otherwise = x ** (a-1) * exp (-x/l) / (exp (logGamma a) * l ** a)
{-# INLINE density #-}

cumulative :: GammaDistribution -> Double -> Double
cumulative (GD k l) x
  | x <= 0    = 0
  | otherwise = incompleteGamma k (x/l)
{-# INLINE cumulative #-}

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
{-# INLINE quantile #-}
{'fraction_non_alphanumeric': 0.12675397567820393, 'fraction_numerical': 0.011459307764265668, 'mean_word_length': 2.9023722627737225, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499306', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1301, 'n_words': 449}