{-# 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 :: forall (v :: * -> *).
Vector v Double =>
v Double -> Maybe (v Double)
standardize v Double
xs
| forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
xs forall a. Ord a => a -> a -> Bool
< Int
2 = forall a. Maybe a
Nothing
| Double
sigma forall a. Eq a => a -> a -> Bool
== Double
0 = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
x -> (Double
x forall a. Num a => a -> a -> a
- Double
mu) forall a. Fractional a => a -> a -> a
/ Double
sigma) v Double
xs
where
mu :: Double
mu = forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs
sigma :: Double
sigma = forall (v :: * -> *). Vector v Double => v Double -> Double
stdDev v Double
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) #-}