I'm trying to simulate a sample from a t distribution with 4 degrees of freedom. The candidate density I'm using is a normal(0,1) distribution. Although the mean does converge to 0, the variance keeps converging to 1.5 instead of 2 as a t distribution with 4 degrees of freedom should have. It's especially confusing since the histogram matches the desired t distribution density almost perfectly. The code I am using is this:
normMH <- function(n=100000) {
t <- rep(NA,n)
t[1] <- 0 #initial value
for (i in 2:n) {
y <- rnorm(1,0,1)
r <- dt(y,df=4)/dt(t[i-1],df=4)*dnorm(t[i-1],0,1)/dnorm(y,0,1) # acceptance ratio
accprob <- min(1,r)
if (runif(1) < accprob)
t[i] <- y
else
t[i] <- t[i-1]
}
return(t)
}
When I plot the variance of the chain at each iteration it converges to 1.5, even though a t distribution with 4 degrees of freedom has variance 2.