# Gibbs

[Robert and Casella (2013)](https://www.springer.com/gp/book/9781475730715) gives the following algorithm,

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrNrmAA-iUxB-9q%2Fgibbs.png?generation=1534951543651348\&alt=media)

while [Liu, Jun S. (2008)](https://www.springer.com/gp/book/9780387763699) introduces two types of Gibbs sampling strategy.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKpQsMjjdpIsAliI8jl%2F-LKpQsnlOZISdk-5DhHt%2Ftwo-types-gibbs.png?generation=1535273570875593\&alt=media)

## Bivariate Gibbs sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrPqB3JkDsAvglh%2Fbi-gibbs.png?generation=1534951543669961\&alt=media)

It is easy to implement this sampler:

```julia
## Julia program for Bivariate Gibbs sampler
## author: weiya <szcfweiya@gmail.com>
## date: 2018-08-22

function bigibbs(T, rho)
    x = ones(T+1)
    y = ones(T+1)
    for t = 1:T
        x[t+1] = randn() * sqrt(1-rho^2) + rho*y[t]
        y[t+1] = randn() * sqrt(1-rho^2) + rho*x[t+1]
    end
    return x, y
end

## example
bigibbs(100, 0.5)
```

## Completion Gibbs Sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrR0J0KpYS_WJu4%2Fc-gibbs.png?generation=1534951552123113\&alt=media)

Example:

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrTE19_kIRNWghw%2Fex-7-1-5.png?generation=1534951552566162\&alt=media)

We can use the following Julia program to implement this algorithm.

```julia
## Julia program for Truncated normal distribution
## author: weiya <szcfweiya@gmail.com>
## date: 2018-08-22

# Truncated normal distribution
function rtrunormal(T, mu, sigma, mu_down)
    x = ones(T)
    z = ones(T+1)
    # set initial value of z
    z[1] = rand()
    if mu < mu_down
        z[1] = z[1] * exp(-0.5 * (mu - mu_down)^2 / sigma^2)
    end
    for t = 1:T
        x[t] = rand() * (mu - mu_down + sqrt(-2*sigma^2*log(z[t]))) + mu_down
        z[t+1] = rand() * exp(-(x[t] - mu)^2/(2*sigma^2))
    end
    return(x)
end

## example
rtrunormal(1000, 1.0, 1.0, 1.2)
```

## Slice Sampler

[Robert and Casella (2013)](https://www.springer.com/gp/book/9781475730715) introduces the following slice sampler algorithm,

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrVRFmxgtsZF8H0%2Fslice.png?generation=1534951543604693\&alt=media)

and [Liu, Jun S. (2008)](https://www.springer.com/gp/book/9780387763699) also presents the slice sampler with slightly different expression:

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKpQsMjjdpIsAliI8jl%2F-LKpQsnoNMTpJBCMUACA%2Fslice-lj.png?generation=1535273570949287\&alt=media)

In my opinion, we can illustrate this algorithm with one dimensioanl case. Suppose we want to sample from normal distribution (or uniform distribution), we can sample uniformly from the region encolsed by the coordinate axis and the density function, that is a bell shape (or a square).

Consider the normal distribution as an instance.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKXEQxs_nQtHkscYjBs%2F-LKXERrXigC81_15AJ-B%2Fex-7-1-7.png?generation=1534951551013929\&alt=media)

It is also easy to write the following Julia program.

```julia
## Julia program for Slice sampler
## author: weiya <szcfweiya@gmail.com>
## date: 2018-08-22

function rnorm_slice(T)
    x = ones(T+1)
    w = ones(T+1)
    for t = 1:T
        w[t+1] = rand() * exp(-1.0 * x[t]^2/2)
        x[t+1] = rand() * 2 * sqrt(-2*log(w[t+1])) - sqrt(-2*log(w[t+1]))
    end
    return x[2:end]
end

## example
rnorm_slice(100)
```

## Data Augmentation

A special case of Completion Gibbs Sampler.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_xx7n2Q_YkXc8cGFQ%2F-LK_xx_-r27b8Gd_48Xd%2Fdataaug.png?generation=1535014067529817\&alt=media)

Let's illustrate the scheme with grouped counting data.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_xx7n2Q_YkXc8cGFQ%2F-LK_xx_1dHml3OPTzjDS%2Fex-7-2-2.png?generation=1535014067679916\&alt=media)

And we can obtain the following algorithm,

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_xx7n2Q_YkXc8cGFQ%2F-LK_xx_3T8vM75N2cd8A%2Fpois_gamma_gibbs.png?generation=1535014067741747\&alt=media)

But it seems to be not obvious to derive the above algorithm, so I wrote some more details

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKo0R_V9ZsgCMIZPBHJ%2F-LKo0S4piQ-7-L1Nxumz%2Fdetails_alg35_fix.jpg?generation=1535249865494614\&alt=media)

[Liu, Jun S. (2008)](https://www.springer.com/gp/book/9780387763699) also presents the DA algorithm which based on Bayesian missing data problem.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKp_IZOptNLKZqXWes9%2F-LKp_JE0gpctKh0PxMDi%2FDA-lj.png?generation=1535276042725503\&alt=media)

Then he argues that $$m$$ copies of $$\mathbf y\_{mis}$$ in each iteration is not really necessary. And briefly summary the DA algorithm:

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKpasGXlsOhrcOutepA%2F-LKpasif0I2hhVal-dxZ%2Fsummary-DA.png?generation=1535276454346115\&alt=media)

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKpbIcT4rG_UdXRddwH%2F-LKpbJHWEnnyXvIlsBgv%2Fscheme-DA.png?generation=1535276567433522\&alt=media)

It seems to agree with the algorithm presented by [Robert and Casella (2013)](https://www.springer.com/gp/book/9781475730715).

Another example:

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKlldS-kpMW0hUIRzU2%2F-LKlle2i3RE0sJoIXbkI%2Fex-7-2-3.png?generation=1535212169332909\&alt=media)

It seems that we do not need to derive the explicit form of $$g(x, z)$$, if we can directly obtain the conditional distribution. We can use the following Julia program to sample.

```julia
## Julia program for Grouped Multinomial Data (Ex. 7.2.3)
## author: weiya <szcfweiya@gmail.com>
## date: 2018-08-26

# call gamma function
#using SpecialFunctions


# sample from Dirichlet distributions
using Distributions

function gmulti(T, x, a, b, alpha1 = 0.5, alpha2 = 0.5, alpha3 = 0.5)
    z = ones(T+1, size(x, 1)-1) # initial z satisfy `z <= x`
    mu = ones(T+1)
    eta = ones(T+1)
    for t = 1:T
        # sample from g_1(theta | y)
        dir = Dirichlet([z[t, 1] + z[t, 2] + alpha1, z[t, 3] + z[t, 4] + alpha2, x[5] + alpha3])
        sample = rand(dir, 1)
        mu[t+1] = sample[1]
        eta[t+1] = sample[2]
        # sample from g_2(z | x, theta)
        for i = 1:2
            bi = Binomial(x[i], a[i]*mu[t+1]/(a[i]*mu[t+1]+b[i]))
            z[t+1, i] = rand(bi, 1)[1]
        end 
        for i = 3:4
            bi = Binomial(x[i], a[i]*eta[t+1]/(a[i]*eta[t+1]+b[i]))
            z[t+1, i] = rand(bi, 1)[1]
        end
    end
    return mu, eta
end

# example

## data
a = [0.06, 0.14, 0.11, 0.09];
b = [0.17, 0.24, 0.19, 0.20];
x = [9, 15, 12, 7, 8]; 

gmulti(100, x, a, b)
```

## Reversible Data Augmentation

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_zRahTaTQehuIUXBc%2F-LK_zSP5JW8rA6IwOpGV%2Frdataaug.png?generation=1535014460650179\&alt=media)

## Reversible Gibbs Sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_zRahTaTQehuIUXBc%2F-LK_zSP7LS1fMr8SWk2o%2Frgibbs.png?generation=1535014460549641\&alt=media)

## Random Sweep Gibbs Sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_zRahTaTQehuIUXBc%2F-LK_zSP94BLHsouqG8pW%2Frsgibbs.png?generation=1535014460651239\&alt=media)

## Random Gibbs Sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LK_zRahTaTQehuIUXBc%2F-LK_zSPBpWXfdXC7u-Eb%2Frandgibbs.png?generation=1535014460686198\&alt=media)

## Hybrid Gibbs Samplers

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LWKu-6PExfX8m1SVbyS%2F-LWKu0eVEdS0ZkiFMe56%2FhybridMCMC.png?generation=1547629503457339\&alt=media)

## Metropolization of the Gibbs Sampler

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKlldS-kpMW0hUIRzU2%2F-LKlle2gfzPrn9jjZ0S_%2Fmetrogibbs.png?generation=1535212169306365\&alt=media)

Let us illuatrate this algorithm with the following example.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKlldS-kpMW0hUIRzU2%2F-LKlle2i3RE0sJoIXbkI%2Fex-7-2-3.png?generation=1535212169332909\&alt=media)

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKlldS-kpMW0hUIRzU2%2F-LKlle2kb4V_v9s8j5lR%2Fex-7-3-8.png?generation=1535212169350979\&alt=media)
