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

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))
时间: 2024-11-09 00:51:21

R code for generating standard normals using Metropolis sampler with uniform proposal distribution的相关文章

MCMC: The Metropolis Sampler

本文主要译自 MCMC: The Metropolis Sampler 正如之前的文章讨论的,我们可以用一个马尔可夫链来对目标分布 \(p(x)\) 进行采样,通常情况下对于很多分布 \(p(x)\),我们无法直接进行采样.为了实现这样的目的,我们需要为马尔可夫链设计一个状态转移算子(transition operator),是的这个马尔可夫链的稳态分布与目标分布吻合.Metropolis 采样算法(更通常的是 Metropolis-Hastings 采样算法)采用简单的启发式方法实现了这样的状

Eclipse android 开发工具报错 R.java not generating

开发中发现我的 R.java文件重来不更新,没有包含我最新的资源信息,我就拔他删除了,以为删除了以后eclipse就会帮我重新生成一个,但是Eclipse 没这么干.... 于是我的android 工程就没有了 R.java文件. 我该如何做?我使用的是win7 提示框中一个相关的提示如下""Doing Project->Clean is what caused the problem for me. Cleaning deletes R.java...and for whate

A Complete Tutorial on Tree Based Modeling from Scratch (in R &amp; Python)

A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON R SHARE  MANISH SARASWAT, APRIL 12, 2016 / 52 Introduction Tree based learning algorithms are considered to be one of the best and mostly used supervised

《R in Nutshell》 读书笔记(连载)

R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshell") 第一部分:基础 第一章 批处理(Batch Mode) R provides a way to run a large set of commands in sequence and save the results to a file. 以batch mode运行R的一种方式是:使用系统

Metropolis Hasting算法

Metropolis Hasting Algorithm: MH算法也是一种基于模拟的MCMC技术,一个非常重要的应用是从给定的概率分布中抽样.主要原理是构造了一个精妙的Markov链,使得该链的稳态 是你给定的概率密度.它的优点,不用多说,自然是能够对付数学形式复杂的概率密度.有人说,单维的MH算法配上Gibbs Sampler差点儿是“无敌”了. 今天试验的过程中发现,MH算法想用好也还不简单,里面的转移參数设定就不是非常好弄.即使用最简单的高斯漂移项,方差的确定也是个头疼的问题,须要不同问

R语言的cpp扩展支持Rcpp模块介绍

概述 Rcpp包提供C++类方便C or C++代码与R软件包进行交互,使用R中提供的.Call() 调用界面. Rcpp提供R中的基础数据类型的C++类供访问.包作者可以保持R的数据结构而无需与C++进行不断的转换.同时,这些数据结构提供C++级别的存取.数据类型可以双向映射.可以从 R中把数据赋给C++, 返回数据从C++到R也完全一样.下面列出支持的数据类型. Transfer from R to C++, and from C++ to R R 数据类型 (SEXP) 与C++对象是一致

Sampling Distributions and Central Limit Theorem in R(转)

The Central Limit Theorem (CLT), and the concept of the sampling distribution, are critical for understanding why statistical inference works. There are at least a handful of problems that require you to invoke the Central Limit Theorem on every ASQ

R Customizing graphics

Customizing graphics GraphicsLaTeXLattice (Treillis) plots In this chapter (it tends to be overly comprehensive: consider it as a reference and feel free to skip it), we consider all the configurable details in graphics: symbols, colours, annotations

R 作业代写代编代码Thinking and Reasoning

R 作业代写代编代码Thinking and ReasoningMAST90044 Thinking and Reasoning with DataInstructions? Assignments are to be placed in the appropriate subject and lab box located just inside the northentrance to the Peter Hall Building. Assignments must be stapled.