speedy-slice 0.2.0 → 0.3.0
raw patch · 4 files changed
+47/−10 lines, 4 filesdep +kan-extensions
Dependencies added: kan-extensions
Files
- Numeric/MCMC/Slice.hs +36/−4
- speedy-slice.cabal +5/−3
- test/BNN.hs +3/−1
- test/Rosenbrock.hs +3/−2
Numeric/MCMC/Slice.hs view
@@ -28,6 +28,7 @@ module Numeric.MCMC.Slice ( mcmc+ , chain , slice -- * Re-exported@@ -37,6 +38,8 @@ , MWC.asGenIO ) where +import Control.Monad (replicateM)+import Control.Monad.Codensity (lowerCodensity) import Control.Monad.Trans.State.Strict (put, get, execStateT) import Control.Monad.Primitive (PrimMonad, PrimState) import Control.Monad.IO.Class (MonadIO, liftIO)@@ -66,7 +69,7 @@ -> Gen (PrimState m) -> m () mcmc n radial chainPosition target gen = runEffect $- chain radial Chain {..} gen+ drive radial Chain {..} gen >-> Pipes.take n >-> Pipes.mapM_ (liftIO . print) where@@ -74,15 +77,44 @@ chainTunables = Nothing chainTarget = Target target Nothing --- A Markov chain driven by the slice transition operator.+-- | Trace 'n' iterations of a Markov chain and collect them in a list.+--+-- >>> results <- withSystemRandom . asGenIO $ mcmc 3 1 [0, 0] rosenbrock 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]+chain n radial position target gen = runEffect $+ drive radial origin gen+ >-> collect n+ where+ ctarget = Target target Nothing++ origin = Chain {+ chainScore = lTarget ctarget position+ , chainTunables = Nothing+ , chainTarget = ctarget+ , chainPosition = position+ }++ collect :: Monad m => Int -> Consumer a m [a]+ collect size = lowerCodensity $+ replicateM size (lift Pipes.await)++-- A Markov chain driven by the slice transition operator.+drive :: (PrimMonad m, FoldableWithIndex (Index (t a)) t, Ixed (t a), Num (IxValue (t a)), Variate (IxValue (t a))) => IxValue (t a) -> Chain (t a) b -> Gen (PrimState m)- -> Producer (Chain (t a) b) m ()-chain radial = loop where+ -> Producer (Chain (t a) b) m c+drive radial = loop where loop state prng = do next <- lift (MWC.sample (execStateT (slice radial) state) prng) yield next
speedy-slice.cabal view
@@ -1,5 +1,5 @@ name: speedy-slice-version: 0.2.0+version: 0.3.0 synopsis: Speedy slice sampling. homepage: http://github.com/jtobin/speedy-slice license: MIT@@ -20,8 +20,9 @@ Additionally you can sample over anything that's an instance of both 'Num' and 'Variate', which is useful in the case of discrete parameters. .- Exports a 'mcmc' function that prints a trace to stdout, as well as a- 'slice' transition operator that can be used more generally.+ Exports a 'mcmc' function that prints a trace to stdout, a 'chain' function+ for collecting results in memory, and a 'slice' transition operator that can+ be used more generally. . > import Numeric.MCMC.Slice > import Data.Sequence (Seq, index, fromList)@@ -44,6 +45,7 @@ Numeric.MCMC.Slice build-depends: base >= 4 && < 6+ , kan-extensions >= 5 && < 6 , lens >= 4 && < 5 , primitive >= 0.6 && < 1.0 , mcmc-types >= 1.0.1
test/BNN.hs view
@@ -11,5 +11,7 @@ x1 = index xs 1 main :: IO ()-main = withSystemRandom . asGenIO $ mcmc 100 1 (fromList [0, 0]) bnn+main = withSystemRandom . asGenIO $ \gen -> do+ _ <- chain 50 1 (fromList [0, 0]) bnn gen+ mcmc 50 1 (fromList [0, 0]) bnn gen
test/Rosenbrock.hs view
@@ -8,6 +8,7 @@ rosenbrock [x0, x1] = negate (5 *(x1 - x0 ^ 2) ^ 2 + 0.05 * (1 - x0) ^ 2) main :: IO ()-main = withSystemRandom . asGenIO $ mcmc 100 1 [0, 0] rosenbrock-+main = withSystemRandom . asGenIO $ \gen -> do+ _ <- chain 50 1 [0, 0] rosenbrock gen+ mcmc 50 1 [0, 0] rosenbrock gen