module Statistics.Test.ChiSquared (
chi2test
, TestType(..)
, TestResult(..)
) where
import Prelude hiding (sum)
import Statistics.Distribution
import Statistics.Distribution.ChiSquared
import Statistics.Function (square)
import Statistics.Sample.Internal (sum)
import Statistics.Test.Types
import qualified Data.Vector as V
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed as U
chi2test :: (G.Vector v (Int,Double), G.Vector v Double)
=> Double
-> Int
-> v (Int,Double)
-> TestResult
chi2test p ndf vec
| ndf < 0 = error $ "Statistics.Test.ChiSquare.chi2test: negative NDF " ++ show ndf
| n < 0 = error $ "Statistics.Test.ChiSquare.chi2test: too short data sample"
| p > 0 && p < 1 = significant $ complCumulative d chi2 < p
| otherwise = error $ "Statistics.Test.ChiSquare.chi2test: bad p-value: " ++ show p
where
n = G.length vec ndf 1
chi2 = sum $ G.map (\(o,e) -> square (fromIntegral o e) / e) vec
d = chiSquared n