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?
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 caseM
is stored in the variablethinning
and you are taking only everythinning
value which is then printed below the for loop.