PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

/tests/Tests/Parametric.hs

http://github.com/bos/statistics
Haskell | 102 lines | 79 code | 6 blank | 17 comment | 0 complexity | db274fa3f7876eccc0f887fefc575bb0 MD5 | raw file
  1module Tests.Parametric (tests) where
  2
  3import Data.Maybe (fromJust)
  4import Statistics.Test.StudentT
  5import Statistics.Types
  6import qualified Data.Vector.Unboxed as U
  7import Test.Tasty (testGroup)
  8import Tests.Helpers  (testEquality)
  9import qualified Test.Tasty as Tst
 10
 11tests :: Tst.TestTree
 12tests = testGroup "Parametric tests" studentTTests
 13
 14-- 2 samples x 20 obs data
 15--
 16-- Both samples are samples from normal distributions with the same variance (= 1.0),
 17-- but their means are different (0.0 and 0.5, respectively).
 18--
 19-- You can reproduce the data with R (3.1.0) as follows:
 20--   set.seed(0)
 21--   sample1 = rnorm(20)
 22--   sample2 = rnorm(20, 0.5)
 23--   student = t.test(sample1, sample2, var.equal=T)
 24--   welch = t.test(sample1, sample2)
 25--   paired = t.test(sample1, sample2, paired=T)
 26sample1, sample2 :: U.Vector Double
 27sample1 = U.fromList [
 28  1.262954284880793e+00,
 29 -3.262333607056494e-01,
 30  1.329799262922501e+00,
 31  1.272429321429405e+00,
 32  4.146414344564082e-01,
 33 -1.539950041903710e+00,
 34 -9.285670347135381e-01,
 35 -2.947204467905602e-01,
 36 -5.767172747536955e-03,
 37  2.404653388857951e+00,
 38  7.635934611404596e-01,
 39 -7.990092489893682e-01,
 40 -1.147657009236351e+00,
 41 -2.894615736882233e-01,
 42 -2.992151178973161e-01,
 43 -4.115108327950670e-01,
 44  2.522234481561323e-01,
 45 -8.919211272845686e-01,
 46  4.356832993557186e-01,
 47 -1.237538421929958e+00]
 48sample2 = U.fromList [
 49  2.757321147216907e-01,
 50  8.773956459817011e-01,
 51  6.333363608148415e-01,
 52  1.304189509744908e+00,
 53  4.428932256161913e-01,
 54  1.003607972233726e+00,
 55  1.585769362145687e+00,
 56 -1.909538396968303e-01,
 57 -7.845993538721883e-01,
 58  5.467261721883520e-01,
 59  2.642934435604988e-01,
 60 -4.288825501025439e-02,
 61  6.668968254321778e-02,
 62 -1.494716467962331e-01,
 63  1.226750747385451e+00,
 64  1.651911754087200e+00,
 65  1.492160365445798e+00,
 66  7.048689050811874e-02,
 67  1.738304100853380e+00,
 68  2.206537181457307e-01]
 69
 70
 71testTTest :: String
 72          -> PValue Double
 73          -> Test d
 74          -> [Tst.TestTree]
 75testTTest name pVal test =
 76  [ testEquality name (isSignificant pVal test) NotSignificant
 77  , testEquality name (isSignificant (mkPValue $ pValue pVal + 1e-5) test)
 78    Significant
 79  ]
 80  
 81studentTTests :: [Tst.TestTree]
 82studentTTests = concat
 83  [ -- R: t.test(sample1, sample2, alt="two.sided", var.equal=T)
 84    testTTest "two-sample t-test SamplesDiffer Student"
 85      (mkPValue 0.03410) (fromJust $ studentTTest SamplesDiffer sample1 sample2)
 86    -- R: t.test(sample1, sample2, alt="two.sided", var.equal=F)
 87  , testTTest "two-sample t-test SamplesDiffer Welch"
 88      (mkPValue 0.03483) (fromJust $ welchTTest SamplesDiffer sample1 sample2)
 89    -- R: t.test(sample1, sample2, alt="two.sided", paired=T)
 90  , testTTest "two-sample t-test SamplesDiffer Paired"
 91      (mkPValue 0.03411) (fromJust $ pairedTTest SamplesDiffer sample12)
 92    -- R: t.test(sample1, sample2, alt="less", var.equal=T)
 93  , testTTest "two-sample t-test BGreater Student"
 94      (mkPValue 0.01705) (fromJust $ studentTTest BGreater sample1 sample2)
 95    -- R: t.test(sample1, sample2, alt="less", var.equal=F)
 96  , testTTest "two-sample t-test BGreater Welch"
 97      (mkPValue 0.01741) (fromJust $ welchTTest BGreater sample1 sample2)
 98    -- R: t.test(sample1, sample2, alt="less", paired=F)
 99  , testTTest "two-sample t-test BGreater Paired"
100      (mkPValue 0.01705) (fromJust $ pairedTTest BGreater sample12)
101  ]
102  where sample12 = U.zip sample1 sample2