-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd79460
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Revision history for uniqueness-periods-vector-stats | ||
|
||
## 0.1.0.0 -- 2020-09-19 | ||
|
||
* First version. Released on an unsuspecting world. | ||
|
||
## 0.1.1.0 -- 2020-09-19 | ||
|
||
* First version revised A. Fixed issues with not importing from GHC.Prim fabsFloat# for GHC lower than 8.2* versions. Removed functions that used Neumaier summation because | ||
they do not have the intended behaviour. | ||
|
||
## 0.1.2.0 -- 2020-09-19 | ||
|
||
* First version revised B. Removed the functions that compute mean with dispersion using unlifted types, because of numeric inaccuracy. | ||
|
||
## 0.2.0.0 -- 2020-12-05 | ||
|
||
* Second version. Some documentation improvements because of the similar code snippets found in the various | ||
previous publications by the other authors. Added new functions and rewrite the existing ones. | ||
|
||
## 0.2.1.0 -- 2020-12-05 | ||
|
||
* Second version revised A. Fixed issue with being wrongly defined for the meanD and meanF functions. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
-- | | ||
-- Module : Numeric.Stats | ||
-- Copyright : (c) OleksandrZhabenko 2020 | ||
-- License : MIT | ||
-- Stability : Experimental | ||
-- Maintainer : [email protected] | ||
-- | ||
-- 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 | ||
|
||
-- | Uses GHC unlifted types from @ghc-prim@ package. | ||
-- Similar code is here: Don Stewart. https://donsbot.wordpress.com/2008/05/06/write-haskell-as-fast-as-c-exploiting-strictness-laziness-and-recursion/ | ||
-- And here: http://fixpt.de/blog/2017-12-04-strictness-analysis-part-1.html | ||
-- And here: Michael Snoyman. https://www.fpcomplete.com/blog/2017/09/all-about-strictness/ | ||
-- | ||
mean2F :: [Float] -> Float# -> Int# -> Float | ||
mean2F ((F# !x):xs) !s1 !l1 = mean2F xs (plusFloat# s1 x) (l1 +# 1#) | ||
mean2F _ !s1 !l1 = | ||
case I# l1 of | ||
0 -> error "Not defined for the third zero argument. " | ||
_ -> F# m | ||
where !m = divideFloat# s1 (int2Float# l1) | ||
|
||
-- | Uses GHC unlifted types from @ghc-prim@ package. | ||
-- Similar code is here: Don Stewart. https://donsbot.wordpress.com/2008/05/06/write-haskell-as-fast-as-c-exploiting-strictness-laziness-and-recursion/ | ||
-- And here: http://fixpt.de/blog/2017-12-04-strictness-analysis-part-1.html | ||
-- And here: Michael Snoyman. https://www.fpcomplete.com/blog/2017/09/all-about-strictness/ | ||
-- | ||
mean2D :: [Double] -> Double# -> Int# -> Double | ||
mean2D ((D# !x):xs) !s1 !l1 = mean2D xs (s1 +## x) (l1 +# 1#) | ||
mean2D _ !s1 !l1 = | ||
case I# l1 of | ||
0 -> error "Not defined for the third zero argument. " | ||
_ -> D# m | ||
where !m = s1 /## int2Double# l1 | ||
|
||
-- | One-pass and tail-recursive realization for the pair of the mean and dispersion. Is vulnerable to the floating-point cancellation errors. | ||
-- Similar code is here: Don Stewart. https://donsbot.wordpress.com/2008/05/06/write-haskell-as-fast-as-c-exploiting-strictness-laziness-and-recursion/ | ||
-- And here: http://fixpt.de/blog/2017-12-04-strictness-analysis-part-1.html | ||
-- And here: Michael Snoyman. https://www.fpcomplete.com/blog/2017/09/all-about-strictness/ | ||
-- When using the needed, please, refer better to their variants. | ||
-- | ||
-- Among the 'meanWithDispersion', 'meanWithDisprsionF' and 'meanWithDispersionD' better to use the last one. | ||
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*x) (l1 + 1) (m0 s1 l1 x) (m0 s2 l1 (x*x) - (m0 s1 l1 x)**2) | ||
where m0 !s3 !l2 !x = (s3 + x) / (l2 + 1) | ||
meanWithDispersion _ _ _ _ !m !d = (m,d) | ||
|
||
-- | Among the 'meanWithDispersion', 'meanWithDisprsionF' and 'meanWithDispersionD' better to use the last one. | ||
meanWithDispersionF :: [Float] -> Float# -> Float# -> Int# -> (Float,Float) | ||
meanWithDispersionF ((F# !x):xs) !s1 !s2 !l1 = meanWithDispersionF xs (plusFloat# s1 x) (plusFloat# s2 (timesFloat# x x)) (l1 +# 1#) | ||
meanWithDispersionF [] !s1 !s2 !l1 = (F# m, F# (minusFloat# (divideFloat# s2 (int2Float# l1)) (timesFloat# m m))) | ||
where !m = divideFloat# s1 (int2Float# l1) | ||
|
||
-- | Among the 'meanWithDispersion', 'meanWithDisprsionF' and 'meanWithDispersionD' better to use the last one. | ||
meanWithDispersionD :: [Double] -> Double# -> Double# -> Int# -> (Double,Double) | ||
meanWithDispersionD ((D# !x):xs) !s1 !s2 !l1 = meanWithDispersionD xs (s1 +## x) (s2 +## (x *## x)) (l1 +# 1#) | ||
meanWithDispersionD [] !s1 !s2 !l1 = (D# m, D# ((s2 /## int2Double# l1) -## (m *## m))) | ||
where !m = s1 /## int2Double# l1 | ||
|
||
-- | Uses 'mean2F' inside. | ||
meanF :: [Float] -> Float | ||
meanF xs = mean2F xs 0.0# 0# | ||
{-# INLINE meanF #-} | ||
|
||
meanD :: [Double] -> Double | ||
meanD xs = mean2D xs 0.0## 0# | ||
|
||
-- | Among the 'meanWithDisp', 'meanWithDispF2' and 'meanWithDispD2' better to use the last one. | ||
meanWithDisp :: (RealFrac a, Floating a) => [a] -> (a,a) | ||
meanWithDisp xs | ||
| null xs = error "Not defined for the empty list. " | ||
| otherwise = meanWithDispersion xs 0.0 0.0 0.0 0.0 0.0 | ||
{-# RULES "realfroc/float" meanWithDisp = meanWithDispF2 #-} | ||
{-# RULES "realfroc/double" meanWithDisp = meanWithDispD2 #-} | ||
{-# INLINE[2] meanWithDisp #-} | ||
|
||
-- | Among the 'meanWithDisp', 'meanWithDispF2' and 'meanWithDispD2' better to use the last one. | ||
meanWithDispF2 :: [Float] -> (Float,Float) | ||
meanWithDispF2 xs | ||
| null xs = error "Not defined for the empty list. " | ||
| otherwise = meanWithDispersionF xs 0.0# 0.0# 0# | ||
{-# INLINE meanWithDispF2 #-} | ||
|
||
-- | Among the 'meanWithDisp', 'meanWithDispF2' and 'meanWithDispD2' better to use the last one. | ||
meanWithDispD2 :: [Double] -> (Double,Double) | ||
meanWithDispD2 xs | ||
| null xs = error "Not defined for the empty list. " | ||
| otherwise = meanWithDispersionD xs 0.0## 0.0## 0# | ||
{-# INLINE meanWithDispD2 #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.2.1.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: [email protected] | ||
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 |