{-
Chi squared test of uniformity. Given a list of counts, integers
separated by whitespace on standard input, this program tests
the hypothesis that they were sampled from a discrete uniform
distribution.
Copyright 2020 Ken Takusagawa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
-}
{-# LANGUAGE ScopedTypeVariables, PackageImports #-}
module Main where {
import Data.Function((&));
import Control.Category((>>>));
import Prelude hiding((.),(>>));
import qualified Data.Maybe as Maybe;
import Statistics.Test.ChiSquared(chi2test,testSignificance,testStatistics);
import "statistics" Statistics.Types(PValue);
import qualified "vector" Data.Vector as Vector;
import qualified Statistics.Test.Types as Statistics;
import Statistics.Distribution.ChiSquared(ChiSquared);
main :: IO();
main = do {
(len,s,result) <- getContents >>= (words >>> map read >>> chi2 >>> return);
putStrLn $ "n " ++ show len;
putStrLn $ "sum " ++ show s;
putStrLn $ "chi^2 " ++ show (testStatistics result);
let { pvalue :: PValue Double ;
pvalue = testSignificance result };
print pvalue;
-- problem with testDistribution https://github.com/bos/statistics/issues/167
};
chi2 :: [Integer] -> (Int, Integer, Statistics.Test ChiSquared);
chi2 obs = let {
s :: Integer;
s = sum obs;
len :: Int;
len = length obs;
expected :: Double;
expected = fromInteger s / fromIntegral len;
f :: Integer -> (Int,Double);
f o = (fromInteger o,expected);
} in (len, s, map f obs & Vector.fromList & chi2test 0 & Maybe.fromJust) ;
} --end