packages feed

mwc-probability-transition (empty) → 0.1.0.0

raw patch · 6 files changed

+181/−0 lines, 6 filesdep +QuickCheckdep +basedep +ghc-primsetup-changed

Dependencies added: QuickCheck, base, ghc-prim, hspec, logging-effect, mtl, mwc-probability, mwc-probability-transition, primitive, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Marco Zocca (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Marco Zocca nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# mwc-probability-transition++[![Build Status](https://travis-ci.org/ocramz/mwc-probability-transition.png)](https://travis-ci.org/ocramz/mwc-probability-transition)++Types and primitives for stochastic simulation (e.g. integration of SDE, random walks, Markov Chain Monte Carlo algorithms etc.)
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ mwc-probability-transition.cabal view
@@ -0,0 +1,49 @@+name:                mwc-probability-transition+version:             0.1.0.0+synopsis:            A Markov stochastic transition operator with logging+description:+            .+            This package provides a 'Transition' type that is useful for modelling (and debugging) stochastic transition kernels (used in e.g. the integration of SDEs, Markov chain Monte Carlo algorithms etc.).+            .+            It wraps the compositional random sampling functionality of `mwc-probability` and offers structured logging via `logging-effect`.+homepage:            https://github.com/ocramz/mwc-probability-transition+license:             BSD3+license-file:        LICENSE+author:              Marco Zocca+maintainer:          zocca.marco gmail+copyright:           2018 Marco Zocca+category:            Numeric+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10+tested-with:         GHC == 8.2.2++library+  default-language:    Haskell2010+  ghc-options:         -Wall+  hs-source-dirs:      src+  exposed-modules:     System.Random.MWC.Probability.Transition+  build-depends:       base >= 4.7 && < 5+                     , ghc-prim+                     , logging-effect+                     , mtl+                     , mwc-probability+                     , primitive+                     , transformers++++test-suite spec+  default-language:    Haskell2010+  ghc-options:         -Wall+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             LibSpec.hs+  build-depends:       base+                     , mwc-probability-transition+                     , hspec+                     , QuickCheck++source-repository head+  type:     git+  location: https://github.com/ocramz/mwc-probability-transition
+ src/System/Random/MWC/Probability/Transition.hs view
@@ -0,0 +1,79 @@+{-# language OverloadedStrings #-}+{-# language DeriveFunctor, GeneralizedNewtypeDeriving #-}+module System.Random.MWC.Probability.Transition (+  -- * Transition+    Transition+  , mkTransition+  , runTransition+  -- ** Helper functions+  , withSeverity+  -- * Re-exported from `logging-effect`+  , Handler+  , WithSeverity(..), Severity(..)+  -- , withFDHandler, defaultBatchingOptions+  ) where++import Control.Monad+import Control.Monad.Primitive++import qualified Control.Monad.State as S++import Control.Monad.Trans.Class (MonadTrans(..), lift)+import Control.Monad.Trans.State.Strict (StateT(..), evalStateT, execStateT, runStateT)+import Control.Monad.Log (MonadLog(..), Handler, WithSeverity(..), Severity(..), LoggingT(..), runLoggingT, withFDHandler, defaultBatchingOptions, logMessage)++import Data.Char++import System.Random.MWC.Probability++++-- | A Markov transition kernel.+newtype Transition message s m a = Transition (+  Gen (PrimState m) -> StateT s (LoggingT message m) a+  ) deriving (Functor)++-- | Construct a 'Transition' from sampling, state transformation and logging functions.+--+-- NB: The three function arguments are used in the order in which they appear here:+--+-- 1. a random sample @w :: t@ is produced, using the current state @x :: s@ as input+--+-- 2. output @z :: a@ and next state @x' :: s@ are computed using @w@ and @x@+--+-- 3. a logging message is constructed, using @z@ and @x'@ as arguments.+mkTransition :: Monad m =>+        (s -> Prob m t)     -- ^ Random generation+     -> (s -> t -> (a, s))  -- ^ (Output, Next state)+     -> (a -> s -> message) -- ^ Log message generation+     -> Transition message s m a+mkTransition fm fs flog = Transition $ \gen -> do+  s <- S.get+  w <- lift . lift $ sample (fm s) gen+  let (a, s') = fs s w+  lift $ logMessage $ flog a s' +  S.put s'+  return a++-- | Run a 'Transition' for a number of steps, while logging each iteration.+runTransition :: Monad m =>+         Handler m message        -- ^ Logging handler+      -> Transition message s m a+      -> Int                      -- ^ Number of iterations +      -> s                        -- ^ Initial state+      -> Gen (PrimState m)        -- ^ PRNG+      -> m [(a, s)]+runTransition logf (Transition fm) n s0 g =+  runLoggingT (replicateM n (runStateT (fm g) s0)) logf++++  +bracketsUpp :: Show a => a -> String+bracketsUpp p = unwords ["[", map toUpper (show p), "]"]++-- | Render a logging message along with an annotation of its severity.+withSeverity :: (t -> String) -> WithSeverity t -> String+withSeverity k (WithSeverity u a ) = unwords [bracketsUpp u, k a]++
+ test/LibSpec.hs view
@@ -0,0 +1,16 @@+module Main where++import Test.Hspec+import Test.Hspec.QuickCheck+++main :: IO ()+main = hspec spec++spec :: Spec+spec =+  describe "Lib" $ do+    it "works" $ do+      True `shouldBe` True+    -- prop "ourAdd is commutative" $ \x y ->+    --   ourAdd x y `shouldBe` ourAdd y x