Skip to main content

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<normMH <- function(nn=100000) {
t<  t <- rep(NA,n)
t[1]<  t[1] <- 0 #initial value
  for (i in 2:100000n) {
y<    y <- rnorm(1,0,1)
r<    r <- dt(y,df=4)/dt(t[i-1],df=4)*dnorm(t[i-1],0,1)/dnorm(y,0,1) #acceptance# acceptance ratio
accprob<    accprob <- min(1,r)
    if (runif(1)<accprob < accprob){t[i]<
      t[i] <- y}
    else{t[i]<
      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.

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){
t<-rep(NA,n)
t[1]<-0 #initial value
for(i in 2:100000){
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.

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.

Source Link
Patty
  • 1.8k
  • 1
  • 12
  • 23

Metropolis–Hastings algorithm variance isn't converging in R?

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){
t<-rep(NA,n)
t[1]<-0 #initial value
for(i in 2:100000){
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.