Skip to content

Commit 393c7e1

Browse files
committed
redux
1 parent ce14b4b commit 393c7e1

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

Statistics/Test/KruskalWallis.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,17 @@ kruskalWallis samples = (nTot - 1) * numerator / denominator
6363
nTot = fromIntegral $ sumWith rsamples U.length
6464
-- Average rank of all samples
6565
avgRank = (nTot + 1) / 2
66-
--
66+
--
6767
numerator = sumWith rsamples $ \sample ->
68+
-- n represents the number of cases in the ith group
6869
let n = fromIntegral $ U.length sample
70+
-- n * deviation^2
71+
-- deviation is the (mean rank sum in the ith group - global mean rank sum)
6972
in n * square (mean sample - avgRank)
73+
-- total rank variance across groups
74+
-- NOTE: if all values are the same across all samples,
75+
-- then the rank is the same across all samples, and therefore
76+
-- the denominator value is zero, meaning that this function outputs NaN.
7077
denominator = sumWith rsamples $ \sample ->
7178
Sample.sum $ U.map (\r -> square (r - avgRank)) sample
7279

@@ -79,9 +86,11 @@ kruskalWallis samples = (nTot - 1) * numerator / denominator
7986
--
8087
-- It uses /Chi-Squared/ distribution for approximation as long as the sizes are
8188
-- larger than 5. Otherwise the test returns 'Nothing'.
82-
kruskalWallisTest :: (Ord a, U.Unbox a) => [U.Vector a] -> Maybe (Test ())
89+
kruskalWallisTest :: (Ord a, U.Unbox a) =>[U.Vector a] -> Maybe (Test ())
8390
kruskalWallisTest [] = Nothing
91+
kruskalWallisTest [_] = Nothing
8492
kruskalWallisTest samples
93+
| isNaN k = Nothing
8594
-- We use chi-squared approximation here
8695
| all (>4) ns = Just Test { testSignificance = mkPValue $ complCumulative d k
8796
, testStatistics = k

tests/Tests/NonParametric.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,15 @@ kruskalWallisTests = zipWith test [(0::Int)..] testData
219219
, 6.10
220220
, Just Significant
221221
)
222+
, ( [ [6, 6, 6, 6, 6]
223+
, [6, 6, 6, 6, 6, 6, 6, 6]
224+
, [6, 6, 6, 6, 6, 6]
225+
]
226+
, 0.0 / 0.0 -- this should equal NaN as all the samples are the same.
227+
, Nothing
228+
)
222229
]
223230

224-
225231
----------------------------------------------------------------
226232
-- K-S test
227233
----------------------------------------------------------------

0 commit comments

Comments
 (0)