module Math.Diversity.Diversity ( hamming
, diversity
, rarefactionCurve
, rarefactionSampleCurve
, rarefactionViable ) where
import Data.List
import qualified Data.Set as Set
import Numeric.SpecFunctions (choose)
import Data.Function (on)
import Math.Diversity.RandomSampling
hamming :: String -> String -> Int
hamming xs ys = length $ filter not $ zipWith (==) xs ys
productDivision :: Double -> [Integer] -> [Integer] -> Double
productDivision acc [] [] = acc
productDivision acc [] (y:ys) = (acc / fromInteger y)
* productDivision acc [] ys
productDivision acc (x:xs) [] = acc * fromInteger x * productDivision acc xs []
productDivision acc (x:xs) (y:ys)
| x == y = productDivision acc xs ys
| otherwise = (fromInteger x / fromInteger y) * productDivision acc xs ys
diversity :: (Ord b) => Double -> [b] -> Double
diversity order sample
| null sample = 0
| order == 1 = exp . h $ speciesList
| otherwise = (sum . map ((** order) . p_i) $ speciesList) ** pow
where
pow = 1 / (1 order)
h = negate . sum . map (\x -> p_i x * log (p_i x))
p_i x = ((fromIntegral . length $ x) :: Double) /
((fromIntegral . length $ sample) :: Double)
speciesList = group . sort $ sample
specialBinomial :: Bool -> Integer -> Integer -> Integer -> Double
specialBinomial False n_total g n = productDivision 1 num den
where
num = [(n_total g n + 1)..(n_total g)]
den = [(n_total n + 1)..n_total]
specialBinomial True n_total g n = choose
(fromIntegral n_total fromIntegral g)
(fromIntegral n)
rarefactionCurve :: (Eq a, Ord a)
=> Bool
-> Int
-> Integer
-> Integer
-> [a]
-> IO [(Int, (Double, Double))]
rarefactionCurve !fastBin !runs !start !interval !xs =
mapM rarefact $ [start,(start + interval)..(n_total 1)] ++ [n_total]
where
rarefact !n
| n == 0 = return (fromIntegral n, (0, 0))
| n == 1 = return (fromIntegral n, (1, 0))
| n == n_total = return (fromIntegral n, (k, 0))
| runs == 0 = return (fromIntegral n, (k inner n, 0))
| otherwise = do
statTuple <-
subsampleES runs (fromIntegral n_total) (fromIntegral n) xs
return (fromIntegral n, statTuple)
inner n = ( \x -> if fastBin
then x / choose (fromIntegral n_total) (fromIntegral n)
else x )
. sum
. map (\g -> specialBinomial fastBin n_total g n)
$ grouped
n_total = genericLength xs
k = genericLength grouped
grouped = map genericLength . group . sort $ xs
getSampleContents :: (Ord a, Ord b) => [(a, b)] -> [Set.Set b]
getSampleContents = map (Set.fromList . map snd)
. groupBy ((==) `on` fst)
. sortBy (compare `on` fst)
rarefactionSampleCurve :: (Ord a, Ord b)
=> Bool
-> Int
-> Int
-> [(a, b)]
-> IO [(Int, (Double, Double))]
rarefactionSampleCurve !fastBin !start !interval !ls =
mapM rarefact $ [start,(start + interval)..(t_total 1)] ++ [t_total]
where
rarefact !t
| t == 0 = return (t, (0, 0))
| t == t_total = return (t, (richness, 0))
| otherwise = return (t, (richness inner t, 0))
inner t = ( \x -> if fastBin
then x / choose t_total t
else x )
. sum
. map ( \s -> specialBinomial
fastBin
(fromIntegral t_total)
(numHave s samples)
(fromIntegral t) )
$ speciesList
numHave s = sum . map (\x -> if Set.member s x then 1 else 0)
richness = genericLength speciesList
speciesList = nub . map snd $ ls
t_total = genericLength samples
samples = getSampleContents ls
rarefactionViable :: [Double] -> Double
rarefactionViable !xs = (genericLength valid / genericLength xs) * 100
where
valid = dropWhile (< (0.95 * last xs)) xs