module Statistics.Distribution.ChiSquared (
ChiSquared
, chiSquared
, chiSquaredNDF
) where
import Data.Typeable (Typeable)
import Statistics.Constants (m_huge)
import Statistics.Math (incompleteGamma,logGamma)
import qualified Statistics.Distribution as D
newtype ChiSquared = ChiSquared Int
deriving (Show,Typeable)
chiSquaredNDF :: ChiSquared -> Int
chiSquaredNDF (ChiSquared ndf) = ndf
chiSquared :: Int -> ChiSquared
chiSquared x = ChiSquared x
instance D.Distribution ChiSquared where
cumulative = cumulative
instance D.ContDistr ChiSquared where
density = density
quantile = quantile
instance D.Mean ChiSquared where
mean (ChiSquared ndf) = fromIntegral ndf
instance D.Variance ChiSquared where
variance (ChiSquared ndf) = fromIntegral (2*ndf)
cumulative :: ChiSquared -> Double -> Double
cumulative chi x
| x <= 0 = 0
| otherwise = incompleteGamma (ndf/2) (x/2)
where
ndf = fromIntegral $ chiSquaredNDF chi
density :: ChiSquared -> Double -> Double
density chi x
| x <= 0 = 0
| otherwise = exp $ log x * (ndf2 1) x2 logGamma ndf2 log 2 * ndf2
where
ndf = fromIntegral $ chiSquaredNDF chi
ndf2 = ndf/2
x2 = x/2
quantile :: ChiSquared -> Double -> Double
quantile d@(ChiSquared ndf) p
| p == 0 = 1/0
| p == 1 = 1/0
| otherwise = D.findRoot d p (fromIntegral ndf) 0 m_huge