Shrinkage Slice (Slice)¶
Implementation of the shrinkage slice sampler of Neal [52] for simulating autocorrelated draws from a distribution that can be specified up to a constant of proportionality.
Stand-Alone Function¶
-
slice!
(v::SliceVariate, width::Vector{Float64}, logf::Function, stype::Symbol=:multivar)¶ Simulate one draw from a target distribution using a shrinkage slice sampler. Parameters are assumed to be continuous, but may be constrained or unconstrained.
Arguments
v
: current state of parameters to be simulated.width
: vector of the same length asv
, defining initial widths of a hyperrectangle from which to simulate values.logf
: function to compute the log-transformed density (up to a normalizing constant) atv.value
.stype
: sampler type. Options are:multivar
: Joint multivariate sampling of parameters.:univar
: Sequential univariate sampling.
Value
Returnsv
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 = [ :x => [1, 2, 3, 4, 5], :y => [1, 3, 3, 3, 5] ] ## Log-transformed Posterior(b0, b1, log(s2)) + Constant logf = function(x) 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 Multivariate Slice Sampling n = 5000 sim1 = Chains(n, 3, names = ["b0", "b1", "s2"]) theta = SliceVariate([0.0, 0.0, 0.0]) width = [1.0, 1.0, 2.0] for i in 1:n slice!(theta, width, logf, :multivar) sim1[i,:,1] = [theta[1:2], exp(theta[3])] end describe(sim1) ## MCMC Simulation with Univariate Slice Sampling n = 5000 sim2 = Chains(n, 3, names = ["b0", "b1", "s2"]) theta = SliceVariate([0.0, 0.0, 0.0]) width = [1.0, 1.0, 2.0] for i in 1:n slice!(theta, width, logf, :univar) sim2[i,:,1] = [theta[1:2], exp(theta[3])] end describe(sim2)
SliceVariate Type¶
Declaration¶
SliceVariate <: VectorVariate
Fields¶
value::Vector{VariateType}
: vector of sampled values.tune::SliceTune
: tuning parameters for the sampling algorithm.
Constructors¶
-
SliceVariate
(x::Vector{VariateType}, tune::SliceTune)¶ -
SliceVariate
(x::Vector{VariateType}, tune=nothing) Construct a
SliceVariate
object that stores sampled values and tuning parameters for slice sampling.Arguments
x
: vector of sampled values.tune
: tuning parameters for the sampling algorithm. Ifnothing
is supplied, parameters are set to their defaults.
Value
Returns aSliceVariate
type object with fields pointing to the values supplied to argumentsx
andtune
.
SliceTune Type¶
Declaration¶
type SliceTune
Fields¶
width::Vector{Float64}
: vector of initial widths defining hyperrectangles from which to simulate values.
Sampler Constructor¶
-
Slice
(params::Vector{Symbol}, width::Vector{T<:Real}, stype::Symbol=:multivar; transform::Bool=false)¶ Construct a
Sampler
object for shrinkage slice sampling. Parameters are assumed to be continuous, but may be constrained or unconstrained.Arguments
params
: stochastic nodes to be updated with the sampler.width
: vector of the same length as the combined elements of nodesparams
, defining initial widths of a hyperrectangle from which to simulate values.stype
: sampler type. Options are:multivar
: Joint multivariate sampling of parameters.:univar
: Sequential univariate sampling.
transform
: whether to sample parameters on the link-transformed scale (unconstrained parameter space). Iftrue
, then constrained parameters are mapped to unconstrained space according to transformations defined by the Stochasticlink()
function, andwidth
is interpreted as being relative to the unconstrained parameter space. Otherwise, sampling is relative to the untransformed space.
Value
Returns aSampler
type object.Example
See the Examples section.