An implementation of the Metropolis-Hastings algorithm for the generation of a Markov chain. This is a default implementation that allows sampling of normally distributed posterior distributions centered on zero with unit standard deviations. Proposals are generated from a multivariate normal distribution with an identity covariance matrix and centered on zero. To alter these sampling and target distributions simply create a new class inheriting from this class and override the appropriate routines.
Evaluates the Hasting's ratio. If the proposal distribution is symmetric, this ratio is unity; however, in the case of an asymmetric distribution this ratio is not ensured to be unity.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xc |
The current state vector. |
|
real(kind=real64), | intent(in), | dimension(size(xc)) | :: | xp |
The proposed state vector. |
The ratio.
Evaluates the proposal distribution PDF at the specified set of variables.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xc |
The array of variables to evaluate. |
The value of the PDF at xc.
Proposes a new sample set of variables. The sample is generated by sampling a multivariate normal distribution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xc |
The current set of variables. |
The proposed set of variables.
Gets the number of accepted steps.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
The number of accepted steps.
Gets a copy of the stored Markov chain.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | optional | :: | bin |
An optional input allowing for a burn-in region. The parameter represents the amount (percentage-based) of the overall chain to disregard as "burn-in" values. The value shoud exist on [0, 1). The default value is 0 such that no values are disregarded. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
The error handling object. |
The resulting chain with each parameter represented by a column.
Gets the length of the chain (number of stored state variables).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
The chain length.
Gets the Cholesky-factored (lower-triangular) form of the proposal covariance matrix.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
The Cholesky-factored form of the proposal covariance matrix store in lower-triangular form.
Gets the covariance matrix of the proposal distribution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
The covariance matrix.
Gets a value determining if the proposal distribution object has been initialized.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
Returns true if the object has been initialized; else, false.
Gets the mean values of the proposal distribution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
An array containing the mean values.
Gets the number of state variables.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(in) | :: | this |
The metropolis_hastings object. |
The number of state variables.
Initializes the multivariate normal distribution used to generate proposals.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | mu |
An N-element array containing the mean values for the distribution. |
|
real(kind=real64), | intent(in), | dimension(:,:) | :: | sigma |
An N-by-N covariance matrix for the distribution. This matrix must be positive-definite. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Initializes the multivariate normal distribution to a mean of zero and a variance of one.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
integer(kind=int32), | intent(in) | :: | n |
The number of state variables. |
||
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Currently, this routine does nothing and is a placeholder for the user that inherits this class to provide functionallity upon acceptance of a proposed value.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
integer(kind=int32), | intent(in) | :: | iter |
The current iteration number. |
||
real(kind=real64), | intent(in) | :: | alpha |
The proposal probabilty term used for acceptance criteria. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xc |
An N-element array containing the current state variables. |
|
real(kind=real64), | intent(in), | dimension(size(xc)) | :: | xp |
An N-element array containing the proposed state variables that were just accepted. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Currently, this routine does nothing and is a placeholder for the user that inherits this class to provide functionallity upon rejection of a proposed value.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
integer(kind=int32), | intent(in) | :: | iter |
The current iteration number. |
||
real(kind=real64), | intent(in) | :: | alpha |
The proposal probabilty term used for acceptance criteria. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xc |
An N-element array containing the current state variables. |
|
real(kind=real64), | intent(in), | dimension(size(xc)) | :: | xp |
An N-element array containing the proposed state variables that were just rejected. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Pushes a new set of state variables onto the buffer.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | x |
The new N-element state array. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
The error handling object. |
Resets the object and clears out the buffer storing the chain values.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
Samples the distribution using the Metropolis-Hastings algorithm.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | xi |
An N-element array containing initial starting values of the state variables. |
|
integer(kind=int32), | intent(in), | optional | :: | niter |
An optional input defining the number of iterations to take. The default is 10,000. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
The error handling object. |
Sets the covariance matrix of the proposal distribution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:,:) | :: | x |
The covariance matrix. This matrix must be positive-definite. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Sets the mean values of the proposal distribution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | x |
The updated mean values. |
|
class(errors), | intent(inout), | optional, | target | :: | err |
An error handling object. |
Returns the probability value from the target distribution at the specified state. The user is expected to overload this routine to define the desired distribution. The default behavior of this routine is to sample a multivariate normal distribution with a mean of zero and a variance of one (identity covariance matrix).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(metropolis_hastings), | intent(inout) | :: | this |
The metropolis_hastings object. |
||
real(kind=real64), | intent(in), | dimension(:) | :: | x |
The state vector. |
The value of the probability density function of the distribution being sampled.