HNumeric 0.3.1.0 → 0.3.2.0
raw patch · 4 files changed
+116/−25 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- HNum.CSV: [lab] :: DataFrame a -> Label
- HNum.CSV: dataframe' :: Header -> Matrix a -> Label -> DataFrame a
- HNum.CSV: fromVectors' :: Header -> [Vector a] -> Label -> DataFrame a
- HNum.CSV: type Label = [String]
+ HNum.Stats: count :: Eq a => a -> [a] -> Int
+ HNum.Stats: cv :: (Statistical v, Floating a) => v a -> a
+ HNum.Stats: describe :: (Show a, Floating a, Ord a) => Vector a -> IO ()
+ HNum.Stats: kurt :: (Statistical v, Floating a) => v a -> a
+ HNum.Stats: med :: (Statistical v, Ord a, Floating a) => v a -> a
+ HNum.Stats: mode :: (Statistical v, Eq a) => v a -> a
+ HNum.Stats: moment :: (Statistical v, Floating a) => a -> v a -> a
+ HNum.Stats: se :: (Statistical v, Floating a) => v a -> a
+ HNum.Stats: skew :: (Statistical v, Floating a) => v a -> a
+ HNum.Stats: skew' :: (Statistical v, Floating a) => v a -> a
+ HNum.Stats: summary :: (Show a, Floating a) => DataFrame a -> IO ()
+ HNum.Vector: qsort :: Ord a => Vector a -> Vector a
+ HNum.Vector: vec :: [a] -> Vector a
- HNum.CSV: DataFrame :: Header -> Matrix a -> Label -> DataFrame a
+ HNum.CSV: DataFrame :: Header -> Matrix a -> DataFrame a
Files
- HNumeric.cabal +2/−2
- src/HNum/CSV.hs +7/−19
- src/HNum/Stats.hs +90/−4
- src/HNum/Vector.hs +17/−0
HNumeric.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: ce543fbe9d1a2354607564a7cc9b45578ee49880af97240e46e90d23d3a5579b+-- hash: c5d1ade7480449723b6215fe9d6fe260a29e127d440ff4511d5d3f926c680586 name: HNumeric-version: 0.3.1.0+version: 0.3.2.0 synopsis: Haskell Numeric Library with pure functionality, R & MATLAB Syntax. description: Please see the README on GitHub at <https://github.com/Axect/HNumeric#readme> category: HNum, library, Numeric, LinearAlgebra, Statistics, bsd3
src/HNum/CSV.hs view
@@ -12,31 +12,20 @@ -- | Type Aliases for convenience type Header = [String]-type Label = [String] -- | DataFrame structure to write csv-data DataFrame a = DataFrame { header :: Header, dat :: Matrix a, lab :: Label} deriving (Show, Eq)+data DataFrame a = DataFrame { header :: Header, dat :: Matrix a} deriving (Show, Eq) --- | No label dataframe+-- | dataframe constructor dataframe :: Header -> Matrix a -> DataFrame a-dataframe h m | length h == row m = DataFrame h m (replicate n "value")+dataframe h m | length h == row m = DataFrame h m | otherwise = error "Length of Header != Length of Data" where n = length m `div` length h --- | With label dataframe-dataframe' :: Header -> Matrix a -> Label -> DataFrame a-dataframe' h m l- | length h == row m && row m == length l = DataFrame h m l- | otherwise = error "Length of Header != Length of Data != Length of Label"---- | No label dataframe from Vectors+-- | dataframe from vectors fromVectors :: Header -> [Vector a] -> DataFrame a fromVectors h vs = dataframe h (matrix vs') where vs' = map toList vs --- | With label dataframe from Vectors-fromVectors' :: Header -> [Vector a] -> Label -> DataFrame a-fromVectors' h vs = dataframe' h (matrix vs') where vs' = map toList vs- instance Functor DataFrame where fmap f df = df { dat = fmap f (dat df) } @@ -55,10 +44,9 @@ write title m = writeFile title (toString m) instance CSVtize DataFrame where- toString (DataFrame h m l) = h' ++ "\n" ++ m'- where h' = cm h ++ ",label"- m' = foldr ((\x y -> x ++ "\n" ++ y) . cm) "" $ matForm $ hcat (show <$> transpose m) l'- l' = transpose $ matrix [l]+ toString (DataFrame h m) = h' ++ "\n" ++ m'+ where h' = cm h+ m' = toString (transpose m) write title df = writeFile title (toString df)
src/HNum/Stats.hs view
@@ -11,13 +11,13 @@ import HNum.Vector import Data.Random.Normal import System.Random+import HNum.CSV -- | To contain coefficients of linear regression. type Coeff a = (a, a) -------------------------------------------------------- -- Basic Probability --------------------------------------------------------- -- | Factorial fac :: Integral a => a -> a fac 0 = 1@@ -36,38 +36,116 @@ c :: Integral a => a -> a -> a n `c` r = (n `p` r) `div` fac r -- -------------------------------------------------------- -- Basic Statistics -------------------------------------------------------- -- | Basic Statistics Class for Vector class VecOps v => Statistical v where+ -- | Sample Mean mean :: Fractional a => v a -> a -- | Single Valued covariance cov' :: Floating a => v a -> v a -> a -- | Covariance Matrix cov :: Floating a => v a -> v a -> Matrix a+ -- | Sample Variance var :: Floating a => v a -> a+ -- | Sample Standard deviation std :: Floating a => v a -> a+ -- | Standard Error+ se :: Floating a => v a -> a -- | Correlation Coefficient cor :: Floating a => v a -> v a -> a+ -- | Median+ med :: (Ord a, Floating a) => v a -> a+ -- | Mode+ mode :: Eq a => v a -> a+ -- | Coefficient of Variation+ cv :: Floating a => v a -> a+ -- | Moment+ moment :: Floating a => a -> v a-> a+ -- | Skewness+ skew :: Floating a => v a -> a+ -- | Skewness 2+ skew' :: Floating a => v a -> a+ -- | kurtosis+ kurt :: Floating a => v a -> a instance Statistical Vector where+ -- mean mean x = sum x / fromIntegral (length x)+ -- cov' cov' x y | length x <= 1 || length y <= 1 = error "Samples are not enough" | length x /= length y = error "Length is not same" | otherwise = ((x .- mean x) .*. (y .- mean y)) / fromIntegral (length x - 1)+ -- cov cov x y = matrix [[var x, cov' x y], [cov' y x, var y]]+ -- var var v = cov' v v+ -- std std = sqrt . var+ -- se+ se x = std x / sqrt (fromIntegral (length x))+ -- cor cor x y = cov' x y / (std x * std y)+ -- med+ med x | even l = ((qs !! (l'-1)) + (qs !! l')) / 2+ | otherwise = qs !! l'+ where l = length x+ l' = l `div` 2+ qs = (toList . qsort) x+ -- mode+ mode x = v !! n+ where v = toList x+ cx = map (`count` v) v+ m = maximum cx+ n = head $ dropWhile (\p -> cx !! p /= m) [0..]+ -- cv+ cv x = std x / mean x+ -- moment+ moment n x = sum ((x .- mean x) .^ n)+ -- skew+ skew x = (1 / fromIntegral l) * moment 3 x / std x ^ 3+ where l = length x+ skew' x = (fromIntegral l^2 / fromIntegral ((l-1) * (l-2))) * skew x+ where l = length x+ -- kurt+ kurt x = moment 4 x / (fromIntegral l * std x ** 4) - 3+ where l = length x ----------------------------------------------------------- Distribution +-- For IO --------------------------------------------------------+summary :: (Show a, Floating a) => DataFrame a -> IO ()+summary df = do+ putStrLn $ "Mean: " ++ show hm+ putStrLn $ "Var: " ++ show hv+ putStrLn $ "Std: " ++ show hs+ where+ h = header df+ m = matForm $ dat df+ ms = map (mean . vector) m+ vs = map (var . vector) m+ ss = map (std . vector) m+ hm = zip h ms+ hv = zip h vs+ hs = zip h ss +describe :: (Show a, Floating a, Ord a) => Vector a -> IO ()+describe v = do+ putStrLn $ "n: " ++ show (length v)+ putStrLn $ "mean: " ++ show (mean v)+ putStrLn $ "std: " ++ show (std v)+ putStrLn $ "med: " ++ show (med v)+ putStrLn $ "mode: " ++ show (mode v)+ putStrLn $ "min: " ++ show (minimum v)+ putStrLn $ "max: " ++ show (maximum v)+ putStrLn $ "skew: " ++ show (skew v)+ putStrLn $ "kurt: " ++ show (kurt v)+ putStrLn $ "SE: " ++ show (se v)+--------------------------------------------------------+-- Linear Regression+-------------------------------------------------------- -- | Least Square Method - (Intercept, Slope) lm :: Floating a => Vector a -> Vector a -> Coeff a lm x y = (my - b1 * mx, b1)@@ -87,3 +165,11 @@ -- | Relative Standard Error rse :: Floating a => Vector a -> Vector a -> a rse x y = sqrt (1 / fromIntegral (length x - 2) * rss x y)++--------------------------------------------------------+-- Backend Functions+--------------------------------------------------------++-- | Count Elements+count :: Eq a => a -> [a] -> Int+count p v = length (filter (== p) v)
src/HNum/Vector.hs view
@@ -20,6 +20,9 @@ vector :: [a] -> Vector a vector = Vector +vec :: [a] -> Vector a+vec = Vector+ -- Instance Section instance Functor Vector where fmap f (Vector x) = Vector (fmap f x)@@ -238,6 +241,20 @@ (.:) :: Vector a -> Matrix a -> Matrix a v .: m | length v == col m = matrix (toList v : matForm m) | otherwise = error "Can't insert length(Vector) /= col(Matrix)"++---------------------------------------------------+-- Sort+---------------------------------------------------+-- | Quick Sort+qsort :: Ord a => Vector a -> Vector a+qsort (Vector []) = vec []+qsort (Vector (x : xs)) =+ (qsort . vec) [ y | y <- xs, y <= x ] `hcat` vec [x] `hcat` (qsort . vec)+ [ y | y <- xs, y > x ]++-- | Merge Sort+--msort :: Ord a => Vector a -> Vector a+--msort --------------------------------------------------- -- Backend Functions (Do not Understand)