码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-31 17:29:18      阅读:118      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!