# Binary Metropolised Gibbs (BMG)¶

Implementation of the binary-state Metropolised Gibbs sampler described by Schafer [79][80] in which components are drawn sequentially from full conditional marginal distributions and accepted together in a single Metropolis-Hastings step. The sampler simulates autocorrelated draws from a distribution that can be specified up to a constant of proportionality.

## Model-Based Constructor¶

`BMG`(params::ElementOrVector{Symbol}; k::Integer=1)

Construct a `Sampler` object for BMG sampling. Parameters are assumed to have binary numerical values (0 or 1).

Arguments

• `params` : stochastic node(s) to be updated with the sampler.
• `k` : number of parameters to select at random for simultaneous updating in each call of the sampler.

Value

Returns a `Sampler{BMGTune}` type object.

Example

See the Pollution and other Examples.

## Stand-Alone Function¶

`bmg!`(v::BMGVariate, logf::Function; k::Integer=1)

Simulate one draw from a target distribution using the BMG sampler. Parameters are assumed to have binary numerical values (0 or 1).

Arguments

• `v` : current state of parameters to be simulated.
• `logf` : function that takes a single `DenseVector` argument of parameter values at which to compute the log-transformed density (up to a normalizing constant).
• `k` : number of parameters, such that `k <= length(v)`, to select at random for simultaneous updating in each call of the sampler.

Value

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

Example

```################################################################################
## Linear Regression
##   y ~ MvNormal(X * (beta0 .* gamma), 1)
##   gamma ~ DiscreteUniform(0, 1)
################################################################################

using Mamba

## Data
n, p = 25, 10
X = randn(n, p)
beta0 = randn(p)
gamma0 = rand(0:1, p)
y = X * (beta0 .* gamma0) + randn(n)

## Log-transformed Posterior(gamma) + Constant
logf = function(gamma::DenseVector)
logpdf(MvNormal(X * (beta0 .* gamma), 1.0), y)
end

## MCMC Simulation with Binary Metropolised Gibbs
t = 10000
sim = Chains(t, p, names = map(i -> "gamma[\$i]", 1:p))
gamma = BMGVariate(zeros(p))
for i in 1:t
bmg!(gamma, logf)
sim[i, :, 1] = gamma
end
describe(sim)
```

## BMGVariate Type¶

### Declaration¶

`typealias BMGVariate SamplerVariate{BMGTune}`

### Fields¶

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

### Constructors¶

`BMGVariate`(x::AbstractVector{T<:Real})
`BMGVariate`(x::AbstractVector{T<:Real}, tune::BMGTune)

Construct a `BMGVariate` object that stores simulated values and tuning parameters for BMG sampling.

Arguments

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

Value

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

## BMGTune Type¶

### Declaration¶

`type BMGTune <: SamplerTune`