module Math.Diversity.RandomSampling ( subsample
, subsampleSpecies
, subsampleES ) where
import qualified Data.Set as Set
import Control.Monad.Random
import System.Random.Shuffle
import Math.Diversity.Statistics
subsample :: (Eq a, Ord a) => Int -> Int -> StdGen -> [a] -> [a]
subsample size n gen xs
| n > size `div` 2 = drop (size n) shuffled
| otherwise = take n shuffled
where
shuffled = shuffle' xs size gen
subsampleSpecies :: (Eq a, Ord a) => Int -> Int -> StdGen -> [a] -> Int
subsampleSpecies size n gen = Set.size . Set.fromList . subsample size n gen
subsampleES :: (Eq a, Ord a) => Int -> Int -> Int -> [a] -> IO (Double, Double)
subsampleES runs size n xs = do
allRuns <- mapM (const run) [1..runs]
let statTuple = medmad . map fromIntegral $ allRuns
return statTuple
where
run = do
gen <- newStdGen
return $ subsampleSpecies size n gen xs