packages feed

mealy 0.4.5.0 → 0.5.0.0

raw patch · 4 files changed

+31/−22 lines, 4 filesdep +harpiedep +harpie-numhaskdep −numhask-arrayPVP ok

version bump matches the API change (PVP)

Dependencies added: harpie, harpie-numhask

Dependencies removed: numhask-array

API changes (from Hackage documentation)

- Data.Mealy: Mealy :: (a -> c) -> (c -> a -> c) -> (c -> b) -> Mealy a b
+ Data.Mealy: Mealy :: (a -> s) -> (s -> a -> s) -> (s -> b) -> Mealy a b

Files

ChangeLog.md view
@@ -1,3 +1,9 @@+0.5+==++- Switched to harpie, away from numhask-array++ 0.4.4 === 
mealy.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: mealy-version: 0.4.5.0+version: 0.5.0.0 license: BSD-3-Clause license-file: LICENSE copyright: Tony Day (c) 2013@@ -11,8 +11,10 @@ bug-reports: https://github.com/tonyday567/mealy/issues synopsis: Mealy machines for processing time-series and ordered data. description:-    @mealy@ provides support for computing statistics (such as an average or a standard deviation) as current state. Usage is to supply a decay function representing the relative weights of recent values versus older ones, in the manner of exponentially-weighted averages. The library attempts to be polymorphic in the statistic which can be combined in applicative style. +    @mealy@ reimagines statistics as a [mealy machine](https://en.wikipedia.org/wiki/Mealy_machine) processing data with some form of order such as time-series data. The 'Mealy', with the help of a decay function specifying the relative weights of recent values versus older value, can be treated as a compression or summary of the data stream into 'current state.'+    Mealies are highly polymorphic, situated at a busy crossroad of theory and practice, and lend themselves to ergonmic, compact and realistic representations of a wide range of online phenomena.+     == Usage      >>> import Mealy@@ -56,7 +58,8 @@         , containers        >=0.6 && <0.8         , mwc-probability   >=2.3.1 && <2.4         , numhask           >=0.11 && <0.13-        , numhask-array     >=0.11 && <0.12+        , harpie            >=0.1 && <0.2+        , harpie-numhask    >=0.1 && <0.2         , primitive         >=0.7.2 && <0.10         , profunctors       >=5.6.2 && <5.7         , tdigest           >=0.2.1 && <0.4
readme.org view
@@ -10,8 +10,7 @@  A sum, for example, looks like ~M id (+) id~ where the first id is the initial injection and the second id is the covariant extraction. -This library provides support for computing statistics (such as an average or a standard deviation)-as current state within a mealy context.+This library provides support for computing statistics (such as an average or a standard deviation) as current state within a mealy context.  * Usage 
src/Data/Mealy.hs view
@@ -4,7 +4,7 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE NoImplicitPrelude #-} --- | Online statistics for ordered data (such as time-series data), modelled as [mealy machines](https://en.wikipedia.org/wiki/Mealy_machine)+-- | A 'Mealy' is a polymorphic statistic reimagined as a [mealy machine](https://en.wikipedia.org/wiki/Mealy_machine) processing and summarising data with some form of order (such as time-series data), where recent data is less relevant than old data. module Data.Mealy   ( -- * Types     Mealy (..),@@ -66,7 +66,9 @@ import Data.Text (Text) import Data.Typeable (Typeable) import GHC.TypeLits-import NumHask.Array as F+import Harpie.Fixed qualified as F+import Harpie.NumHask qualified as N+import Harpie.Shape qualified as S import NumHask.Prelude hiding (asum, diff, fold, id, last, (.))  -- $setup@@ -75,6 +77,7 @@ -- >>> import Control.Category ((>>>)) -- >>> import Data.List -- >>> import Data.Mealy.Simulate+-- >>> import Harpie.Fixed qualified as F -- >>> g <- create -- >>> xs0 <- rvs g 10000 -- >>> xs1 <- rvs g 10000@@ -103,11 +106,11 @@  -- | A 'Mealy' a b is a triple of functions ----- * (a -> s) __inject__ Convert an input into the state type.+-- * (a -> s) __inject__ Convert an input into an internal state type. -- * (s -> a -> s) __step__ Update state given prior state and (new) input. -- * (s -> b) __extract__ Convert state to the output type. ----- By adopting this order, a Mealy sum looks like:+-- By adopting this order, a sum, for example, looks like: -- -- > M id (+) id --@@ -116,7 +119,7 @@ -- __inject__ kicks off state on the initial element of the Foldable, but is otherwise  independent of __step__. -- -- > scan (M i s e) (x : xs) = e <$> scanl' s (i x) xs-data Mealy a b = forall c. Mealy (a -> c) (c -> a -> c) (c -> b)+data Mealy a b = forall s. Mealy (a -> s) (s -> a -> s) (s -> b)  -- | Strict Pair data Pair' a b = Pair' !a !b deriving (Eq, Ord, Show, Read)@@ -401,16 +404,16 @@ -- \end{align} -- \] ----- > let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2--- > let zs = zip (zipWith (\x y -> fromList [x,y] :: F.Array '[2] Double) xs1 xs2) ys--- > fold (beta 0.99) zs--- [0.4982692361226971, 1.038192474255091]+-- >>> let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2+-- >>> let zs = zip (zipWith (\x y -> F.array @'[2] [x,y]) xs1 xs2) ys+-- >>> fold (beta 0.99) zs+-- [0.6228820021456606,0.8461936860075405] beta :: (ExpField a, KnownNat n, Eq a) => a -> Mealy (F.Array '[n] a, a) (F.Array '[n] a) beta r = M inject step extract   where     -- extract :: Averager (RegressionState n a) a -> (F.Array '[n] a)     extract (A (RegressionState xx x xy y) c) =-      (\a b -> recip a `F.mult` b)+      (\a b -> recip a `N.mult` b)         ((one / c) *| (xx - F.expand (*) x x))         ((xy - (y *| x)) |* (one / c))     step x (xs, y) = rsOnline r x (inject (xs, y))@@ -430,7 +433,7 @@ alpha r = (\xs b y -> y - sum (liftR2 (*) b xs)) <$> lmap fst (arrayify $ ma r) <*> beta r <*> lmap snd (ma r) {-# INLINEABLE alpha #-} -arrayify :: (HasShape s) => Mealy a b -> Mealy (F.Array s a) (F.Array s b)+arrayify :: (S.KnownNats s) => Mealy a b -> Mealy (F.Array s a) (F.Array s b) arrayify (M sExtract sStep sInject) = M extract step inject   where     extract = fmap sExtract@@ -439,10 +442,10 @@  -- | multiple regression ----- > let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2--- > let zs = zip (zipWith (\x y -> fromList [x,y] :: F.Array '[2] Double) xs1 xs2) ys--- > fold (reg 0.99) zs--- ([0.4982692361226971, 1.038192474255091],2.087160803386695e-3)+-- >>> let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2+-- >>> let zs = zip (zipWith (\x y -> F.array @'[2] [x,y]) xs1 xs2) ys+-- >>> fold (reg 0.99) zs+-- ([0.6228820021456606,0.8461936860075405],2.536775201287266e-2) reg :: (ExpField a, KnownNat n, Eq a) => a -> Mealy (F.Array '[n] a, a) (F.Array '[n] a, a) reg r = (,) <$> beta r <*> alpha r {-# INLINEABLE reg #-}@@ -560,8 +563,6 @@ {-# INLINEABLE onlineL1 #-}  -- | moving median--- > L.fold (maL1 inc d r) [1..n]--- 93.92822312742108 maL1 :: (Ord a, Field a, Absolute a) => a -> a -> a -> Mealy a a maL1 i d r = onlineL1 i d id (* r) {-# INLINEABLE maL1 #-}