statistics icon indicating copy to clipboard operation
statistics copied to clipboard

Feature request: permutation test

Open GregorySchwartz opened this issue 6 years ago • 4 comments

This should be relatively straightforward to implement: type permutationTest :: (v a -> v b -> Double) -> Int -> PValue, i.e. takes in a function that takes a vector of labels and a vector of samples to report a test statistic, a number of random runs, and returns the p-value (possibly with the observed test statistic of the samples with labels).

GregorySchwartz avatar Sep 19 '19 19:09 GregorySchwartz

I don't understand what this test should do. Function with provided signature could only compute PValue from Int and doesn't seems to be useful

Shimuuar avatar Sep 21 '19 15:09 Shimuuar

You're absolutely right. I meant:

permutationTest :: (v a -> v b -> Double) -> Int -> v a -> v b -> PValue

That is, (function of labels, samples to test statistic) -> number of runs -> labels -> samples -> p-value.

GregorySchwartz avatar Sep 23 '19 15:09 GregorySchwartz

It would be basically (with mwc-random):

-- | Permutation test returning p-value.                     
permutationTest :: (G.Vector v a, G.Vector v b)              
                => (v a -> v b -> Double)                    
                -> Int                                       
                -> v a                                       
                -> v b                                       
                -> IO Double                                 
permutationTest f runs labs samples = do                     
  let obs = f labs samples                                   
      run r = do                                             
        g <- R.initialize . V.singleton $ fromIntegral r     
        ls <- R.uniformShuffle labs g                        
        return $ f ls samples                                
                                                             
  n <- genericLength . filter (>= obs) <$> mapM run [1..runs]
                                                             
  return $ n / fromIntegral runs

GregorySchwartz avatar Sep 23 '19 15:09 GregorySchwartz

Although it should be permutationTest :: (v a -> b -> Double) -> Int -> v a -> b -> PValue to generalize the sample structure (it could be a matrix or tree, for instance).

GregorySchwartz avatar Sep 23 '19 18:09 GregorySchwartz