uniqueness-periods-vector-stats (empty) → 0.1.0.0
raw patch · 5 files changed
+126/−0 lines, 5 filesdep +basedep +ghc-primsetup-changed
Dependencies added: base, ghc-prim
Files
- ChangeLog.md +5/−0
- LICENSE +20/−0
- Numeric/Stats.hs +74/−0
- Setup.hs +2/−0
- uniqueness-periods-vector-stats.cabal +25/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for uniqueness-periods-vector-stats++## 0.1.0.0 -- 2020-09-19++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2020 OleksandrZhabenko++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Numeric/Stats.hs view
@@ -0,0 +1,74 @@+-- |+-- Module : Numeric.Stats+-- Copyright : (c) OleksandrZhabenko 2020+-- License : MIT+-- Stability : Experimental+-- Maintainer : olexandr543@yahoo.com+--+-- A very basic descriptive statistics. Functions use a tail recursion approach to compute the values and are strict by an accumulator.++{-# LANGUAGE BangPatterns, MagicHash #-}++module Numeric.Stats where ++import GHC.Exts+import GHC.Prim++-- | Inspired by: https://www.mat.univie.ac.at/~neum/scan/01.pdf+sumNeumaierF :: [Float] -> Float# -> Float# -> Float+sumNeumaierF ((F# !x):xs) !c !sum1 = sumNeumaierF xs c1 t+ where { !c1 = if I# (geFloat# (fabsFloat# sum1) (fabsFloat# x)) == 1 then plusFloat# (plusFloat# c (minusFloat# sum1 t)) x else plusFloat# (plusFloat# c (minusFloat# x t)) sum1 ; !t = plusFloat# sum1 x }+sumNeumaierF _ c sum1 = F# (plusFloat# sum1 c)++-- | Inspired by: https://www.mat.univie.ac.at/~neum/scan/01.pdf+-- Looks like that it does not work for Haskell GHC (may be the compiler uses associativity rules to add numbers) as expected, the floating-point arithmetic +-- cancellation errors grows just like for the usual 'sum'.+sumNeumaier :: RealFrac a => [a] -> a -> a -> a+sumNeumaier (!x:xs) !c !sum1 = sumNeumaier xs c1 t+ where { !c1 = if abs sum1 >= abs x then c + (sum1 - t) + x else c + (x - t) + sum1 ; !t = sum1 + x }+sumNeumaier _ c sum = sum + c++sum_NF :: [Float] -> Float+sum_NF xs = sumNeumaierF xs 0.0# 0.0#+{-# INLINE sum_NF #-}++-- | A tail-recursive realization for the statistic mean. +mean2 :: RealFrac a => [a] -> a -> a -> a -> a+mean2 (x:xs) !s1 !l1 _ = mean2 xs (s1 + x) (l1 + 1) ((s1 + x) / (l1 + 1))+mean2 _ _ _ m = m++-- | Similar to 'mean2', but uses GHC unlifted types from @ghc-prim@ package. +mean2F :: [Float] -> Float# -> Float# -> Float# -> Float+mean2F ((F# !x):xs) !s1 !l1 _ = mean2F xs (plusFloat# s1 x) (plusFloat# l1 1.0#) (divideFloat# (plusFloat# s1 x) (plusFloat# l1 1.0#))+mean2F _ _ _ m = F# m++-- | One-pass and tail-recursive realization for the pair of the mean and dispersion. Is vulnerable to the floating-point cancellation errors. +meanWithDispersion :: (RealFrac a, Floating a) => [a] -> a -> a -> a -> a -> a -> (a,a)+meanWithDispersion (!x:xs) !s1 !s2 !l1 !m1 !d = meanWithDispersion xs (s1 + x) (s2 + x**2) (l1 + 1) (m0 s1 l1 x) (m0 s2 l1 (x**2) - (m0 s1 l1 x)**2)+ where m0 !s3 !l2 !x = (s3 + x) / (l2 + 1)+meanWithDispersion _ _ _ _ !m !d = (m,d)++meanWithDispersionF :: [Float] -> Float# -> Float# -> Float# -> Float# -> Float# -> (Float,Float)+meanWithDispersionF ((F# !x):xs) !s1 !s2 !l1 !m1 !d = + meanWithDispersionF xs (plusFloat# s1 x) (plusFloat# s2 (powerFloat# x 2.0#)) (plusFloat# l1 1.0#) (m0 s1 l1 x) + (m0 s2 l1 (minusFloat# (powerFloat# x 2.0#) (powerFloat# (m0 s1 l1 x) 2.0#)))+ where m0 !s3 !l2 !x = (divideFloat# (plusFloat# s3 x) (plusFloat# l2 1.0#))+meanWithDispersionF _ _ _ _ !m !d = (F# m, F# d)++-- | A tail-recursive realization for the statistic mean. A variant of the 'mean2' function. +mean :: RealFrac a => [a] -> a+mean xs = mean2 xs 0.0 0.0 0.0+{-# INLINE mean #-}++-- | Uses 'mean2F' inside. +meanF :: [Float] -> Float+meanF xs = mean2F xs 0.0# 0.0# 0.0#+{-# INLINE meanF #-}++meanWithDisp :: (RealFrac a, Floating a) => [a] -> (a,a)+meanWithDisp xs = meanWithDispersion xs 0.0 0.0 0.0 0.0 0.0+{-# INLINE meanWithDisp #-}++meanWithDispF :: [Float] -> (Float, Float)+meanWithDispF xs = meanWithDispersionF xs 0.0# 0.0# 0.0# 0.0# 0.0#+{-# INLINE meanWithDispF #-}
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ uniqueness-periods-vector-stats.cabal view
@@ -0,0 +1,25 @@+-- Initial uniqueness-periods-vector-stats.cabal generated by cabal init.+-- For further documentation, see http://haskell.org/cabal/users-guide/++name: uniqueness-periods-vector-stats+version: 0.1.0.0+synopsis: A very basic descriptive statistics.+description: A very basic descriptive statistics. Functions use a tail recursion approach to compute the values and are strict by an accumulator.+homepage: https://hackage.haskell.org/package/uniqueness-periods-vector-stats+license: MIT+license-file: LICENSE+author: OleksandrZhabenko+maintainer: olexandr543@yahoo.com+copyright: Oleksandr Zhabenko+category: Math,Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Numeric.Stats+ -- other-modules:+ other-extensions: BangPatterns, MagicHash+ build-depends: base >=4.7 && <4.15, ghc-prim >=0.3.1 && <1+ -- hs-source-dirs:+ default-language: Haskell2010