Search code examples
pythonsamplingmcmc

Ignoring samples in Gibbs sampling


import random,math

def gibbs(N=50000,thin=1000):
    x=0
    y=0
    print "Iter  x  y"
    for i in range(N):
        for j in range(thin):
            x=random.gammavariate(3,1.0/(y*y+4))
            y=random.gauss(1.0/(x+1),1.0/math.sqrt(2*x+2))
        print i,x,y

gibbs()

The above python code is the Gibbs Sampling and the following line confuses me.

for j in range(thin):

What is the significance of that additional inner loop?


Solution

  • The reason seems to be the introduction of thinning into your Gibbs sampling. Thinning is used to reduce the effect of correlation between consecutive samples. Gibbs sampling generates a Markov Chain of samples and the nearby samples are correlated, while typically the intention is to draw samples that are independent.

    To achieve that, you can use only every M-th value, while ignoring all intermediate values. In this case M is stored in the variable thinning and you are taking only every thinning value which is then printed below the for loop.