Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weightedMean #22

Closed
wants to merge 16 commits into from
31 changes: 31 additions & 0 deletions src/Streamly/Statistics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ module Streamly.Statistics
, sumInt
, mean
, welfordMean
, weightedMean
)
where

import Control.Monad.IO.Class (MonadIO(..))
import Data.Bifunctor(bimap)
import Data.Function ((&))
import Data.Maybe (fromMaybe)
import Foreign.Ptr (castPtr, plusPtr)
import Foreign.Storable

import Streamly.Data.Fold.Tee(Tee(..), toFold)
import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..))
import Streamly.Internal.Data.Tuple.Strict
Expand All @@ -33,6 +37,19 @@ import qualified Streamly.Data.Fold as Fold

import Prelude hiding (sum, min, max)

instance Storable (Double, Double) where
sizeOf _ = 2 * sizeOf (undefined :: Double)
alignment _ = 16
peek p = do
let q = castPtr p
a <- peek q
b <- peek (q `plusPtr` sizeOf a)
return (a, b)
poke p (a, b) = do
let q = castPtr p
poke q a
poke (q `plusPtr` sizeOf a) b

specdrake marked this conversation as resolved.
Show resolved Hide resolved
-- XXX Make the following more numerically stable. Try to extend welfordMean
-- XXX method.
-- XXX - stdDev
Expand Down Expand Up @@ -271,3 +288,17 @@ welfordMean = Fold step initial extract
{-# INLINE geometricMean #-}
geometricMean :: forall m a. (MonadIO m, Floating a) => Fold m (a, Maybe a) a
geometricMean = exp <$> Fold.lmap (bimap log (log <$>)) mean

-- | @weightedMean@ computes the weighted mean of the sample data.
--
-- The weights should add up to 1. It uses Kahan-Babuska-Neumaier summation.
--
{-# INLINE weightedMean #-}
weightedMean :: forall m a. (MonadIO m, Fractional a)
=> Fold m ((a, a), Maybe (a, a)) a
weightedMean = Fold.lmap func sum
specdrake marked this conversation as resolved.
Show resolved Hide resolved

where

func ((w, v), Nothing) = (w * v, Nothing)
func ((w, v), Just (w1, v1)) = (w * v, Just $ w1 * v1)