# Random Walk Metropolis (RWM)¶

Random walk Metropolis-Hastings algorithm [43][60] in which parameters are sampled from symmetric distributions centered around the current values. The sampler simulates autocorrelated draws from a distribution that can be specified up to a constant of proportionality.

## Model-Based Constructor¶

`RWM`(params::ElementOrVector{Symbol}, scale::ElementOrVector{T<:Real}; proposal::SymDistributionType=Normal)

Construct a `Sampler` object for RWM sampling. Parameters are assumed to be continuous, but may be constrained or unconstrained.

Arguments

• `params` : stochastic node(s) to be updated with the sampler. Constrained parameters are mapped to unconstrained space according to transformations defined by the Stochastic `unlist()` function.
• `scale` : scaling value or vector of the same length as the combined elements of nodes `params` for the `proposal` distribution. Values are relative to the unconstrained parameter space, where candidate draws are generated.
• `proposal` : symmetric distribution of type `Biweight`, `Cosine`, `Epanechnikov`, `Normal`, `SymTriangularDist`, `SymUniform`, or `Triweight` to be centered around current parameter values and used to generate proposal draws. Specified `scale` determines the standard deviations of Normal proposals and widths of the others.

Value

Returns a `Sampler{RWMTune}` type object.

Example

See the Dyes and other Examples.

## Stand-Alone Function¶

`rwm!`(v::RWMVariate, scale::ElementOrVector{T<:Real}, logf::Function; proposal::SymDistributionType=Normal)

Simulate one draw from a target distribution using an RWM sampler. Parameters are assumed to be continuous and unconstrained.

Arguments

• `v` : current state of parameters to be simulated.
• `scale` : scalar or vector of the same length as `v` for the `proposal` distribution.
• `logf` : function that takes a single `DenseVector` argument of parameter values at which to compute the log-transformed density (up to a normalizing constant).
• `proposal` : symmetric distribution of type `Biweight`, `Cosine`, `Epanechnikov`, `Normal`, `SymTriangularDist`, `SymUniform`, or `Triweight` to be centered around current parameter values and used to generate proposal draws. Specified `scale` determines the standard deviations of Normal proposals and widths of the others.

Value

Returns `v` updated with simulated values and associated tuning parameters.

Example

The following example samples parameters in a simple linear regression model. Details of the model specification and posterior distribution can be found in the Supplement.

```################################################################################
## Linear Regression
##   y ~ N(b0 + b1 * x, s2)
##   b0, b1 ~ N(0, 1000)
##   s2 ~ invgamma(0.001, 0.001)
################################################################################

using Mamba

## Data
data = Dict(
:x => [1, 2, 3, 4, 5],
:y => [1, 3, 3, 3, 5]
)

## Log-transformed Posterior(b0, b1, log(s2)) + Constant
logf = function(x::DenseVector)
b0 = x[1]
b1 = x[2]
logs2 = x[3]
r = data[:y] - b0 - b1 * data[:x]
(-0.5 * length(data[:y]) - 0.001) * logs2 -
(0.5 * dot(r, r) + 0.001) / exp(logs2) -
0.5 * b0^2 / 1000 - 0.5 * b1^2 / 1000
end

## MCMC Simulation with Random Walk Metropolis
n = 5000
burnin = 1000
sim = Chains(n, 3, names = ["b0", "b1", "s2"])
theta = RWMVariate([0.0, 0.0, 0.0])
scale = [0.5, 0.25, 1.0]
for i in 1:n
rwm!(theta, scale, logf, proposal=SymUniform)
sim[i, :, 1] = [theta[1:2]; exp(theta[3])]
end
describe(sim)
```

## RWMVariate Type¶

### Declaration¶

`typealias RWMVariate SamplerVariate{RWMTune}`

### Fields¶

• `value::Vector{Float64}` : simulated values.
• `tune::RWMTune` : tuning parameters for the sampling algorithm.

### Constructors¶

`RWMVariate`(x::AbstractVector{T<:Real})
`RWMVariate`(x::AbstractVector{T<:Real}, tune::RWMTune)

Construct a `RWMVariate` object that stores simulated values and tuning parameters for RWM sampling.

Arguments

• `x` : simulated values.
• `tune` : tuning parameters for the sampling algorithm. If not supplied, parameters are set to their defaults.

Value

Returns a `RWMVariate` type object with fields set to the values supplied to arguments `x` and `tune`.

## RWMTune Type¶

### Declaration¶

`type RWMTune <: SamplerTune`

### Fields¶

• `scale::Union{Real, Vector}` : scaling for the proposal distribution.
• `proposal::SymDistributionType` : proposal distribution.