首页 > 其他 > 详细

R code for generating standard normals using Metropolis sampler with uniform proposal distribution

时间:2015-03-31 17:28:02      阅读:126      评论:0      收藏:0      [点我收藏+]

R code for generating standard normals using Metropolis sampler with uniform proposal distribution:

 

# metropolis for N(0,1) based on uniform candidates
norm<-function (n, alpha) 
{
        vec <- vector("numeric", n)
        x <- 0
        vec[1] <- x
        for (i in 2:n) {
                innov <- runif(1, -alpha, alpha)
                can <- x + innov
                aprob <- min(1, dnorm(can)/dnorm(x))
                u <- runif(1)
                if (u < aprob) 
                        x <- can
                vec[i] <- x
        }
        vec
}


normvec<-norm(10000,1)
par(mfrow=c(2,1))
plot(ts(normvec))
hist(normvec,30)
par(mfrow=c(1,1))

 

R code for generating standard normals using Metropolis sampler with uniform proposal distribution

原文:http://www.cnblogs.com/ymshuibingcheng/p/4381319.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!