flat-mcmc

Painless, efficient, general-purpose sampling from continuous distributions.
Log | Files | Refs | README | LICENSE

commit 22ea247b2977204402373def9cae33b3f203a020
parent d0a096b5cb722832fa59ea02c65801b21e92accf
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri, 28 Oct 2016 14:07:36 +1300

Use unboxed vectors (resolves #2).

Diffstat:
Mflat-mcmc.cabal | 14+++++++-------
Mlib/Numeric/MCMC/Flat.hs | 6+++---
Mstack.yaml | 2+-
Mtest/BNN.hs | 17+++++++++--------
Mtest/Rosenbrock.hs | 17+++++++++--------
5 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/flat-mcmc.cabal b/flat-mcmc.cabal @@ -1,5 +1,5 @@ name: flat-mcmc -version: 1.0.1 +version: 1.1.1 synopsis: Painless general-purpose sampling. homepage: http://jtobin.github.com/flat-mcmc license: MIT @@ -52,12 +52,12 @@ library hs-source-dirs: lib exposed-modules: Numeric.MCMC.Flat build-depends: - base < 5 + base > 4 && < 6 , mcmc-types >= 1.0.1 && < 2 - , monad-par - , monad-par-extras + , monad-par >= 0.3.4.8 && < 1 + , monad-par-extras >= 0.3.3 && < 1 , mwc-probability >= 1.0.1 && < 2 - , pipes >= 4 && < 5 + , pipes > 4 && < 5 , primitive , transformers , vector @@ -70,7 +70,7 @@ Test-suite rosenbrock ghc-options: -rtsopts -threaded build-depends: - base < 5 + base , flat-mcmc , vector @@ -82,7 +82,7 @@ Test-suite bnn ghc-options: -rtsopts -threaded build-depends: - base < 5 + base , flat-mcmc , vector diff --git a/lib/Numeric/MCMC/Flat.hs b/lib/Numeric/MCMC/Flat.hs @@ -64,7 +64,7 @@ instance Show Chain where . V.map show $ chainPosition -type Particle = Vector Double +type Particle = U.Vector Double type Ensemble = Vector Particle @@ -73,13 +73,13 @@ symmetric = fmap transform uniform where transform z = 0.5 * (z + 1) ^ (2 :: Int) stretch :: Particle -> Particle -> Double -> Particle -stretch p0 p1 z = V.zipWith (+) (V.map (* z) p0) (V.map (* (1 - z)) p1) +stretch p0 p1 z = U.zipWith (+) (U.map (* z) p0) (U.map (* (1 - z)) p1) acceptProb :: Target Particle -> Particle -> Particle -> Double -> Double acceptProb target particle proposal z = lTarget target proposal - lTarget target particle - + log z * (fromIntegral (V.length particle) - 1) + + log z * (fromIntegral (U.length particle) - 1) move :: Target Particle -> Particle -> Particle -> Double -> Double -> Particle move target p0 p1 z zc = diff --git a/stack.yaml b/stack.yaml @@ -2,4 +2,4 @@ flags: {} packages: - '.' extra-deps: [] -resolver: lts-5.2 +resolver: lts-7.0 diff --git a/test/BNN.hs b/test/BNN.hs @@ -3,18 +3,19 @@ module Main where import Numeric.MCMC.Flat -import Data.Vector (Vector, toList, fromList) +import qualified Data.Vector.Unboxed as U (Vector, toList, fromList) +import qualified Data.Vector as V (fromList) -bnn :: Vector Double -> Double +bnn :: U.Vector Double -> Double bnn xs = -0.5 * (x0 ^ 2 * x1 ^ 2 + x0 ^ 2 + x1 ^ 2 - 8 * x0 - 8 * x1) where - [x0, x1] = toList xs + [x0, x1] = U.toList xs ensemble :: Ensemble -ensemble = fromList [ - fromList [negate 1.0, negate 1.0] - , fromList [negate 1.0, 1.0] - , fromList [1.0, negate 1.0] - , fromList [1.0, 1.0] +ensemble = V.fromList [ + U.fromList [negate 1.0, negate 1.0] + , U.fromList [negate 1.0, 1.0] + , U.fromList [1.0, negate 1.0] + , U.fromList [1.0, 1.0] ] main :: IO () diff --git a/test/Rosenbrock.hs b/test/Rosenbrock.hs @@ -3,18 +3,19 @@ module Main where import Numeric.MCMC.Flat -import Data.Vector (Vector, toList, fromList) +import qualified Data.Vector.Unboxed as U (Vector, toList, fromList) +import qualified Data.Vector as V (fromList) -rosenbrock :: Vector Double -> Double +rosenbrock :: U.Vector Double -> Double rosenbrock xs = negate (5 *(x1 - x0 ^ 2) ^ 2 + 0.05 * (1 - x0) ^ 2) where - [x0, x1] = toList xs + [x0, x1] = U.toList xs ensemble :: Ensemble -ensemble = fromList [ - fromList [negate 1.0, negate 1.0] - , fromList [negate 1.0, 1.0] - , fromList [1.0, negate 1.0] - , fromList [1.0, 1.0] +ensemble = V.fromList [ + U.fromList [negate 1.0, negate 1.0] + , U.fromList [negate 1.0, 1.0] + , U.fromList [1.0, negate 1.0] + , U.fromList [1.0, 1.0] ] main :: IO ()