-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Sample from a posterior using Markov chain Monte Carlo -- -- Please see the README on GitHub at -- https://github.com/dschrempf/mcmc#readme @package mcmc @version 0.2.2 -- | Creation date: Mon Aug 3 10:46:27 2020. module Mcmc.Internal.ByteString -- | For a given width, align string to the right; use given fill -- character; trim on the left if string is longer. alignRightWith :: Char -> Int -> ByteString -> ByteString -- | For a given width, align string to the right; trim on the left if -- string is longer. alignRight :: Int -> ByteString -> ByteString -- | For a given width, align string to the left; use given fill character; -- trim on the right if string is longer. alignLeftWith :: Char -> Int -> ByteString -> ByteString -- | For a given width, align string to the left; trim on the right if -- string is longer. alignLeft :: Int -> ByteString -> ByteString -- | Creation date: Wed May 20 09:10:27 2020. module Mcmc.Item -- | An Item, or link of the Markov chain. For reasons of -- computational efficiency, each state is associated with the -- corresponding prior and likelihood. data Item a Item :: a -> Log Double -> Log Double -> Item a -- | The current state in the state space a. [state] :: Item a -> a -- | The current prior. [prior] :: Item a -> Log Double -- | The current likelihood. [likelihood] :: Item a -> Log Double instance GHC.Read.Read a => GHC.Read.Read (Mcmc.Item.Item a) instance GHC.Show.Show a => GHC.Show.Show (Mcmc.Item.Item a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Mcmc.Item.Item a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Mcmc.Item.Item a) instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Mcmc.Item.Item a) instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Mcmc.Item.Item a) instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Numeric.Log.Log a) instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Numeric.Log.Log a) -- | Creation date: Fri May 29 12:30:03 2020. module Mcmc.Monitor.Log -- | Print a log value. renderLog :: Log Double -> ByteString -- | Creation date: Fri May 29 11:11:49 2020. module Mcmc.Monitor.Parameter -- | Instruction about a parameter to monitor. data MonitorParameter a MonitorParameter :: String -> (a -> Builder) -> MonitorParameter a -- | Name of parameter. [mpName] :: MonitorParameter a -> String -- | Instruction about how to extract the parameter from the state. [mpFunc] :: MonitorParameter a -> a -> Builder -- | Convert a parameter monitor from one data type to another using a -- lens. -- -- For example, to monitor a Double value being the first entry of -- a tuple: -- --
--   mon = _1 @@ monitorDouble
--   
(@.) :: Lens' b a -> MonitorParameter a -> MonitorParameter b -- | Monitor Int. monitorInt :: String -> MonitorParameter Int -- | Monitor Double with eight decimal places (half precision). monitorDouble :: String -> MonitorParameter Double -- | Monitor Double with full precision computing the shortest -- string of digits that correctly represent the number. monitorDoubleF :: String -> MonitorParameter Double -- | Monitor Double in exponential format and half precision. monitorDoubleE :: String -> MonitorParameter Double -- | Creation date: Fri May 29 11:11:49 2020. -- -- Batch mean monitors. module Mcmc.Monitor.ParameterBatch -- | 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. -- -- 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 :: String -> ([a] -> Builder) -> MonitorParameterBatch a -- | Name of batch monitored parameter. [mbpName] :: MonitorParameterBatch a -> String -- | Instruction about how to extract the batch mean from the trace. [mbpFunc] :: MonitorParameterBatch a -> [a] -> Builder -- | Convert a batch parameter monitor from one data type to another using -- a lens. -- -- For example, to batch monitor a real float value being the first entry -- of a tuple: -- --
--   mon = _1 @# monitorBatchMeanRealFloat
--   
(@#) :: Lens' b a -> MonitorParameterBatch a -> MonitorParameterBatch b -- | Batch monitor. Print the mean with eight decimal places (half -- precision). monitorBatchMean :: Real a => String -> MonitorParameterBatch a -- | Batch monitor. Print the mean with full precision computing the -- shortest string of digits that correctly represent the number. monitorBatchMeanF :: Real a => String -> MonitorParameterBatch a -- | Batch monitor real float parameters such as Double with -- scientific notation and eight decimal places. monitorBatchMeanE :: Real a => String -> MonitorParameterBatch a -- | Batch monitor parameters with custom lens and builder. monitorBatchCustom :: String -> ([a] -> a) -> (a -> Builder) -> MonitorParameterBatch a -- | Creation date: Fri May 29 12:36:43 2020. module Mcmc.Monitor.Time -- | Adapted from System.ProgressBar.renderDuration of package -- terminal-progressbar-0.4.1. renderDuration :: NominalDiffTime -> ByteString -- | Render duration in seconds. renderDurationS :: NominalDiffTime -> ByteString -- | Creation date: Wed May 20 14:37:09 2020. -- -- From https://wiki.haskell.org/Random_shuffle. module Mcmc.Tools.Shuffle -- | Shuffle a list. shuffle :: [a] -> GenIO -> IO [a] -- | Shuffle a list n times. shuffleN :: [a] -> Int -> GenIO -> IO [[a]] -- | grabble xs m n is O(m*n'), where n' = min n -- (length xs). Choose n' elements from xs, -- without replacement, and that m times. grabble :: [a] -> Int -> Int -> GenIO -> IO [[a]] -- | Creation date: Wed May 20 13:42:53 2020. module Mcmc.Proposal -- | A Proposal is an instruction about how the Markov chain will -- traverse the state space a. Essentially, it is a probability -- mass or probability density conditioned on the current state (i.e., a -- kernel). -- -- A Proposal may be tuneable in that it contains information -- about how to enlarge or shrink the step size to tune the acceptance -- ratio. data Proposal a Proposal :: String -> Int -> ProposalSimple a -> Maybe (Tuner a) -> Proposal a -- | Name (no proposals with the same name are allowed in a Cycle). [pName] :: Proposal a -> String -- | The weight determines how often a Proposal is executed per -- iteration of the Markov chain. [pWeight] :: Proposal a -> Int -- | Simple proposal without tuning information. [pSimple] :: Proposal a -> ProposalSimple a -- | Tuning is disabled if set to Nothing. [pTuner] :: Proposal a -> Maybe (Tuner a) -- | Convert a proposal from one data type to another using a lens. -- -- For example: -- --
--   scaleFirstEntryOfTuple = scale >>> _1
--   
(@~) :: Lens' b a -> Proposal a -> Proposal b -- | Simple proposal without tuning information. -- -- Instruction about randomly moving from the current state to a new -- state, given some source of randomness. -- -- 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 :: (a -> GenIO -> IO (a, Log Double)) -> ProposalSimple a [pSample] :: ProposalSimple a -> a -> GenIO -> IO (a, Log Double) -- | Tune the acceptance ratio of a Proposal; see tune, or -- autotuneCycle. data Tuner a -- | Create a possibly tuneable proposal. createProposal :: String -> Int -> (Double -> ProposalSimple a) -> Bool -> Proposal a -- | Tune a Proposal. Return Nothing if Proposal is -- not tuneable. If the parameter dt is larger than 1.0, the -- Proposal is enlarged, if 0<dt<1.0, it is shrunk. -- Negative tuning parameters are not allowed. tune :: Double -> Proposal a -> Maybe (Proposal a) -- | Define the order in which Proposals are executed in a -- Cycle. The total number of Proposals per Cycle -- may differ between Orders (e.g., compare RandomO and -- RandomReversibleO). data Order -- | Shuffle the Proposals in the Cycle. The Proposals -- are replicated according to their weights and executed in random -- order. If a Proposal has weight w, it is executed -- exactly w times per iteration. RandomO :: Order -- | The Proposals are executed sequentially, in the order they -- appear in the Cycle. Proposals with weight -- w>1 are repeated immediately w times (and not -- appended to the end of the list). SequentialO :: Order -- | Similar to RandomO. However, a reversed copy of the list of -- shuffled Proposals is appended such that the resulting Markov -- chain is reversible. Note: the total number of Proposals -- executed per cycle is twice the number of RandomO. RandomReversibleO :: Order -- | Similar to SequentialO. However, a reversed copy of the list of -- sequentially ordered Proposals is appended such that the -- resulting Markov chain is reversible. SequentialReversibleO :: Order -- | In brief, a Cycle is a list of proposals. The state of the -- Markov chain will be logged only after all Proposals in the -- Cycle have been completed, and the iteration counter will be -- increased by one. The order in which the Proposals are executed -- is specified by Order. The default is RandomO. -- -- Proposals must have unique names, so that they can be -- identified. data Cycle a -- | Create a Cycle from a list of Proposals. fromList :: [Proposal a] -> Cycle a -- | Set the order of Proposals in a Cycle. setOrder :: Order -> Cycle a -> Cycle a -- | Replicate Proposals according to their weights and possibly -- shuffle them. getNCycles :: Cycle a -> Int -> GenIO -> IO [[Proposal a]] -- | Tune Proposals in the Cycle. See tune. tuneCycle :: Map (Proposal a) Double -> Cycle a -> Cycle a -- | Calculate acceptance ratios and auto tune the Proposals in the -- Cycle. For now, a Proposal is enlarged when the -- acceptance ratio is above 0.44, and shrunk otherwise. Do not change -- Proposals that are not tuneable. autotuneCycle :: Acceptance (Proposal a) -> Cycle a -> Cycle a -- | Summarize the Proposals in the Cycle. Also report -- acceptance ratios. summarizeCycle :: Acceptance (Proposal a) -> Cycle a -> ByteString -- | For each key k, store the number of accepted and rejected -- proposals. data Acceptance k -- | In the beginning there was the Word. -- -- Initialize an empty storage of accepted/rejected values. emptyA :: Ord k => [k] -> Acceptance k -- | For key k, prepend an accepted (True) or rejected (False) -- proposal. pushA :: (Ord k, Show k) => k -> Bool -> Acceptance k -> Acceptance k -- | Reset acceptance storage. resetA :: Ord k => Acceptance k -> Acceptance k -- | Transform keys using the given lists. Keys not provided will not be -- present in the new Acceptance variable. transformKeysA :: (Ord k1, Ord k2) => [k1] -> [k2] -> Acceptance k1 -> Acceptance k2 -- | Acceptance ratios for all proposals. acceptanceRatios :: Acceptance k -> Map k Double instance GHC.Show.Show Mcmc.Proposal.Order instance GHC.Classes.Eq Mcmc.Proposal.Order instance Data.Aeson.Types.ToJSON.ToJSONKey k => Data.Aeson.Types.ToJSON.ToJSON (Mcmc.Proposal.Acceptance k) instance (GHC.Classes.Ord k, Data.Aeson.Types.FromJSON.FromJSONKey k) => Data.Aeson.Types.FromJSON.FromJSON (Mcmc.Proposal.Acceptance k) instance Data.Default.Class.Default Mcmc.Proposal.Order instance GHC.Show.Show (Mcmc.Proposal.Proposal a) instance GHC.Classes.Eq (Mcmc.Proposal.Proposal a) instance GHC.Classes.Ord (Mcmc.Proposal.Proposal a) -- | Creation date: Thu May 14 20:26:27 2020. module Mcmc.Proposal.Generic -- | Generic function to create proposals for continuous parameters -- (Double). proposalGenericContinuous :: (ContDistr d, ContGen d) => d -> (a -> Double -> a) -> Maybe (Double -> Double) -> ProposalSimple a -- | Generic function to create proposals for discrete parameters -- (Int). proposalGenericDiscrete :: (DiscreteDistr d, DiscreteGen d) => d -> (a -> Int -> a) -> Maybe (Int -> Int) -> ProposalSimple a -- | Creation date: Wed May 6 10:59:13 2020. module Mcmc.Proposal.Slide -- | Additive proposal with normally distributed kernel. slide :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | Additive proposal with normally distributed kernel with mean zero. -- This proposal is very fast, because the Metropolis-Hastings ratio does -- not include calculation of the forwards and backwards kernels. slideSymmetric :: String -> Int -> Double -> Bool -> Proposal Double -- | Additive proposal with uniformly distributed kernel. This proposal is -- very fast, because the Metropolis-Hastings ratio does not include -- calculation of the forwards and backwards kernels. slideUniform :: String -> Int -> Double -> Bool -> Proposal Double -- | Creation date: Thu May 14 21:49:23 2020. module Mcmc.Proposal.Scale -- | Multiplicative proposal with Gamma distributed kernel. scale :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | 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 :: String -> Int -> Double -> Bool -> Proposal Double -- | Creation date: Thu Jun 25 15:49:48 2020. -- -- See https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/. module Mcmc.Proposal.Bactrian -- | Additive symmetric proposal with kernel similar to the silhouette of a -- Bactrian camel. The Bactrian kernel 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 Bactrian kernel. -- -- See https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/. slideBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | Multiplicative proposal with kernel similar to the silhouette of a -- Bactrian camel. See slideBactrian. scaleBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | Creation date: Wed May 20 09:11:25 2020. module Mcmc.Trace -- | A Trace passes through a list of states with associated -- likelihoods which are called Items. New Items are -- prepended, and the path of the Markov chain is stored in reversed -- order. data Trace a -- | The empty trace. singletonT :: Item a -> Trace a -- | Prepend an Item to a Trace. pushT :: Item a -> Trace a -> Trace a -- | Get the most recent item of the trace. headT :: Trace a -> Item a -- | Get the N most recent items of the trace. takeItems :: Int -> Trace a -> [Item a] -- | Shorten the trace to given length. takeT :: Int -> Trace a -> Trace a instance GHC.Classes.Eq a => GHC.Classes.Eq (Mcmc.Trace.Trace a) instance GHC.Read.Read a => GHC.Read.Read (Mcmc.Trace.Trace a) instance GHC.Show.Show a => GHC.Show.Show (Mcmc.Trace.Trace a) instance GHC.Base.Semigroup (Mcmc.Trace.Trace a) instance GHC.Base.Monoid (Mcmc.Trace.Trace a) instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Mcmc.Trace.Trace a) instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Mcmc.Trace.Trace a) -- | Creation date: Sat Jun 27 10:49:28 2020. module Mcmc.Verbosity -- | Not much to say here. data Verbosity Quiet :: Verbosity Warn :: Verbosity Info :: Verbosity Debug :: Verbosity -- | Perform action if Verbosity is Warn or higher. warn :: Applicative m => Verbosity -> m () -> m () -- | Perform action if Verbosity is Info or higher. info :: Applicative m => Verbosity -> m () -> m () -- | Perform action if Verbosity is Debug. debug :: Applicative m => Verbosity -> m () -> m () instance Data.Aeson.Types.ToJSON.ToJSON Mcmc.Verbosity.Verbosity instance Data.Aeson.Types.FromJSON.FromJSON Mcmc.Verbosity.Verbosity instance GHC.Classes.Ord Mcmc.Verbosity.Verbosity instance GHC.Classes.Eq Mcmc.Verbosity.Verbosity instance GHC.Show.Show Mcmc.Verbosity.Verbosity -- | Creation date: Thu May 21 14:35:11 2020. module Mcmc.Monitor -- | A Monitor describes which part of the Markov chain should be -- logged and where. Further, they allow output of summary statistics per -- iteration in a flexible way. data Monitor a Monitor :: MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a -- | Monitor writing to standard output. [mStdOut] :: Monitor a -> MonitorStdOut a -- | Monitors writing to files. [mFiles] :: Monitor a -> [MonitorFile a] -- | Monitors calculating batch means and writing to files. [mBatches] :: Monitor a -> [MonitorBatch a] -- | Monitor to standard output; constructed with monitorStdOut. data MonitorStdOut a -- | Monitor to standard output. monitorStdOut :: [MonitorParameter a] -> Int -> MonitorStdOut a -- | Monitor to a file; constructed with monitorFile. data MonitorFile a -- | Monitor parameters to a file. monitorFile :: String -> [MonitorParameter a] -> Int -> MonitorFile a -- | Monitor to a file, but calculate batch means for the given batch size; -- constructed with monitorBatch. -- -- Batch monitors are slow at the moment because the monitored parameter -- has to be extracted from the state for each iteration. data MonitorBatch a -- | Monitor parameters to a file, see MonitorBatch. monitorBatch :: String -> [MonitorParameterBatch a] -> Int -> MonitorBatch a -- | Open the files associated with the Monitor. mOpen :: String -> Bool -> Monitor a -> IO (Monitor a) -- | Open the files associated with the Monitor in append mode. mAppend :: String -> Monitor a -> IO (Monitor a) -- | Get header line of MonitorStdOut. mHeader :: Monitor a -> ByteString -- | Execute monitors; print status information to files and return text to -- be printed to standard output and log file. mExec :: Verbosity -> Int -> Int -> UTCTime -> Trace a -> Int -> Monitor a -> IO (Maybe ByteString) -- | Close the files associated with the Monitor. mClose :: Monitor a -> IO (Monitor a) -- | Creation date: Tue May 5 18:01:15 2020. module Mcmc.Status -- | The Status contains all information to run an MCMC chain. It is -- constructed using the function status. data Status a Status :: String -> Item a -> Int -> Trace a -> Acceptance (Proposal a) -> Maybe Int -> Maybe Int -> Int -> Bool -> Maybe Int -> Verbosity -> GenIO -> Maybe (Int, UTCTime) -> Maybe Handle -> (a -> Log Double) -> (a -> Log Double) -> Cycle a -> Monitor a -> Status a -- | The name of the MCMC chain; used as file prefix. [name] :: Status a -> String -- | The current Item of the chain combines the current state and -- the current likelihood. [item] :: Status a -> Item a -- | The iteration is the number of completed cycles. [iteration] :: Status a -> Int -- | The Trace of the Markov chain in reverse order, the most recent -- Item is at the head of the list. [trace] :: Status a -> Trace a -- | For each Proposal, store the list of accepted (True) and -- rejected (False) proposals; for reasons of efficiency, the list is -- also stored in reverse order. [acceptance] :: Status a -> Acceptance (Proposal a) -- | Number of burn in iterations; deactivate burn in with Nothing. [burnInIterations] :: Status a -> Maybe Int -- | Auto tuning period (only during burn in); deactivate auto tuning with -- Nothing. [autoTuningPeriod] :: Status a -> Maybe Int -- | Number of normal iterations excluding burn in. Note that auto tuning -- only happens during burn in. [iterations] :: Status a -> Int -- | Overwrite output files? Default is False, change with -- force. [forceOverwrite] :: Status a -> Bool -- | Save the chain with trace of given length at the end of the run? -- Default is no save (Nothing). Change with saveWith. [save] :: Status a -> Maybe Int -- | Verbosity. [verbosity] :: Status a -> Verbosity -- | The random number generator. [generator] :: Status a -> GenIO -- | Starting time and starting iteration of chain; used to calculate run -- time and ETA. [start] :: Status a -> Maybe (Int, UTCTime) -- | Handle to log file. [logHandle] :: Status a -> Maybe Handle -- | The prior function. The un-normalized posterior is the product of the -- prior and the likelihood. [priorF] :: Status a -> a -> Log Double -- | The likelihood function. The un-normalized posterior is the product of -- the prior and the likelihood. [likelihoodF] :: Status a -> a -> Log Double -- | A set of Proposals form a Cycle. [cycle] :: Status a -> Cycle a -- | A Monitor observing the chain. [monitor] :: Status a -> Monitor a -- | Initialize the Status of a Markov chain Monte Carlo run. status :: String -> (a -> Log Double) -> (a -> Log Double) -> Cycle a -> Monitor a -> a -> Maybe Int -> Maybe Int -> Int -> GenIO -> Status a -- | Save the Markov chain with trace of given length. saveWith :: Int -> Status a -> Status a -- | Overwrite existing files; it is not necessary to use force, -- when a chain is continued. force :: Status a -> Status a -- | Do not print anything to standard output. Do not create log file. File -- monitors and batch monitors are executed normally. quiet :: Status a -> Status a -- | Be verbose. debug :: Status a -> Status a -- | Creation date: Tue Jun 16 10:18:54 2020. -- -- Save and load an MCMC run. It is easy to save and restore the current -- state and likelihood (or the trace), but it is not feasible to store -- all the proposals and so on, so they have to be provided again when -- continuing a run. module Mcmc.Save -- | 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 saveStatus :: ToJSON a => FilePath -> Status a -> IO () -- | Load a Status from file. -- -- Important information that cannot be saved and has to be provided -- again when a chain is restored: - prior function - likelihood function -- - cycle - monitor -- -- To avoid incomplete continued runs, the mcmc file is removed -- after load. loadStatus :: FromJSON a => (a -> Log Double) -> (a -> Log Double) -> Cycle a -> Monitor a -> FilePath -> IO (Status a) instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Mcmc.Save.Save a) instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Mcmc.Save.Save a) -- | Creation date: Fri May 29 10:19:45 2020. module Mcmc.Mcmc -- | An Mcmc state transformer; usually fiddling around with this type is -- not required, but it is used by the different inference algorithms. type Mcmc a = StateT (Status a) IO -- | Write to standard output and log file. mcmcOutT :: ByteString -> Mcmc a () -- | Write to standard output and log file. mcmcOutS :: String -> Mcmc a () -- | Print warning message. mcmcWarnT :: ByteString -> Mcmc a () -- | Print warning message. mcmcWarnS :: String -> Mcmc a () -- | Print info message. mcmcInfoT :: ByteString -> Mcmc a () -- | Print info message. mcmcInfoS :: String -> Mcmc a () -- | Print debug message. mcmcDebugT :: ByteString -> Mcmc a () -- | Print debug message. mcmcDebugS :: String -> Mcmc a () -- | Auto tune the Proposals in the Cycle of the chain. Reset -- acceptance counts. See autotuneCycle. mcmcAutotune :: Mcmc a () -- | Reset acceptance counts. mcmcResetA :: Mcmc a () -- | Print short summary of Proposals in Cycle. See -- summarizeCycle. mcmcSummarizeCycle :: Mcmc a ByteString -- | Report what is going to be done. mcmcReport :: ToJSON a => Mcmc a () -- | Print header line of standard output monitor. mcmcMonitorStdOutHeader :: Mcmc a () -- | Execute the Monitors of the chain. See mExec. mcmcMonitorExec :: ToJSON a => Mcmc a () -- | Run an MCMC algorithm. mcmcRun :: ToJSON a => Mcmc a () -> Status a -> IO (Status a) -- | Creation date: Tue May 5 20:11:30 2020. module Mcmc.Metropolis -- | Run a Markov chain for a given number of Metropolis-Hastings steps. mh :: ToJSON a => Status a -> IO (Status a) -- | Continue a Markov chain for a given number of Metropolis-Hastings -- steps. mhContinue :: ToJSON a => Int -> Status a -> IO (Status a) -- | Creation date: Tue May 5 18:01:15 2020. -- -- A short introduction to update mechanisms using the -- Metropolis-Hastings algorithm (see Geyer, C. J., 2011; Introduction to -- Markov Chain Monte Carlo. In Handbook of Markov Chain Monte Carlo (pp. -- 45), Chapman & Hall/CRC). -- -- For examples, please see mcmc-examples. -- -- The import of this module alone should cover most use cases. module Mcmc -- | A Proposal is an instruction about how the Markov chain will -- traverse the state space a. Essentially, it is a probability -- mass or probability density conditioned on the current state (i.e., a -- kernel). -- -- A Proposal may be tuneable in that it contains information -- about how to enlarge or shrink the step size to tune the acceptance -- ratio. data Proposal a -- | Convert a proposal from one data type to another using a lens. -- -- For example: -- --
--   scaleFirstEntryOfTuple = scale >>> _1
--   
(@~) :: Lens' b a -> Proposal a -> Proposal b -- | Multiplicative proposal with Gamma distributed kernel. scale :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | 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 :: String -> Int -> Double -> Bool -> Proposal Double -- | Additive proposal with normally distributed kernel. slide :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | Additive symmetric proposal with kernel similar to the silhouette of a -- Bactrian camel. The Bactrian kernel 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 Bactrian kernel. -- -- See https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3845170/. slideBactrian :: String -> Int -> Double -> Double -> Bool -> Proposal Double -- | Additive proposal with normally distributed kernel with mean zero. -- This proposal is very fast, because the Metropolis-Hastings ratio does -- not include calculation of the forwards and backwards kernels. slideSymmetric :: String -> Int -> Double -> Bool -> Proposal Double -- | Additive proposal with uniformly distributed kernel. This proposal is -- very fast, because the Metropolis-Hastings ratio does not include -- calculation of the forwards and backwards kernels. slideUniform :: String -> Int -> Double -> Bool -> Proposal Double -- | In brief, a Cycle is a list of proposals. The state of the -- Markov chain will be logged only after all Proposals in the -- Cycle have been completed, and the iteration counter will be -- increased by one. The order in which the Proposals are executed -- is specified by Order. The default is RandomO. -- -- Proposals must have unique names, so that they can be -- identified. data Cycle a -- | Create a Cycle from a list of Proposals. fromList :: [Proposal a] -> Cycle a -- | Define the order in which Proposals are executed in a -- Cycle. The total number of Proposals per Cycle -- may differ between Orders (e.g., compare RandomO and -- RandomReversibleO). data Order -- | Shuffle the Proposals in the Cycle. The Proposals -- are replicated according to their weights and executed in random -- order. If a Proposal has weight w, it is executed -- exactly w times per iteration. RandomO :: Order -- | The Proposals are executed sequentially, in the order they -- appear in the Cycle. Proposals with weight -- w>1 are repeated immediately w times (and not -- appended to the end of the list). SequentialO :: Order -- | Similar to RandomO. However, a reversed copy of the list of -- shuffled Proposals is appended such that the resulting Markov -- chain is reversible. Note: the total number of Proposals -- executed per cycle is twice the number of RandomO. RandomReversibleO :: Order -- | Similar to SequentialO. However, a reversed copy of the list of -- sequentially ordered Proposals is appended such that the -- resulting Markov chain is reversible. SequentialReversibleO :: Order -- | Set the order of Proposals in a Cycle. setOrder :: Order -> Cycle a -> Cycle a -- | Initialize the Status of a Markov chain Monte Carlo run. status :: String -> (a -> Log Double) -> (a -> Log Double) -> Cycle a -> Monitor a -> a -> Maybe Int -> Maybe Int -> Int -> GenIO -> Status a -- | Save the Markov chain with trace of given length. saveWith :: Int -> Status a -> Status a -- | Overwrite existing files; it is not necessary to use force, -- when a chain is continued. force :: Status a -> Status a -- | Do not print anything to standard output. Do not create log file. File -- monitors and batch monitors are executed normally. quiet :: Status a -> Status a -- | Be verbose. debug :: Status a -> Status a -- | A Monitor describes which part of the Markov chain should be -- logged and where. Further, they allow output of summary statistics per -- iteration in a flexible way. data Monitor a Monitor :: MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a -- | Monitor to standard output; constructed with monitorStdOut. data MonitorStdOut a -- | Monitor to standard output. monitorStdOut :: [MonitorParameter a] -> Int -> MonitorStdOut a -- | Monitor to a file; constructed with monitorFile. data MonitorFile a -- | Monitor parameters to a file. monitorFile :: String -> [MonitorParameter a] -> Int -> MonitorFile a -- | Monitor to a file, but calculate batch means for the given batch size; -- constructed with monitorBatch. -- -- Batch monitors are slow at the moment because the monitored parameter -- has to be extracted from the state for each iteration. data MonitorBatch a -- | Monitor parameters to a file, see MonitorBatch. monitorBatch :: String -> [MonitorParameterBatch a] -> Int -> MonitorBatch a -- | Run a Markov chain for a given number of Metropolis-Hastings steps. mh :: ToJSON a => Status a -> IO (Status a) -- | Continue a Markov chain for a given number of Metropolis-Hastings -- steps. mhContinue :: ToJSON a => Int -> Status a -> IO (Status a) -- | Load a Status from file. -- -- Important information that cannot be saved and has to be provided -- again when a chain is restored: - prior function - likelihood function -- - cycle - monitor -- -- To avoid incomplete continued runs, the mcmc file is removed -- after load. loadStatus :: FromJSON a => (a -> Log Double) -> (a -> Log Double) -> Cycle a -> Monitor a -> FilePath -> IO (Status a)