statistics 0.2 → 0.2.1
raw patch · 6 files changed
+29/−26 lines, 6 files
Files
- Statistics/Constants.hs +1/−1
- Statistics/Function.hs +15/−1
- Statistics/Resampling.hs +1/−12
- Statistics/Sample.hs +9/−9
- Statistics/Types.hs +2/−2
- statistics.cabal +1/−1
Statistics/Constants.hs view
@@ -22,7 +22,7 @@ -- | A very large number. m_huge :: Double-m_huge = 1.797693e308+m_huge = 1.7976931348623157e308 {-# INLINE m_huge #-} -- | The largest 'Int' /x/ such that 2**(/x/-1) is approximately
Statistics/Function.hs view
@@ -15,10 +15,13 @@ minMax , sort , partialSort+ , createU ) where +import Control.Exception (assert)+import Control.Monad.ST (unsafeSTToIO) import Data.Array.Vector.Algorithms.Combinators (apply)-import Data.Array.Vector ((:*:)(..), UA, UArr, foldlU)+import Data.Array.Vector import qualified Data.Array.Vector.Algorithms.Intro as I -- | Sort an array.@@ -44,3 +47,14 @@ go (MM lo hi) k = MM (min lo k) (max hi k) fini (MM lo hi) = lo :*: hi {-# INLINE minMax #-}++-- | Create an array, using the given action to populate each element.+createU :: (UA e) => Int -> (Int -> IO e) -> IO (UArr e)+createU size itemAt = assert (size >= 0) $+ unsafeSTToIO (newMU size) >>= loop 0+ where+ loop k arr | k >= size = unsafeSTToIO (unsafeFreezeAllMU arr)+ | otherwise = do+ r <- itemAt k+ unsafeSTToIO (writeMU arr k r)+ loop (k+1) arr
Statistics/Resampling.hs view
@@ -16,11 +16,11 @@ , resample ) where -import Control.Exception (assert) import Control.Monad (forM_) import Control.Monad.ST (unsafeSTToIO) import Data.Array.Vector import Data.Array.Vector.Algorithms.Intro (sort)+import Statistics.Function (createU) import Statistics.Types (Estimator, Sample) import System.Random.Mersenne (MTGen, random) @@ -50,17 +50,6 @@ writeMU arr k . est $ re loop (k+1) ers n = lengthU samples---- | Create an array, using the given action to populate each element.-createU :: (UA e) => Int -> (Int -> IO e) -> IO (UArr e)-createU size itemAt = assert (size >= 0) $- unsafeSTToIO (newMU size) >>= loop 0- where- loop k arr | k >= size = unsafeSTToIO (unsafeFreezeAllMU arr)- | otherwise = do- r <- itemAt k- unsafeSTToIO (writeMU arr k r)- loop (k+1) arr -- | Compute a statistical estimate repeatedly over a sample, each -- time omitting a successive element.
Statistics/Sample.hs view
@@ -38,7 +38,7 @@ -- $references ) where -import Data.Array.Vector (foldlU)+import Data.Array.Vector (foldlU, lengthU) import Statistics.Types (Sample) -- | Arithmetic mean. This uses Welford's algorithm to provide@@ -83,16 +83,16 @@ -- Because of the need for two passes, these functions are /not/ -- subject to stream fusion. +data V = V {-# UNPACK #-} !Double {-# UNPACK #-} !Double+ robustVar :: Sample -> T-robustVar samp = fini . foldlU go (T1 0 0 0) $ samp+robustVar samp = fini . foldlU go (V 0 0) $ samp where- go (T1 n s c) x = T1 n' s' c'- where n' = n + 1- s' = s + d * d- c' = c + d- d = x - m- fini (T1 n s c) = T (s - c ** (2 / fromIntegral n)) n- m = mean samp+ go (V s c) x = V (s + d * d) (c + d)+ where d = x - m+ fini (V s c) = T (s - (c * c) / fromIntegral n) n+ n = lengthU samp+ m = mean samp -- | Maximum likelihood estimate of a sample's variance. variance :: Sample -> Double
Statistics/Types.hs view
@@ -11,8 +11,8 @@ module Statistics.Types (- Sample- , Estimator+ Estimator+ , Sample , Weights ) where
statistics.cabal view
@@ -1,5 +1,5 @@ name: statistics-version: 0.2+version: 0.2.1 synopsis: A library of statistical types, data, and functions description: This library provides a number of common functions and types useful