Copyright | (c) Dominik Schrempf 2020 |
---|---|
License | GPL-3.0-or-later |
Maintainer | dominik.schrempf@gmail.com |
Stability | unstable |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Creation date: Tue May 5 18:01:15 2020.
For an introduction to Markov chain Monte Carlo (MCMC) samplers and update mechanisms using the Metropolis-Hastings-Green algorithm, please see Geyer, C. J., (2011), Introduction to Markov Chain Monte Carlo, In Handbook of Markov Chain Monte Carlo (pp. 45), CRC press.
This library focusses on classical Markov chain Monte Carlo algorithms such as the Metropolis-Hastings-Green [1] algorithm, or population methods involving parallel chains such as the Metropolic-coupled Markov chain Monte Carlo [2] algorithm. In particular, sequential Monte Carlo [3] algorithms following a moving posterior distribution are not provided.
An MCMC sampler can be run with mcmc
, for example using the
Metropolis-Hastings-Green algorithm mhg
.
Usually, it is best to start with an example:
- Basic inference of the accuracy of an archer
- More involved examples
The import of this module alone should cover most use cases.
[1]
Geyer, C. J. (2011), Introduction to markov chain monte carlo, In
Handbook of Markov Chain Monte Carlo (pp. 45), CRC press.
[2]
Geyer, C. J. (1991), Markov chain monte carlo maximum likelihood,
Computing Science and Statistics, Proceedings of the 23rd Symposium on the
Interface.
[3]
Sequential monte carlo methods in practice (2001), Editors: Arnaud
Doucet, Nando de Freitas, and Neil Gordon, Springer New York.
Synopsis
- newtype PName = PName {}
- newtype PWeight = PWeight {
- fromPWeight :: Int
- data Proposal a
- (@~) :: Lens' b a -> Proposal a -> Proposal b
- data Tune
- scale :: Double -> Double -> PName -> PWeight -> Tune -> Proposal Double
- scaleUnbiased :: Double -> PName -> PWeight -> Tune -> Proposal Double
- scaleContrarily :: Double -> Double -> PName -> PWeight -> Tune -> Proposal (Double, Double)
- scaleBactrian :: Double -> Double -> PName -> PWeight -> Tune -> Proposal Double
- slide :: Double -> Double -> PName -> PWeight -> Tune -> Proposal Double
- slideSymmetric :: Double -> PName -> PWeight -> Tune -> Proposal Double
- slideUniformSymmetric :: Double -> PName -> PWeight -> Tune -> Proposal Double
- slideContrarily :: Double -> Double -> PName -> PWeight -> Tune -> Proposal (Double, Double)
- slideBactrian :: Double -> Double -> PName -> PWeight -> Tune -> Proposal Double
- module Mcmc.Proposal.Simplex
- data Cycle a
- cycleFromList :: [Proposal a] -> Cycle a
- data Order
- setOrder :: Order -> Cycle a -> Cycle a
- module Mcmc.Settings
- data Monitor a = Monitor (MonitorStdOut a) [MonitorFile a] [MonitorBatch a]
- data MonitorStdOut a
- monitorStdOut :: [MonitorParameter a] -> Int -> MonitorStdOut a
- data MonitorFile a
- monitorFile :: String -> [MonitorParameter a] -> Int -> MonitorFile a
- data MonitorBatch a
- monitorBatch :: String -> [MonitorParameterBatch a] -> Int -> MonitorBatch a
- module Mcmc.Monitor.Parameter
- module Mcmc.Monitor.ParameterBatch
- module Mcmc.Prior
- mcmc :: Algorithm a => Settings -> a -> IO a
- mcmcContinue :: Algorithm a => Int -> Settings -> a -> IO a
- module Mcmc.Algorithm.Metropolis
- module Mcmc.Algorithm.MC3
- type PriorFunction a = a -> Log Double
- noPrior :: PriorFunction a
- type LikelihoodFunction a = a -> Log Double
- noLikelihood :: LikelihoodFunction a
Proposals
A Proposal
is an instruction about how to advance a given Markov
chain so that it possibly reaches a new state. That is, Proposal
s
specify how the chain traverses the state space. As far as this MCMC
library is concerned, Proposal
s are considered to be /elementary
updates/ in that they cannot be decomposed into smaller updates.
Proposal
s can be combined to form composite updates, a technique often
referred to as composition. On the other hand, mixing (used in the
sense of mixture models) is the random choice of a Proposal
(or a
composition of Proposal
s) from a given set.
The composition and mixture of Proposal
s allows specification
of nearly all MCMC algorithms involving a single chain (i.e., population
methods such as particle filters are excluded). In particular, Gibbs
samplers of all sorts can be specified using this procedure. For
reference, please see the short encyclopedia of MCMC
methods.
This library enables composition and mixture of Proposal
s via the
Cycle
data type. Essentially, a Cycle
is a set of Proposal
s. The
chain advances after the completion of each Cycle
, which is called an
iteration, and the iteration counter is increased by one.
The Proposal
s in a Cycle
can be executed in the given order or in a
random sequence which allows, for example, specification of a fixed scan
Gibbs sampler, or a random sequence scan Gibbs sampler, respectively. See
Order
.
Note that it is of utter importance that the given Cycle
enables
traversal of the complete state space. Otherwise, the Markov chain will
not converge to the correct stationary posterior distribution.
Proposals are named according to what they do, i.e., how they change the
state of a Markov chain, and not according to the intrinsically used
probability distributions. For example, slideSymmetric
is a sliding
proposal. Under the hood, it uses the normal distribution with mean zero
and given variance. The sampled variate is added to the current value of
the variable (hence, the name slide). The same nomenclature is used by
RevBayes [4]. The probability distributions and intrinsic properties of a
specific proposal are specified in detail in the documentation.
The other method, which is used intrinsically, is more systematic, but
also a little bit more complicated: we separate between the proposal
distribution and how the state is affected. And here, I am referring to
the operator (addition, multiplication, any other binary operator). For
example, the sliding proposal with mean m
, standard deviation s
, and
tuning parameter t
is implemented as
slideSimple :: Double -> Double -> Double -> ProposalSimple Double slideSimple m s t = genericContinuous (normalDistr m (s * t)) (+) (Just negate) Nothing
This specification is more involved. Especially since we need to know the
probability of jumping back, and so we need to know the inverse operator
negate
. However, it also allows specification of new proposals with
great ease.
[4]
Höhna, S., Landis, M. J., Heath, T. A., Boussau, B., Lartillot, N.,
Moore, B. R., Huelsenbeck, J. P., …, Revbayes: bayesian phylogenetic
inference using graphical models and an interactive model-specification
language, Systematic Biology, 65(4), 726–736 (2016).
http://dx.doi.org/10.1093/sysbio/syw021
Proposal name.
The weight determines how often a Proposal
is executed per iteration of
the Markov chain.
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 Markov kernel).
A Proposal
may be tuneable in that it contains information about how to enlarge
or shrink the step size to tune the acceptance rate.
Instances
Eq (Proposal a) Source # | |
Ord (Proposal a) Source # | |
(@~) :: Lens' b a -> Proposal a -> Proposal b Source #
Convert a proposal from one data type to another using a lens.
For example:
scaleFirstEntryOfTuple = _1 @~ scale
Tune the proposal?
Multiplicative proposal with gamma distributed kernel.
Multiplicative proposal with gamma distributed kernel.
The scale of the gamma distribution is set to (shape)^{-1}, so that the mean of the gamma distribution is 1.0.
Multiplicative proposal with gamma distributed kernel.
The two values are scaled contrarily so that their product stays constant. Contrary proposals are useful when parameters are confounded.
:: Double | Spike parameter. |
-> Double | Standard deviation. |
-> PName | Name. |
-> PWeight | Weight. |
-> Tune | Enable tuning. |
-> Proposal Double |
Multiplicative proposal with kernel similar to the silhouette of a Bactrian
camel. See slideBactrian
.
:: Double | Mean. |
-> Double | Standard deviation. |
-> PName | Name. |
-> PWeight | Weight. |
-> Tune | Enable tuning. |
-> Proposal Double |
Additive proposal with normally distributed kernel.
Additive proposal with normally distributed kernel with mean zero. This proposal is very fast, because the Metropolis-Hastings-Green ratio does not include calculation of the forwards and backwards kernels.
slideUniformSymmetric Source #
Additive proposal with uniformly distributed kernel with mean zero. This proposal is very fast, because the Metropolis-Hastings-Green ratio does not include calculation of the forwards and backwards kernels.
:: Double | Mean. |
-> Double | Standard deviation. |
-> PName | Name. |
-> PWeight | Weight. |
-> Tune | Enable tuning. |
-> Proposal (Double, Double) |
Additive proposal with normally distributed kernel.
The two values are slid contrarily so that their sum stays constant. Contrary proposals are useful when parameters are confounded.
:: Double | Spike parameter \(m\). |
-> Double | Standard deviation \(s\). |
-> PName | Name. |
-> PWeight | Weight. |
-> Tune | Enable tuning. |
-> 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 \(m \in (0, 1)\) loosely determines the standard deviations of the individual humps while the second parameter \(s > 0\) refers to the standard deviation of the complete Bactrian kernel.
module Mcmc.Proposal.Simplex
In brief, a Cycle
is a list of proposals.
The state of the Markov chain will be logged only after all Proposal
s in
the Cycle
have been completed, and the iteration counter will be increased
by one. The order in which the Proposal
s are executed is specified by
Order
. The default is RandomO
.
No proposals with the same name and description are allowed in a Cycle
, so
that they can be uniquely identified.
Define the order in which Proposal
s are executed in a Cycle
. The total
number of Proposal
s per Cycle
may differ between Order
s (e.g., compare
RandomO
and RandomReversibleO
).
RandomO | Shuffle the |
SequentialO | The |
RandomReversibleO | Similar to |
SequentialReversibleO | Similar to |
Settings
module Mcmc.Settings
Monitors
A Monitor
describes which part of the Markov chain should be logged
and where. Monitor files can directly be loaded into established MCMC
analysis tools working with tab separated tables (for example,
Tracer).
There are three different Monitor
types:
MonitorStdOut
- Log to standard output.
MonitorFile
- Log to a file.
MonitorBatch
- Log summary statistics such as the mean of the last states to a file.
A Monitor
observing the chain.
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.
Monitor (MonitorStdOut a) [MonitorFile a] [MonitorBatch a] |
data MonitorStdOut a Source #
Monitor to standard output; constructed with monitorStdOut
.
:: [MonitorParameter a] | Instructions about which parameters to log. |
-> Int | Logging period. |
-> MonitorStdOut a |
Monitor to standard output.
data MonitorFile a Source #
Monitor to a file; constructed with monitorFile
.
:: String | Name; used as part of the file name. |
-> [MonitorParameter a] | Instructions about which parameters to log. |
-> Int | Logging period. |
-> MonitorFile a |
Monitor parameters to a file.
data MonitorBatch a Source #
Batch monitor to a file.
Calculate summary statistics over the last given number of iterations (batch
size). Construct with monitorBatch
.
:: String | Name; used as part of the file name. |
-> [MonitorParameterBatch a] | Instructions about how to calculate the summary statistics. |
-> Int | Batch size. |
-> MonitorBatch a |
Batch monitor parameters to a file, see MonitorBatch
.
module Mcmc.Monitor.Parameter
module Mcmc.Monitor.ParameterBatch
Prior distributions
Convenience functions for computing priors.
module Mcmc.Prior
MCMC samplers
mcmcContinue :: Algorithm a => Int -> Settings -> a -> IO a Source #
Continue an MCMC algorithm for the given number of iterations.
Currently, it is only possible to continue MCMC algorithms that have completed successfully. This restriction is necessary, because for parallel chains, it is hardly possible to ensure all chains are synchronized when the process is killed.
See:
See also settingsLoad
, mhgLoad
, and mc3Load
.
Algorithms
module Mcmc.Algorithm.Metropolis
module Mcmc.Algorithm.MC3
Useful type synonyms
type PriorFunction a = a -> Log Double Source #
Prior function.
noPrior :: PriorFunction a Source #
Flat prior function. Useful for testing and debugging.
type LikelihoodFunction a = a -> Log Double Source #
Likelihood function.
noLikelihood :: LikelihoodFunction a Source #
Flat likelihood function. Useful for testing and debugging.