Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

a programming bug with weird consequences

$
0
0

By xi’an

smalvar

(This article was originally published at Xi’an’s Og » R, and syndicated at StatsBlogs.)

One student of mine coded by mistake an independent Metropolis-Hastings algorithm with too small a variance in the proposal when compared with the target variance. Here is the R code of this implementation:

#target is N(0,1)
#proposal is N(0,.01)
T=1e5
prop=x=rnorm(T,sd=.01)
ratop=dnorm(prop,log=TRUE)-dnorm(prop,sd=.01,log=TRUE)
ratav=ratop[1]
logu=ratop-log(runif(T))
for (t in 2:T){
  if (logu[t]>ratav){
    x[t]=prop[t];ratav=ratop[t]}else{x[t]=x[t-1]}
  }

It produces outputs of the following shape
which is quite amazing because of the small variance. The reason for the lengthy freezes of the chain is the occurrence with positive probability of realisations from the proposal with very small proposal density values, as they induce very small Metropolis-Hastings acceptance probabilities and are almost “impossible” to leave. This is due to the lack of control of the target, which is flat over the domain of the proposal for all practical purposes. Obviously, in such a setting, the outcome is unrelated with the N(0,1) target!

Filed under: Kids, R, Statistics, University life Tagged: acceptance probability, convergence assessment, independent Metropolis-Hastings algorithm, Metropolis-Hastings algorithm

Please comment on the article here: Xi’an’s Og » R

Tags: acceptance probability, convergence assessment, independent Metropolis-Hastings algorithm, Kids, Metropolis-Hastings algorithm, R, statistics, University life

…read more

Source:: statsblogs.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles