Copyright | (c) 2015 Jared Tobin |
---|---|
License | MIT |
Maintainer | Jared Tobin <jared@jtobin.ca> |
Stability | unstable |
Portability | ghc |
Safe Haskell | None |
Language | Haskell2010 |
This implementation performs slice sampling by first finding a bracket about a mode (using a simple doubling heuristic), and then doing rejection sampling along it. The result is a reliable and computationally inexpensive sampling routine.
The mcmc
function streams a trace to stdout to be processed elsewhere,
while the slice
transition can be used for more flexible purposes, such as
working with samples in memory.
See Neal, 2003 for the definitive reference of the algorithm.
Synopsis
- mcmc :: (MonadIO m, PrimMonad m, Show (t a), FoldableWithIndex (Index (t a)) t, Ixed (t a), Num (IxValue (t a)), Variate (IxValue (t a))) => Int -> IxValue (t a) -> t a -> (t a -> Double) -> Gen (PrimState m) -> m ()
- chain :: (PrimMonad m, FoldableWithIndex (Index (f a)) f, Ixed (f a), Variate (IxValue (f a)), Num (IxValue (f a))) => Int -> IxValue (f a) -> f a -> (f a -> Double) -> Gen (PrimState m) -> m [Chain (f a) b]
- slice :: (PrimMonad m, FoldableWithIndex (Index (t a)) t, Ixed (t a), Num (IxValue (t a)), Variate (IxValue (t a))) => IxValue (t a) -> Transition m (Chain (t a) b)
- create :: PrimMonad m => m (Gen (PrimState m))
- createSystemRandom :: IO GenIO
- withSystemRandom :: PrimBase m => (Gen (PrimState m) -> m a) -> IO a
- asGenIO :: (GenIO -> IO a) -> GenIO -> IO a
Documentation
mcmc :: (MonadIO m, PrimMonad m, Show (t a), FoldableWithIndex (Index (t a)) t, Ixed (t a), Num (IxValue (t a)), Variate (IxValue (t a))) => Int -> IxValue (t a) -> t a -> (t a -> Double) -> Gen (PrimState m) -> m () Source #
Trace n
iterations of a Markov chain and stream them to stdout.
>>>
let rosenbrock [x0, x1] = negate (5 *(x1 - x0 ^ 2) ^ 2 + 0.05 * (1 - x0) ^ 2)
>>>
withSystemRandom . asGenIO $ mcmc 3 1 [0, 0] rosenbrock
-3.854097694213343e-2,0.16688601288358407 -9.310661272172682e-2,0.2562387977415508 -0.48500122500661846,0.46245400501919076
chain :: (PrimMonad m, FoldableWithIndex (Index (f a)) f, Ixed (f a), Variate (IxValue (f a)), Num (IxValue (f a))) => Int -> IxValue (f a) -> f a -> (f a -> Double) -> Gen (PrimState m) -> m [Chain (f a) b] Source #
Trace n
iterations of a Markov chain and collect them in a list.
>>>
results <- withSystemRandom . asGenIO $ mcmc 3 1 [0, 0] rosenbrock
slice :: (PrimMonad m, FoldableWithIndex (Index (t a)) t, Ixed (t a), Num (IxValue (t a)), Variate (IxValue (t a))) => IxValue (t a) -> Transition m (Chain (t a) b) Source #
A slice sampling transition operator.
Re-exported
createSystemRandom :: IO GenIO #
Seed a PRNG with data from the system's fast source of pseudo-random numbers.
withSystemRandom :: PrimBase m => (Gen (PrimState m) -> m a) -> IO a #
Seed a PRNG with data from the system's fast source of pseudo-random numbers, then run the given action.
This function is unsafe and for example allows STRefs or any other mutable data structure to escape scope:
>>>
ref <- withSystemRandom $ \_ -> newSTRef 1
>>>
withSystemRandom $ \_ -> modifySTRef ref succ >> readSTRef ref
2>>>
withSystemRandom $ \_ -> modifySTRef ref succ >> readSTRef ref
3