flat-mcmc

Painless, efficient, general-purpose sampling from continuous distributions.
git clone git://git.jtobin.io/flat-mcmc.git
Log | Files | Refs | README | LICENSE

Tests.hs (876B)


      1 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
      2 
      3 module Main where
      4 
      5 import Control.Monad
      6 import Control.Monad.State.Strict
      7 import qualified Data.Vector as V
      8 import qualified Data.Vector.Unboxed as U
      9 import Numeric.MCMC.Flat
     10 
     11 lRosenbrock :: Density
     12 lRosenbrock xs =
     13   let [x0, x1] = U.toList xs
     14   in  (-1) * (5 * (x1 - x0 ^ 2) ^ 2 + 0.05 * (1 - x0) ^ 2)
     15 
     16 defaultEnsemble :: Ensemble
     17 defaultEnsemble = V.fromList $ map U.fromList 
     18   [[0.1, 0.5], [0.8, 0.1], [1.0, 0.2], [0.9, 0.8], [-0.2, 0.3], [-0.1, 0.9]]
     19 
     20 opts :: Options
     21 opts = Options 10
     22 
     23 origin :: Chain
     24 origin = Chain {
     25     logObjective = lRosenbrock
     26   , ensemble     = defaultEnsemble
     27   , iterations   = 0
     28   , accepts      = 0
     29   } 
     30 
     31 -- cabal test --show-details=streaming
     32 main :: IO ()
     33 main = withSystemRandom . asGenIO $ \g -> do
     34   trace <- sample (replicateM 5000 (flatGranular opts) `evalStateT` origin) g
     35   print trace
     36