mcmc-types

Common types for implementing MCMC algorithms.
Log | Files | Refs | README | LICENSE

commit 83e8dfa11786e7b8bcdc458509e0de719da74604
parent 2b870a193d9aafcf397420a3198429af44ae5abc
Author: Jared Tobin <jared@jtobin.ca>
Date:   Tue,  6 Oct 2015 16:14:56 +1300

Reorg.

Diffstat:
MData/Sampling/Types.hs | 97++++++++++++++++++-------------------------------------------------------------
Mmcmc-types.cabal | 8+++++---
Mstack.yaml | 5+++--
3 files changed, 30 insertions(+), 80 deletions(-)

diff --git a/Data/Sampling/Types.hs b/Data/Sampling/Types.hs @@ -1,87 +1,34 @@ +{-# OPTIONS_GHC -Wall #-} +{-# LANGUAGE RecordWildCards #-} module Data.Sampling.Types ( - Parameter(..) - , continuous - , discrete - , continuousVector - , discreteVector - , Target - , Parameters - , Observations - , createTargetWithGradient - , createTargetWithoutGradient - , handleGradient - , lTarget - , glTarget + Transition + , Chain(..) + , Target(..) ) where -import Data.Map (Map) +import Control.Monad.Trans.State.Strict (StateT) +import System.Random.MWC.Probability (Prob) -data Parameter = - Continuous Double - | Discrete Int - | ContinuousVector [Double] - | DiscreteVector [Int] - deriving (Eq, Show) +-- | A transition operator. +type Transition m a = StateT a (Prob m) () -discrete :: Int -> Parameter -discrete = Discrete - -continuous :: Double -> Parameter -continuous = Continuous - -continuousVector :: [Double] -> Parameter -continuousVector = ContinuousVector - -discreteVector :: [Int] -> Parameter -discreteVector = DiscreteVector - -instance Num Parameter where - (Continuous a) + (Continuous b) = Continuous (a + b) - (Discrete a) + (Discrete b) = Discrete (a + b) - _ + _ = noInstanceError - - (Continuous a) - (Continuous b) = Continuous (a - b) - (Discrete a) - (Discrete b) = Discrete (a - b) - _ - _ = noInstanceError - - (Continuous a) * (Continuous b) = Continuous (a * b) - (Discrete a) * (Discrete b) = Discrete (a * b) - _ * _ = noInstanceError - - abs (Continuous a) = Continuous (abs a) - abs (Discrete a) = Discrete (abs a) - abs _ = noInstanceError - - signum (Continuous a) = Continuous (signum a) - signum (Discrete a) = Discrete (signum a) - signum _ = noInstanceError - - fromInteger = Continuous . fromInteger +-- | The @Chain@ type specifies the state of a Markov chain at any given +-- iteration. +data Chain a b = Chain { + chainTarget :: Target a + , chainScore :: !Double + , chainPosition :: a + , chainTunables :: Maybe b + } -noInstanceError :: t -noInstanceError = error - "only continuous and discrete parameters support ring operations" +instance Show a => Show (Chain a b) where + show Chain {..} = filter (`notElem` "fromList []") (show chainPosition) -- | A @Target@ consists of a function from parameter space to the reals, as -- well as possibly a gradient. -data Target = Target { - lTarget :: Parameters -> Double - , glTarget :: Maybe (Parameters -> Parameters) +data Target a = Target { + lTarget :: a -> Double + , glTarget :: Maybe (a -> a) } -createTargetWithGradient - :: (Parameters -> Double) -> (Parameters -> Parameters) -> Target -createTargetWithGradient f g = Target f (Just g) - -createTargetWithoutGradient :: (Parameters -> Double) -> Target -createTargetWithoutGradient f = Target f Nothing - -handleGradient :: Maybe t -> t -handleGradient Nothing = error "handleGradient: target has no gradient" -handleGradient (Just g) = g - -type Environment a = Map String a -type Parameters = Environment Parameter -type Observations = Environment Parameter - diff --git a/mcmc-types.cabal b/mcmc-types.cabal @@ -1,5 +1,5 @@ name: mcmc-types -version: 0.1.0.4 +version: 0.2.0.0 synopsis: Common types for sampling. description: Common types for sampling. homepage: http://github.com/jtobin/mcmc-types @@ -8,12 +8,14 @@ license-file: LICENSE author: Jared Tobin maintainer: jared@jtobin.ca build-type: Simple -cabal-version: >=1.10 +cabal-version: >= 1.18 library exposed-modules: Data.Sampling.Types default-language: Haskell2010 build-depends: - base >= 4.7 && < 4.8 + base , containers + , mwc-probability + , transformers diff --git a/stack.yaml b/stack.yaml @@ -1,5 +1,6 @@ flags: {} packages: -- '.' + - '.' + - ../mwc-probability extra-deps: [] -resolver: lts-2.21 +resolver: lts-3.3