mcmc 0.2.3 → 0.2.4
raw patch · 21 files changed
+147/−141 lines, 21 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Mcmc.Item: instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Numeric.Log.Log a)
- Mcmc.Item: instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Numeric.Log.Log a)
- Mcmc.Proposal: ProposalSimple :: (a -> GenIO -> IO (a, Log Double)) -> ProposalSimple a
- Mcmc.Proposal: [pSample] :: ProposalSimple a -> a -> GenIO -> IO (a, Log Double)
- Mcmc.Proposal: newtype ProposalSimple a
+ Mcmc: scaleBactrian :: Double -> Double -> String -> Int -> Bool -> Proposal Double
+ Mcmc: scaleContrarily :: Double -> Double -> String -> Int -> Bool -> Proposal (Double, Double)
+ Mcmc: slideContrarily :: Double -> Double -> String -> Int -> Bool -> Proposal (Double, Double)
+ Mcmc.Monitor.Parameter: (>$<) :: Contravariant f => (a -> b) -> f b -> f a
+ Mcmc.Monitor.Parameter: infixl 4 >$<
+ Mcmc.Monitor.Parameter: instance Data.Functor.Contravariant.Contravariant Mcmc.Monitor.Parameter.MonitorParameter
+ Mcmc.Monitor.ParameterBatch: (>$<) :: Contravariant f => (a -> b) -> f b -> f a
+ Mcmc.Monitor.ParameterBatch: infixl 4 >$<
+ Mcmc.Monitor.ParameterBatch: instance Data.Functor.Contravariant.Contravariant Mcmc.Monitor.ParameterBatch.MonitorParameterBatch
+ Mcmc.Proposal: type ProposalSimple a = a -> GenIO -> IO (a, Log Double)
- Mcmc: scale :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc: scale :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc: scaleUnbiased :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc: scaleUnbiased :: Double -> String -> Int -> Bool -> Proposal Double
- Mcmc: slide :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc: slide :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc: slideBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc: slideBactrian :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc: slideSymmetric :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc: slideSymmetric :: Double -> String -> Int -> Bool -> Proposal Double
- Mcmc: slideUniform :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc: slideUniform :: Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal: createProposal :: String -> Int -> (Double -> ProposalSimple a) -> Bool -> Proposal a
+ Mcmc.Proposal: createProposal :: (Double -> ProposalSimple a) -> String -> Int -> Bool -> Proposal a
- Mcmc.Proposal.Bactrian: scaleBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Bactrian: scaleBactrian :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Bactrian: slideBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Bactrian: slideBactrian :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Scale: scale :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Scale: scale :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Scale: scaleContrarily :: String -> Int -> Double -> Double -> Bool -> Proposal (Double, Double)
+ Mcmc.Proposal.Scale: scaleContrarily :: Double -> Double -> String -> Int -> Bool -> Proposal (Double, Double)
- Mcmc.Proposal.Scale: scaleUnbiased :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Scale: scaleUnbiased :: Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Slide: slide :: String -> Int -> Double -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Slide: slide :: Double -> Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Slide: slideContrarily :: String -> Int -> Double -> Double -> Bool -> Proposal (Double, Double)
+ Mcmc.Proposal.Slide: slideContrarily :: Double -> Double -> String -> Int -> Bool -> Proposal (Double, Double)
- Mcmc.Proposal.Slide: slideSymmetric :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Slide: slideSymmetric :: Double -> String -> Int -> Bool -> Proposal Double
- Mcmc.Proposal.Slide: slideUniform :: String -> Int -> Double -> Bool -> Proposal Double
+ Mcmc.Proposal.Slide: slideUniform :: Double -> String -> Int -> Bool -> Proposal Double
Files
- README.md +1/−2
- bench/Normal.hs +2/−2
- bench/Poisson.hs +4/−4
- mcmc.cabal +1/−1
- src/Mcmc.hs +4/−1
- src/Mcmc/Item.hs +11/−16
- src/Mcmc/Mcmc.hs +2/−0
- src/Mcmc/Metropolis.hs +3/−1
- src/Mcmc/Monitor.hs +0/−8
- src/Mcmc/Monitor/Parameter.hs +17/−1
- src/Mcmc/Monitor/ParameterBatch.hs +14/−0
- src/Mcmc/Prior.hs +3/−5
- src/Mcmc/Proposal.hs +15/−15
- src/Mcmc/Proposal/Bactrian.hs +18/−16
- src/Mcmc/Proposal/Generic.hs +2/−2
- src/Mcmc/Proposal/Scale.hs +14/−14
- src/Mcmc/Proposal/Slide.hs +16/−16
- src/Mcmc/Save.hs +4/−19
- src/Mcmc/Status.hs +10/−12
- test/Mcmc/ProposalSpec.hs +2/−2
- test/Mcmc/SaveSpec.hs +4/−4
README.md view
@@ -6,8 +6,7 @@ Sample from a posterior using Markov chain Monte Carlo methods. At the moment, the library is tailored to the Metropolis-Hastings algorithm-since it covers most use cases. However, implementation of more algorithms is-planned in the future.+since it covers most use cases. More algorithms will be implemented soon. ## Documentation
bench/Normal.hs view
@@ -37,7 +37,7 @@ proposals :: Cycle Double proposals = fromList- [slideSymmetric "medium" 1 1.0 True]+ [slideSymmetric 1.0 "medium" 1 True] mons :: [MonitorParameter Double] mons = [monitorDouble "mu"]@@ -68,7 +68,7 @@ proposalsBactrian :: Cycle Double proposalsBactrian = fromList- [slideBactrian "bactrian" 1 0.5 1.0 True]+ [slideBactrian 0.5 1.0 "bactrian" 1 True] normalBactrianBench :: GenIO -> IO () normalBactrianBench g = do
bench/Poisson.hs view
@@ -48,10 +48,10 @@ product [f ft yr x | (ft, yr) <- zip fatalities normalizedYears] proposalAlpha :: Proposal I-proposalAlpha = _1 @~ slideSymmetric "alpha" 2 0.2 False+proposalAlpha = _1 @~ slideSymmetric 0.2 "alpha" 2 False proposalBeta :: Proposal I-proposalBeta = _2 @~ slideSymmetric "beta" 1 0.2 False+proposalBeta = _2 @~ slideSymmetric 0.2 "beta" 1 False proposals :: Cycle I proposals = fromList [proposalAlpha, proposalBeta]@@ -60,10 +60,10 @@ initial = (0, 0) monAlpha :: MonitorParameter I-monAlpha = fst @. monitorDouble "alpha"+monAlpha = fst >$< monitorDouble "alpha" monBeta :: MonitorParameter I-monBeta = snd @. monitorDouble "beta"+monBeta = snd >$< monitorDouble "beta" monStd :: MonitorStdOut I monStd = monitorStdOut [monAlpha, monBeta] 150
mcmc.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: mcmc-version: 0.2.3+version: 0.2.4 synopsis: Sample from a posterior using Markov chain Monte Carlo description: Please see the README on GitHub at <https://github.com/dschrempf/mcmc#readme> category: Math, Statistics
src/Mcmc.hs view
@@ -86,10 +86,13 @@ (@~), scale, scaleUnbiased,+ scaleContrarily,+ scaleBactrian, slide,- slideBactrian, slideSymmetric, slideUniform,+ slideContrarily,+ slideBactrian, Cycle, fromList, Order (..),
src/Mcmc/Item.hs view
@@ -1,5 +1,4 @@ {-# LANGUAGE OverloadedStrings #-}-{-# OPTIONS_GHC -fno-warn-orphans #-} -- | -- Module : Mcmc.Item@@ -18,15 +17,9 @@ where import Data.Aeson+import Data.Aeson.Types import Numeric.Log -instance ToJSON a => ToJSON (Log a) where- toJSON (Exp x) = toJSON x- toEncoding (Exp x) = toEncoding x--instance FromJSON a => FromJSON (Log a) where- parseJSON v = Exp <$> parseJSON v- -- | An 'Item', or link of the Markov chain. For reasons of computational -- efficiency, each state is associated with the corresponding prior and -- likelihood.@@ -41,13 +34,15 @@ deriving (Eq, Ord, Show, Read) instance ToJSON a => ToJSON (Item a) where- toJSON (Item x p l) = object ["s" .= x, "p" .= p, "l" .= l]- toEncoding (Item x p l) = pairs ("s" .= x <> "p" .= p <> "l" .= l)+ toJSON (Item x (Exp p) (Exp l)) = object ["s" .= x, "p" .= p, "l" .= l]+ toEncoding (Item x (Exp p) (Exp l)) = pairs ("s" .= x <> "p" .= p <> "l" .= l) +item :: FromJSON a => Object -> Parser (Item a)+item v = do+ s <- v .: "s"+ p <- v .: "p"+ l <- v .: "l"+ return $ Item s (Exp p) (Exp l)+ instance FromJSON a => FromJSON (Item a) where- parseJSON = withObject "Item" $- \v ->- Item- <$> v .: "s"- <*> v .: "p"- <*> v .: "l"+ parseJSON = withObject "Item" item
src/Mcmc/Mcmc.hs view
@@ -11,6 +11,8 @@ -- Portability : portable -- -- Creation date: Fri May 29 10:19:45 2020.+--+-- Functions to work with the 'Mcmc' state transformer. module Mcmc.Mcmc ( Mcmc, mcmcOutT,
src/Mcmc/Metropolis.hs view
@@ -12,6 +12,8 @@ -- Portability : portable -- -- Creation date: Tue May 5 20:11:30 2020.+--+-- Metropolis-Hastings algorithm. module Mcmc.Metropolis ( mh, mhContinue,@@ -51,7 +53,7 @@ mhPropose :: Proposal a -> Mcmc a () mhPropose m = do- let p = pSample $ pSimple m+ let p = pSimple m s <- get let (Item x pX lX) = item s pF = priorF s
src/Mcmc/Monitor.hs view
@@ -149,10 +149,6 @@ mfPeriod :: Int } --- XXX: The file monitor also includes iteration, prior, likelihood, and--- posterior. What if I want to log trees; or other complex objects? In this--- case, we need a simpler monitor to a file.- -- | Monitor parameters to a file. monitorFile :: -- | Name; used as part of the file name.@@ -246,10 +242,6 @@ mbParams :: [MonitorParameterBatch a], mbSize :: Int }---- XXX: The batch monitor also includes iteration, prior, likelihood, and--- posterior. What if I want to log trees; or other complex objects? In this--- case, we need a simpler monitor to a file. -- | Monitor parameters to a file, see 'MonitorBatch'. monitorBatch ::
src/Mcmc/Monitor/Parameter.hs view
@@ -14,6 +14,7 @@ module Mcmc.Monitor.Parameter ( -- * Parameter monitors MonitorParameter (..),+ (>$<), (@.), monitorInt, monitorDouble,@@ -24,8 +25,17 @@ import qualified Data.ByteString.Builder as BB import qualified Data.Double.Conversion.ByteString as BC+import Data.Functor.Contravariant -- | Instruction about a parameter to monitor.+--+-- Convert a parameter monitor from one data type to another with '(>$<)'.+--+-- For example, monitor a 'Double' value being the first entry of a tuple:+--+-- @+-- mon = fst >$< monitorDouble+-- @ data MonitorParameter a = MonitorParameter { -- | Name of parameter. mpName :: String,@@ -33,15 +43,21 @@ mpFunc :: a -> BB.Builder } +instance Contravariant (MonitorParameter) where+ contramap f (MonitorParameter n m) = MonitorParameter n (m . f)+ -- | Convert a parameter monitor from one data type to another. --+-- DEPRECATED.+-- -- For example, to monitor a 'Double' value being the first entry of a tuple: -- -- @ -- mon = fst @. monitorDouble -- @ (@.) :: (b -> a) -> MonitorParameter a -> MonitorParameter b-(@.) f (MonitorParameter n m) = MonitorParameter n (m . f)+(@.) = contramap+{-# DEPRECATED (@.) "Superseded by the contravariant instance, use '(>$<)'." #-} -- | Monitor 'Int'. monitorInt ::
src/Mcmc/Monitor/ParameterBatch.hs view
@@ -16,6 +16,7 @@ module Mcmc.Monitor.ParameterBatch ( -- * Batch parameter monitors MonitorParameterBatch (..),+ (>$<), (@#), monitorBatchMean, monitorBatchMeanF,@@ -26,12 +27,21 @@ import qualified Data.ByteString.Builder as BB import qualified Data.Double.Conversion.ByteString as BC+import Data.Functor.Contravariant -- | Instruction about a parameter to monitor via batch means. Usually, the -- monitored parameter is average over the batch size. However, arbitrary -- functions performing more complicated analyses on the states in the batch can -- be provided. --+-- Convert a batch monitor from one data type to another with '(>$<)'.+--+-- For example, batch monitor the mean of the first entry of a tuple:+--+-- @+-- mon = fst >$< monitorBatchMean+-- @+-- -- XXX: Batch monitors are slow at the moment because the monitored parameter -- has to be extracted from the state for each iteration. data MonitorParameterBatch a = MonitorParameterBatch@@ -41,6 +51,9 @@ mbpFunc :: [a] -> BB.Builder } +instance Contravariant (MonitorParameterBatch) where+ contramap f (MonitorParameterBatch n m) = MonitorParameterBatch n (m . map f)+ -- | Convert a batch parameter monitor from one data type to another. -- -- For example, to batch monitor the mean of the first entry of a tuple:@@ -50,6 +63,7 @@ -- @ (@#) :: (b -> a) -> MonitorParameterBatch a -> MonitorParameterBatch b (@#) f (MonitorParameterBatch n m) = MonitorParameterBatch n (m . map f)+{-# DEPRECATED (@#) "Superseded by the contravariant instance, use '(>$<)'." #-} mean :: Real a => [a] -> Double mean xs = realToFrac (sum xs) / fromIntegral (length xs)
src/Mcmc/Prior.hs view
@@ -19,9 +19,7 @@ normal, exponential, gamma,-- -- * Discrete priors-+ -- -- * Discrete priors -- No discrete priors are available yet. -- * Auxiliary functions@@ -98,8 +96,8 @@ -- | Intelligent product that stops when encountering a zero. ----- Use with care because the elements have to be checked for positiveness, and--- this can take some time if the list is long and does not contain any zeroes.+-- Use with care because the elements are checked for positiveness, and this can+-- take some time if the list is long and does not contain any zeroes. product' :: [Log Double] -> Log Double product' = fromMaybe 0 . prodM
src/Mcmc/Proposal.hs view
@@ -19,7 +19,7 @@ ( -- * Proposal Proposal (..), (@~),- ProposalSimple (..),+ ProposalSimple, Tuner (tParam, tFunc), createProposal, tune,@@ -45,6 +45,7 @@ where import Data.Aeson+import Data.Bifunctor import qualified Data.ByteString.Builder as BB import qualified Data.ByteString.Lazy.Char8 as BL import Data.Default@@ -92,7 +93,7 @@ -- For example: -- -- @--- scaleFirstEntryOfTuple = scale >>> _1+-- scaleFirstEntryOfTuple = _1 @~ scale -- @ (@~) :: Lens' b a -> Proposal a -> Proposal b (@~) l (Proposal n w s t) = Proposal n w (convertS l s) (convertT l <$> t)@@ -104,13 +105,12 @@ -- -- In order to calculate the Metropolis-Hastings ratio, we need to know the -- ratio of the backward to forward kernels (i.e., the probability masses or--- probability densities). For unbiased proposals, this ratio is 1.0.-newtype ProposalSimple a = ProposalSimple- { pSample :: a -> GenIO -> IO (a, Log Double)- }+-- probability densities). For unbiased proposals, this ratio is 1.0. For biased+-- proposals, the ratio is ?.+type ProposalSimple a = a -> GenIO -> IO (a, Log Double) convertS :: Lens' b a -> ProposalSimple a -> ProposalSimple b-convertS l (ProposalSimple s) = ProposalSimple s'+convertS l s = s' where s' v g = do (x', r) <- s (v ^. l) g@@ -129,19 +129,19 @@ -- | Create a possibly tuneable proposal. createProposal ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Function creating a simple proposal for a given tuning parameter. The -- larger the tuning parameter, the larger the proposal (and the lower the -- expected acceptance ratio), and vice versa. (Double -> ProposalSimple a) ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Activate tuning? Bool -> Proposal a-createProposal n w f True = Proposal n w (f 1.0) (Just $ Tuner 1.0 f)-createProposal n w f False = Proposal n w (f 1.0) Nothing+createProposal f n w True = Proposal n w (f 1.0) (Just $ Tuner 1.0 f)+createProposal f n w False = Proposal n w (f 1.0) Nothing -- Minimal tuning parameter; subject to change. tuningParamMin :: Double@@ -345,8 +345,8 @@ -- | For key @k@, prepend an accepted (True) or rejected (False) proposal. pushA :: (Ord k, Show k) => k -> Bool -> Acceptance k -> Acceptance k-pushA k True = Acceptance . M.adjust (\(a, r) -> (succ a, r)) k . fromAcceptance-pushA k False = Acceptance . M.adjust (\(a, r) -> (a, succ r)) k . fromAcceptance+pushA k True = Acceptance . M.adjust (first succ) k . fromAcceptance+pushA k False = Acceptance . M.adjust (second succ) k . fromAcceptance {-# INLINEABLE pushA #-} -- | Reset acceptance storage.
src/Mcmc/Proposal/Bactrian.hs view
@@ -67,30 +67,32 @@ | m < 0 = error "bactrianAdditiveSimple: Spike parameter negative." | m >= 1 = error "bactrianAdditiveSimple: Spike parameter 1.0 or larger." | s <= 0 = error "bactrianAdditiveSimple: Standard deviation 0.0 or smaller."- | otherwise = ProposalSimple $ bactrianAdditive m (t * s)+ | otherwise = bactrianAdditive m (t * s) -- | Additive symmetric proposal with kernel similar to the silhouette of a--- Bactrian camel. The [Bactrian--- kernel](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/figure/fig01)--- is a mixture of two symmetrically arranged normal distributions. The spike+-- Bactrian camel.+--+-- The [Bactrian+-- kernel](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/figure/fig01) is+-- a mixture of two symmetrically arranged normal distributions. The spike -- parameter loosely determines the standard deviations of the individual humps--- while the other parameter refers to the standard deviation of the complete+-- while the second parameter refers to the standard deviation of the complete -- Bactrian kernel. -- -- See https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/. slideBactrian ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Spike parameter. Double -> -- | Standard deviation. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal Double-slideBactrian n w m s = createProposal n w (bactrianAdditiveSimple m s)+slideBactrian m s = createProposal (bactrianAdditiveSimple m s) -- We have: -- x (1+dx ) = x'@@ -118,20 +120,20 @@ | m < 0 = error "bactrianMultSimple: Spike parameter negative." | m >= 1 = error "bactrianMultSimple: Spike parameter 1.0 or larger." | s <= 0 = error "bactrianMultSimple: Standard deviation 0.0 or smaller."- | otherwise = ProposalSimple $ bactrianMult m (t * s)+ | otherwise = bactrianMult m (t * s) -- | Multiplicative proposal with kernel similar to the silhouette of a Bactrian -- camel. See 'slideBactrian'. scaleBactrian ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Spike parameter. Double -> -- | Standard deviation. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal Double-scaleBactrian n w m s = createProposal n w (bactrianMultSimple m s)+scaleBactrian m s = createProposal (bactrianMultSimple m s)
src/Mcmc/Proposal/Generic.hs view
@@ -50,7 +50,7 @@ -- required for biased proposals. Maybe (Double -> Double) -> ProposalSimple a-genericContinuous d f fInv = ProposalSimple $ sampleCont d f fInv+genericContinuous d f fInv = sampleCont d f fInv sampleDiscrete :: (DiscreteDistr d, DiscreteGen d) =>@@ -82,4 +82,4 @@ -- required for biased proposals. Maybe (Int -> Int) -> ProposalSimple a-genericDiscrete fd f fInv = ProposalSimple $ sampleDiscrete fd f fInv+genericDiscrete fd f fInv = sampleDiscrete fd f fInv
src/Mcmc/Proposal/Scale.hs view
@@ -29,37 +29,37 @@ -- | Multiplicative proposal with Gamma distributed kernel. scale ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Shape. Double -> -- | Scale. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal Double-scale n w k th = createProposal n w (scaleSimple k th)+scale k th = createProposal (scaleSimple k th) -- | Multiplicative proposal with Gamma distributed kernel. -- -- The scale of the Gamma distributions is set to (shape)^{-1}, so that the mean -- of the Gamma distribution is 1.0. scaleUnbiased ::+ -- | Shape.+ Double -> -- | Name. String -> -- | Weight. Int ->- -- | Shape.- Double -> -- | Enable tuning. Bool -> Proposal Double-scaleUnbiased n w k = createProposal n w (scaleSimple k (1 / k))+scaleUnbiased k = createProposal (scaleSimple k (1 / k)) contra :: (Double, Double) -> Double -> (Double, Double)-contra (x, y) z = (x*z, y/z)+contra (x, y) z = (x * z, y / z) scaleContrarilySimple :: Double -> Double -> Double -> ProposalSimple (Double, Double) scaleContrarilySimple k th t = genericContinuous (gammaDistr (k / t) (th * t)) contra (Just recip)@@ -69,15 +69,15 @@ -- The two values are scaled contrarily so that their product stays constant. -- Contrary proposals are useful when parameters are confounded. scaleContrarily ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Shape. Double -> -- | Scale. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal (Double, Double)-scaleContrarily n w k th = createProposal n w (scaleContrarilySimple k th)+scaleContrarily k th = createProposal (scaleContrarilySimple k th)
src/Mcmc/Proposal/Slide.hs view
@@ -30,18 +30,18 @@ -- | Additive proposal with normally distributed kernel. slide ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Mean. Double -> -- | Standard deviation. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal Double-slide n w m s = createProposal n w (slideSimple m s)+slide m s = createProposal (slideSimple m s) -- The actual proposal with tuning parameter. slideSymmetricSimple :: Double -> Double -> ProposalSimple Double@@ -51,16 +51,16 @@ -- proposal is very fast, because the Metropolis-Hastings ratio does not include -- calculation of the forwards and backwards kernels. slideSymmetric ::+ -- | Standard deviation.+ Double -> -- | Name. String -> -- | Weight. Int ->- -- | Standard deviation.- Double -> -- | Enable tuning. Bool -> Proposal Double-slideSymmetric n w s = createProposal n w (slideSymmetricSimple s)+slideSymmetric s = createProposal (slideSymmetricSimple s) -- The actual proposal with tuning parameter. slideUniformSimple :: Double -> Double -> ProposalSimple Double@@ -71,16 +71,16 @@ -- because the Metropolis-Hastings ratio does not include calculation of the -- forwards and backwards kernels. slideUniform ::+ -- | Delta.+ Double -> -- | Name. String -> -- | Weight. Int ->- -- | Delta.- Double -> -- | Enable tuning. Bool -> Proposal Double-slideUniform n w d = createProposal n w (slideUniformSimple d)+slideUniform d = createProposal (slideUniformSimple d) contra :: (Double, Double) -> Double -> (Double, Double) contra (x, y) d = (x + d, y - d)@@ -93,15 +93,15 @@ -- The two values are slid contrarily so that their sum stays constant. Contrary -- proposals are useful when parameters are confounded. slideContrarily ::- -- | Name.- String ->- -- | Weight.- Int -> -- | Mean. Double -> -- | Standard deviation. Double ->+ -- | Name.+ String ->+ -- | Weight.+ Int -> -- | Enable tuning. Bool -> Proposal (Double, Double)-slideContrarily n w m s = createProposal n w (slideContrarilySimple m s)+slideContrarily m s = createProposal (slideContrarilySimple m s)
src/Mcmc/Save.hs view
@@ -93,19 +93,8 @@ -- | Save a 'Status' to file. ----- Saved information:--- - state--- - iteration--- - trace--- - acceptance ratios--- - generator------ Important information that cannot be saved and has to be provided again when--- a chain is restored:--- - prior function--- - likelihood function--- - cycle--- - monitor+-- Some important values have to be provided upon restoring the status. See+-- 'loadStatus'. saveStatus :: ToJSON a => FilePath -> Status a -> IO () saveStatus fn s = BL.writeFile fn $ compress $ encode (toSave s) @@ -175,13 +164,9 @@ -- already a good indicator. when (p x /= svp)- ( error- "loadStatus: Provided prior function does not match the saved prior."- )+ (error "loadStatus: Provided prior function does not match the saved prior.") when (l x /= svl)- ( error- "loadStatus: Provided likelihood function does not match the saved likelihood."- )+ (error "loadStatus: Provided likelihood function does not match the saved likelihood.") removeFile fn return s
src/Mcmc/Status.hs view
@@ -1,18 +1,13 @@--- XXX: Add possibility to store supplementary information about the chain.------ Maybe something like Trace b; and give a function a -> b to extract--- supplementary info.+-- Note: It is not necessary to add another type @b@ to store supplementary+-- information about the chain. The information can just be stored in @a@+-- equally well. --- XXX: Status tuned exclusively to the Metropolis-Hastings algorithm. We--- should abstract the algorithm from the chain. For example,+-- XXX: Status tuned exclusively to the Metropolis-Hastings algorithm. We should+-- abstract the algorithm from the chain. Maybe something like: -- -- @--- data Status a b = Status { Chain a; Algorithm a b}+-- data Status a = Status { Chain a; Algorithm a} -- @------ where a described the state space and b the auxiliary information of the--- algorithm. This would also solve the above problem, for example in terms of--- the Hamiltonian algorithm -- | -- Module : Mcmc.Status@@ -49,6 +44,9 @@ -- | The 'Status' contains all information to run an MCMC chain. It is -- constructed using the function 'status'.+--+-- The polymorphic type @a@ stores the state of the chain. It can also be used+-- to store auxiliary information. data Status a = Status { -- MCMC related variables; saved. @@ -136,7 +134,7 @@ -- that auto tuning only happens during burn in. Int -> -- | A source of randomness. For reproducible runs, make- -- sure to use a generator with the same seed.+ -- sure to use generators with the same, fixed seed. GenIO -> Status a status n p l c m x mB mT nI g
test/Mcmc/ProposalSpec.hs view
@@ -20,10 +20,10 @@ import Test.Hspec p1 :: Proposal Double-p1 = slideSymmetric "test1" 1 1.0 True+p1 = slideSymmetric 1.0 "test1" 1 True p2 :: Proposal Double-p2 = slideSymmetric "test2" 3 1.0 True+p2 = slideSymmetric 1.0 "test2" 3 True c :: Cycle Double c = fromList [p1, p2]
test/Mcmc/SaveSpec.hs view
@@ -38,10 +38,10 @@ proposals :: Cycle Double proposals = fromList- [ slideSymmetric "small" 5 0.1 True,- slideSymmetric "medium" 2 1.0 True,- slideSymmetric "large" 2 5.0 True,- slide "skewed" 1 1.0 4.0 True+ [ slideSymmetric 0.1 "small" 5 True,+ slideSymmetric 1.0 "medium" 2 True,+ slideSymmetric 5.0 "large" 2 True,+ slide 1.0 4.0 "skewed" 1 True ] monStd :: MonitorStdOut Double