#Example Metropolis-Hastings 
#Target distribution 
f= function(x)
{
    dnorm(x,0,1)
}



N = 100000    # Number of iterations
x = rep(NA,N)


#Initial value
x =  0  
acc = 0
for(i in 2:N)
{
  y = runif(1,-1,1) # proposal  This gives the failure...
  R = f(y)*0.5/(f(x[i-1])*0.5 )       # acceptance ratio 
                                      # note that the acceptance rate is min(1,R),
  if(runif(1)<R)                      # The syntax her will give that since we allways accept if R>1 
  {
    x[i] = y
    acc = acc+1
  }
  else
   x[i] = x[i-1]
}



par(mfrow=c(1,1))
hist(x,100,prob=T,ylim=c(0,0.7), xlim=c(-4, 4),main='Failure of independence sampler\n Irreducible error')
y = seq(-3,3,length=600)
lines(y,f(y),col=2)




