{-# LANGUAGE FlexibleContexts #-}
module Statistics.Sample.Normalize
(
standardize
) where
import Statistics.Sample
import qualified Data.Vector.Generic as G
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Storable as S
standardize :: (G.Vector v Double) => v Double -> Maybe (v Double)
standardize xs
| G.length xs < 2 = Nothing
| sigma == 0 = Nothing
| otherwise = Just $ G.map (\x -> (x - mu) / sigma) xs
where
mu = mean xs
sigma = stdDev xs
{-# INLINABLE standardize #-}
{-# SPECIALIZE standardize :: V.Vector Double -> Maybe (V.Vector Double) #-}
{-# SPECIALIZE standardize :: U.Vector Double -> Maybe (U.Vector Double) #-}
{-# SPECIALIZE standardize :: S.Vector Double -> Maybe (S.Vector Double) #-}