# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.stats.random.rng.multivariate.mcmc.metropolis

## Class Metropolis

• All Implemented Interfaces:
RandomVectorGenerator, Seedable

public class Metropolis
extends AbstractMetropolis
This basic Metropolis implementation assumes using symmetric proposal function.
• Wikipedia on Metropolis-Hastings
• Roberts, G. O., Gelman, A., Gilks, W. R. "Weak convergence and optimal scaling of random walk Metropolis algorithms," Ann. Appl. Probab. 1997.
• Liu, Jun S. "Ch.5.1, The Metropolis Algorithm (p.106-107 in pdf)," Monte Carlo Strategies in Scientific Computing. 2002.
• ### Constructor Summary

Constructors
Constructor and Description
Metropolis(RealScalarFunction logf, RealVectorFunction proposalFunction, Vector initialState, RandomLongGenerator uniform)
Constructs a new instance with the given parameters.
Metropolis(RealScalarFunction logf, Vector initialState, double sigma, RandomLongGenerator uniform)
Constructs a new instance, which draws the offset of the next proposed state from the previous state from a standard Normal distribution, with the given variance and zero covariance.
Metropolis(RealScalarFunction logf, Vector initialState, Matrix scale, RandomLongGenerator uniform)
Constructs a new instance, which draws the offset of the next proposed state from the previous state from a standard Normal distribution, multiplied by the given scale matrix.
• ### Method Summary

All Methods
Modifier and Type Method and Description
protected boolean isProposalAccepted(Vector currentState, Vector proposedState)
Decides whether the given proposed state should be accepted, or whether the system should remain in it's current state.
protected Vector nextProposedState(Vector currentState)
Proposes a next state for the system.
• ### Methods inherited from class com.numericalmethod.suanshu.stats.random.rng.multivariate.mcmc.metropolis.AbstractMetropolis

acceptanceRate, nextVector, seed
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### Metropolis

public Metropolis(RealScalarFunction logf,
RealVectorFunction proposalFunction,
Vector initialState,
RandomLongGenerator uniform)
Constructs a new instance with the given parameters.
Parameters:
logf - the log of the unnormalized pdf from which we wish to sample
proposalFunction - generates a proposal for the next state, given the current state. This function MUST be symmetric about it's input argument for the algorithm to converge.
initialState - the initial state of the algorithm
uniform - the random long generator to be used. This should be the same RLG that is used in the proposal function.
• #### Metropolis

public Metropolis(RealScalarFunction logf,
Vector initialState,
double sigma,
RandomLongGenerator uniform)
Constructs a new instance, which draws the offset of the next proposed state from the previous state from a standard Normal distribution, with the given variance and zero covariance.
Parameters:
logf - the log of the unnormalized pdf from which we wish to sample
initialState - the initial state of the algorithm
sigma - the standard deviation the Normal random vector generated by the proposal distribution
uniform - the random long generator to be used. This should be the same RLG that is used in the proposal function.
• #### Metropolis

public Metropolis(RealScalarFunction logf,
Vector initialState,
Matrix scale,
RandomLongGenerator uniform)
Constructs a new instance, which draws the offset of the next proposed state from the previous state from a standard Normal distribution, multiplied by the given scale matrix.
Parameters:
logf - the log of the unnormalized pdf from which we wish to sample
initialState - the initial state of the algorithm
scale - the square root of the covariance matrix, where the next proposal is computed by x+scale*z, where x is the previous proposal and z is a standard Normal random vector
uniform - the random long generator to be used. This should be the same RLG that is used in the proposal function.
• ### Method Detail

• #### nextProposedState

protected Vector nextProposedState(Vector currentState)
Description copied from class: AbstractMetropolis
Proposes a next state for the system.
Specified by:
nextProposedState in class AbstractMetropolis
Parameters:
currentState - the current state of the system
Returns:
the proposed next state
• #### isProposalAccepted

protected boolean isProposalAccepted(Vector currentState,
Vector proposedState)
Description copied from class: AbstractMetropolis
Decides whether the given proposed state should be accepted, or whether the system should remain in it's current state.
Specified by:
isProposalAccepted in class AbstractMetropolis
Parameters:
currentState - the current state of the system
proposedState - the proposed next state of the system
Returns:
whether the system should accept the proposed next state