【发布时间】:2021-03-16 03:11:32
【问题描述】:
我需要使用以下方法创建随机游走算法 instructions
到目前为止,这是我所拥有的:
target = function(x){
return(ifelse(x<0,0,exp(-x)))
}
x = rep(0,1000)
x[1] = 4 #initialize; I've set arbitrarily set this to 3
for(i in 2:10000){
current_x = x[4]
proposed_x = current_x + rnorm(1,mean=0,sd=1)
A = target(proposed_x)/target(current_x)
if(runif(1)<A){
x[i] = proposed_x # accept move with probability min(1,A)
} else {
x[i] = current_x # otherwise "reject" move, and stay where we are
}
}
plot(x,main="values of x visited by the MH algorithm")
hist(x,xlim=c(0,10),probability = TRUE, main="Histogram of values of x visited by MH algorithm")
xx = seq(0,10,length=100)
lines(xx,target(xx),col="red")
我的代码的结果是:results 1 & results 2。
如何重新创建 MCMC 随机游走实验?
【问题讨论】:
标签: r markov-chains