{-# LANGUAGE FlexibleContexts #-}-- |-- Module : Statistics.Sample.Normalize-- Copyright : (c) 2017 Gregory W. Schwartz-- License : BSD3---- Maintainer : gsch@mail.med.upenn.edu-- Stability : experimental-- Portability : portable---- Functions for normalizing samples.moduleStatistics.Sample.Normalize(standardize )whereimportStatistics.Sample importqualifiedData.Vector.GenericasGimportqualifiedData.VectorasVimportqualifiedData.Vector.UnboxedasUimportqualifiedData.Vector.StorableasS-- | /O(n)/ Normalize a sample using standard scores:---- \[ z = \frac{x - \mu}{\sigma} \]---- Where μ is sample mean and σ is standard deviation computed from-- unbiased variance estimation. If sample to small to compute σ or-- it's equal to 0 @Nothing@ is returned.standardize ::(G.Vectorv Double)=>v Double->Maybe(v Double)standardize :: forall (v :: * -> *).
Vector v Double =>
v Double -> Maybe (v Double)
standardize v Double
xs |v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.lengthv Double
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
2=Maybe (v Double)
forall a. Maybe a
Nothing|Double
sigma Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
==Double
0=Maybe (v Double)
forall a. Maybe a
Nothing|Bool
otherwise=v Double -> Maybe (v Double)
forall a. a -> Maybe a
Just(v Double -> Maybe (v Double)) -> v Double -> Maybe (v Double)
forall a b. (a -> b) -> a -> b
$(Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map(\Double
x ->(Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
mu )Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
sigma )v Double
xs wheremu :: Double
mu =v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs sigma :: Double
sigma =v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
stdDev v Double
xs {-# INLINABLEstandardize #-}{-# SPECIALIZEstandardize ::V.VectorDouble->Maybe(V.VectorDouble)#-}{-# SPECIALIZEstandardize ::U.VectorDouble->Maybe(U.VectorDouble)#-}{-# SPECIALIZEstandardize ::S.VectorDouble->Maybe(S.VectorDouble)#-}

AltStyle によって変換されたページ (->オリジナル) /