monoid-statistics 1.1.2 → 1.1.3
raw patch · 4 files changed
+136/−26 lines, 4 filesdep ~vectorPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: vector
API changes (from Hackage documentation)
+ Data.Monoid.Statistics.Class: calcEffNEvt :: CalcNEvt a => a -> Double
+ Data.Monoid.Statistics.Class: calcEvtsW :: CalcNEvt a => a -> Double
+ Data.Monoid.Statistics.Class: calcEvtsWErr :: CalcNEvt a => a -> Double
+ Data.Monoid.Statistics.Class: class CalcNEvt a
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Int.Int32
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Int.Int64
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Types.Int
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Types.Word
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Word.Word32
+ Data.Monoid.Statistics.Class: instance Data.Monoid.Statistics.Class.CalcNEvt GHC.Word.Word64
+ Data.Monoid.Statistics.Numeric: instance Data.Data.Data a => Data.Data.Data (Data.Monoid.Statistics.Numeric.CountG a)
+ Data.Monoid.Statistics.Numeric: instance Data.Monoid.Statistics.Class.CalcNEvt Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance Data.Vector.Unboxed.Base.IsoUnbox Data.Monoid.Statistics.Numeric.CountW (GHC.Types.Double, GHC.Types.Double)
+ Data.Monoid.Statistics.Numeric: instance Data.Vector.Unboxed.Base.Unbox Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance Foreign.Storable.Storable a => Foreign.Storable.Storable (Data.Monoid.Statistics.Numeric.CountG a)
+ Data.Monoid.Statistics.Numeric: instance GHC.Base.Monoid Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance GHC.Base.Semigroup Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance GHC.Classes.Eq Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance GHC.Generics.Generic Data.Monoid.Statistics.Numeric.CountW
+ Data.Monoid.Statistics.Numeric: instance GHC.Real.Real a => Data.Monoid.Statistics.Class.CalcNEvt (Data.Monoid.Statistics.Numeric.CountG a)
+ Data.Monoid.Statistics.Numeric: instance GHC.Real.Real a => Data.Monoid.Statistics.Class.StatMonoid Data.Monoid.Statistics.Numeric.CountW a
+ Data.Monoid.Statistics.Numeric: instance GHC.Show.Show Data.Monoid.Statistics.Numeric.CountW
Files
- Changelog.md +8/−0
- Data/Monoid/Statistics/Class.hs +46/−0
- Data/Monoid/Statistics/Numeric.hs +80/−24
- monoid-statistics.cabal +2/−2
Changelog.md view
@@ -1,3 +1,11 @@+# Changes in 1.1.3++- `Data` and `Storable` instances for `CountG`.++- `CalcNEvt` type class added and `CountW` accumulator for counting weighted+ events.++ # Changes in 1.1.2 - `Unbox` instances for `MeanNaive`, `WMeanNaive`, `WMeanKBN`.
Data/Monoid/Statistics/Class.hs view
@@ -27,10 +27,12 @@ -- * Ad-hoc type classes for select statistics -- $adhoc , CalcCount(..)+ , CalcNEvt(..) , CalcMean(..) , HasMean(..) , CalcVariance(..) , HasVariance(..)+ -- ** Deriving via , CalcViaHas(..) -- * Exception handling , Partial(..)@@ -44,6 +46,8 @@ import Control.Monad.Catch (MonadThrow(..)) import Data.Data (Typeable,Data) import Data.Monoid+import Data.Int+import Data.Word import Data.Vector.Unboxed (Unbox) import Data.Vector.Unboxed.Deriving (derivingUnbox) import qualified Data.Foldable as F@@ -285,8 +289,50 @@ {-# MINIMAL (getVariance,getVarianceML) | (getStddev,getStddevML) #-} +-- | Type class for accumulators that are used for event counting with+-- possibly weighted events. Those are mostly used as accumulators+-- in histograms.+class CalcNEvt a where+ -- | Calculate sum of events weights.+ calcEvtsW :: a -> Double+ -- | Calculate error estimate (1σ or 68% CL). All instances defined+ -- in library use normal approximation which breaks down for small+ -- number of events.+ calcEvtsWErr :: a -> Double+ -- | Calculate effective number of events which is defined as+ -- \[N=E(w)^2/\operatorname{Var}(w)\] or as number of events+ -- which will yield same estimate for mean variance is they all+ -- have same weight.+ calcEffNEvt :: a -> Double+ calcEffNEvt = calcEvtsW +instance CalcNEvt Int where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW +instance CalcNEvt Int32 where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW++instance CalcNEvt Int64 where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW++instance CalcNEvt Word where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW++instance CalcNEvt Word32 where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW++instance CalcNEvt Word64 where+ calcEvtsW = fromIntegral+ calcEvtsWErr = sqrt . calcEvtsW+++-- | Derive instances for 'CalcMean' and 'CalcVariance' from 'HasMean'+-- and 'HasVariance' instances. newtype CalcViaHas a = CalcViaHas a deriving newtype (HasMean, HasVariance)
Data/Monoid/Statistics/Numeric.hs view
@@ -1,15 +1,21 @@-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE DeriveDataTypeable #-}-{-# LANGUAGE DeriveFoldable #-}-{-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE DeriveTraversable #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE ViewPatterns #-}+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE DeriveAnyClass #-}+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE DeriveFoldable #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveTraversable #-}+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE DerivingVia #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE ViewPatterns #-} -- | -- Monoids for calculating various statistics in constant space module Data.Monoid.Statistics.Numeric (@@ -55,6 +61,10 @@ import Data.Data (Typeable,Data) import Data.Vector.Unboxed (Unbox) import Data.Vector.Unboxed.Deriving (derivingUnbox)+import qualified Data.Vector.Unboxed as VU+import qualified Data.Vector.Generic as VG+import qualified Data.Vector.Generic.Mutable as VGM+import Foreign.Storable (Storable) import Numeric.Sum import GHC.Generics (Generic) @@ -67,7 +77,8 @@ -- | Calculate number of elements in the sample. newtype CountG a = CountG { calcCountN :: a }- deriving (Show,Eq,Ord,Typeable)+ deriving stock (Show,Eq,Ord,Data)+ deriving newtype (Storable) type Count = CountG Int @@ -90,8 +101,43 @@ instance CalcCount (CountG Int) where calcCount = calcCountN +instance Real a => CalcNEvt (CountG a) where+ calcEvtsW = realToFrac . calcCountN+ calcEvtsWErr = sqrt . calcEvtsW+ {-# INLINE calcEvtsW #-}+ {-# INLINE calcEvtsWErr #-} +---------------------------------------------------------------- +-- | Accumulator type for counting weighted events. Weights are+-- presumed to be independent and follow same distribution \[W\].+-- In this case sum of weights follows compound Poisson+-- distribution. Its expectation could be then estimated as+-- \[\sum_iw_i\] and variance as \[\sum_iw_i^2\].+--+-- Main use of this data type is as accumulator in histograms which+-- count weighted events.+data CountW = CountW+ !Double -- Sum of weights+ !Double -- Sum of weight squares+ deriving stock (Show,Eq,Generic)++instance Semigroup CountW where+ CountW wA w2A <> CountW wB w2B = CountW (wA+wB) (w2A+w2B)+ {-# INLINE (<>) #-}+instance Monoid CountW where+ mempty = CountW 0 0++instance Real a => StatMonoid CountW a where+ addValue (CountW w w2) a = CountW (w + x) (w2 + x*x)+ where+ x = realToFrac a++instance CalcNEvt CountW where+ calcEvtsW (CountW w _ ) = w+ calcEvtsWErr (CountW _ w2) = sqrt w2+ calcEffNEvt (CountW w w2) = w * w / w2+ ---------------------------------------------------------------- -- | Type alias for currently recommended algorithms for calculation@@ -116,7 +162,7 @@ -- numbers loses precision and genrally use 'MeanKBN' is -- recommended. data MeanNaive = MeanNaive !Int !Double- deriving (Show,Eq,Typeable,Data,Generic)+ deriving stock (Show,Eq,Data,Generic) asMeanNaive :: MeanNaive -> MeanNaive asMeanNaive = id@@ -150,7 +196,7 @@ -- doing more operations. This means that it's usually possible to -- compute sum (and therefore mean) within 1 ulp. data MeanKBN = MeanKBN !Int {-# UNPACK #-} !KBNSum- deriving (Show,Eq,Typeable,Data,Generic)+ deriving stock (Show,Eq,Data,Generic) asMeanKBN :: MeanKBN -> MeanKBN asMeanKBN = id@@ -182,7 +228,7 @@ data WMeanNaive = WMeanNaive !Double -- Weight !Double -- Weighted sum- deriving (Show,Eq,Typeable,Data,Generic)+ deriving stock (Show,Eq,Data,Generic) asWMeanNaive :: WMeanNaive -> WMeanNaive asWMeanNaive = id@@ -215,7 +261,7 @@ data WMeanKBN = WMeanKBN {-# UNPACK #-} !KBNSum -- Weight {-# UNPACK #-} !KBNSum -- Weighted sum- deriving (Show,Eq,Typeable,Data,Generic)+ deriving stock (Show,Eq,Data,Generic) asWMeanKBN :: WMeanKBN -> WMeanKBN asWMeanKBN = id@@ -260,7 +306,7 @@ data Variance = Variance {-# UNPACK #-} !Int -- Number of elements in the sample {-# UNPACK #-} !Double -- Current sum of elements of sample {-# UNPACK #-} !Double -- Current sum of squares of deviations from current mean- deriving (Show,Eq,Typeable)+ deriving stock (Show,Eq,Typeable) -- | Type restricted 'id ' asVariance :: Variance -> Variance@@ -316,7 +362,7 @@ -- | Calculate minimum of sample newtype Min a = Min { calcMin :: Maybe a }- deriving (Show,Eq,Ord,Typeable,Data,Generic)+ deriving stock (Show,Eq,Ord,Data,Generic) instance Ord a => Semigroup (Min a) where Min (Just a) <> Min (Just b) = Min (Just $! min a b)@@ -335,7 +381,7 @@ -- | Calculate maximum of sample newtype Max a = Max { calcMax :: Maybe a }- deriving (Show,Eq,Ord,Typeable,Data,Generic)+ deriving stock (Show,Eq,Ord,Data,Generic) instance Ord a => Semigroup (Max a) where Max (Just a) <> Max (Just b) = Max (Just $! max a b)@@ -355,7 +401,7 @@ -- | Calculate minimum of sample of Doubles. For empty sample returns NaN. Any -- NaN encountered will be ignored. newtype MinD = MinD { calcMinD :: Double }- deriving (Show,Typeable,Data,Generic)+ deriving stock (Show,Data,Generic) instance Eq MinD where MinD a == MinD b@@ -381,7 +427,7 @@ -- | Calculate maximum of sample. For empty sample returns NaN. Any -- NaN encountered will be ignored. newtype MaxD = MaxD { calcMaxD :: Double }- deriving (Show,Typeable,Data,Generic)+ deriving stock (Show,Data,Generic) instance Eq MaxD where MaxD a == MaxD b@@ -408,7 +454,7 @@ data BinomAcc = BinomAcc { binomAccSuccess :: !Int , binomAccTotal :: !Int }- deriving (Show,Eq,Ord,Typeable,Data,Generic)+ deriving stock (Show,Eq,Ord,Data,Generic) -- | Type restricted 'id' asBinomAcc :: BinomAcc -> BinomAcc@@ -428,7 +474,7 @@ -- | Value @a@ weighted by weight @w@ data Weighted w a = Weighted w a- deriving (Show,Eq,Ord,Typeable,Data,Generic,Functor,Foldable,Traversable)+ deriving stock (Show,Eq,Ord,Data,Generic,Functor,Foldable,Traversable) @@ -495,6 +541,16 @@ [| \(BinomAcc k n) -> (k,n) |] [| \(k,n) -> BinomAcc k n |] +instance VU.IsoUnbox CountW (Double,Double) where+ toURepr (CountW w w2) = (w,w2)+ fromURepr (w,w2) = CountW w w2+ {-# INLINE toURepr #-}+ {-# INLINE fromURepr #-}+newtype instance VU.MVector s CountW = MV_CountW (VU.MVector s (Double,Double))+newtype instance VU.Vector CountW = V_CountW (VU.Vector (Double,Double))+deriving via (CountW `VU.As` (Double,Double)) instance VGM.MVector VU.MVector CountW+deriving via (CountW `VU.As` (Double,Double)) instance VG.Vector VU.Vector CountW+instance VU.Unbox CountW -- $references --
monoid-statistics.cabal view
@@ -1,5 +1,5 @@ Name: monoid-statistics-Version: 1.1.2+Version: 1.1.3 Cabal-Version: >= 1.10 License: BSD3 License-File: LICENSE@@ -40,7 +40,7 @@ -- Build-Depends: base >=4.12 && <5 , exceptions >=0.10- , vector >=0.11 && <1+ , vector >=0.13 && <1 , vector-th-unbox >=0.2.1.6 , math-functions >=0.3 --