mcmc-types

Common types for implementing MCMC algorithms.
git clone git://git.jtobin.io/mcmc-types.git
Log | Files | Refs | README | LICENSE

Types.hs (2121B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 {-# LANGUAGE RecordWildCards #-}
      3 
      4 -- |
      5 -- Module: Data.Sampling.Types
      6 -- Copyright: (c) 2015 Jared Tobin
      7 -- License: MIT
      8 --
      9 -- Maintainer: Jared Tobin <jared@jtobin.ca>
     10 -- Stability: unstable
     11 -- Portability: ghc
     12 --
     13 -- Common types for implementing Markov Chain Monte Carlo (MCMC) algorithms.
     14 --
     15 -- 'Target' is a product type intended to hold a log-target density function and
     16 -- potentially its gradient.
     17 --
     18 -- The 'Chain' type represents a kind of annotated parameter space.
     19 -- Technically all that's required here is the type of the parameter space
     20 -- itself (held here in 'chainPosition') but in practice some additional
     21 -- information is typically useful.  Additionally there is 'chainScore' for
     22 -- holding the most recent score of the chain, as well as the target itself for
     23 -- implementing things like annealing.  The `chainTunables` field can be used
     24 -- to hold arbitrary data.
     25 --
     26 -- One should avoid exploiting these features to do something nasty (like, say,
     27 -- invalidating the Markov property).
     28 --
     29 -- The 'Transition' type permits probabilistic transitions over some state
     30 -- space by way of the underlying 'Prob' monad.
     31 
     32 module Data.Sampling.Types (
     33     Transition
     34   , Chain(..)
     35   , Target(..)
     36   ) where
     37 
     38 import Control.Monad.Trans.State.Strict (StateT)
     39 import System.Random.MWC.Probability (Prob)
     40 
     41 -- | A generic transition operator.
     42 --
     43 --   Has access to randomness via the underlying 'Prob' monad.
     44 type Transition m a = StateT a (Prob m) ()
     45 
     46 -- | The @Chain@ type specifies the state of a Markov chain at any given
     47 --   iteration.
     48 data Chain a b = Chain {
     49     chainTarget   :: Target a
     50   , chainScore    :: !Double
     51   , chainPosition :: a
     52   , chainTunables :: Maybe b
     53   }
     54 
     55 instance Show a => Show (Chain a b) where
     56   show Chain {..} = filter (`notElem` "fromList []") (show chainPosition)
     57 
     58 -- | A @Target@ consists of a function from parameter space to the reals, as
     59 --   well as possibly a gradient.
     60 --
     61 --   Most implementations assume a /log/-target, so records are named
     62 --   accordingly.
     63 data Target a = Target {
     64     lTarget  :: a -> Double
     65   , glTarget :: Maybe (a -> a)
     66   }
     67