1

I am trying to generate samples of MCMC using R and I found an interesting point.

At every i-th step, I add new sample as follows

for(i in 1: M){
newsample=generate_sample(y.vec[i]);
y.vec[i+1]=newsample;
}

As a consequence, I could generate length of M(10^8) but it takes a lot of time, say 3 days.

Accidentally, I changed it to double for-loop statement

for(j in 1: K){
   for(i in 1: L){
   newsample=generate_sample(y.vec[i]);
   y.vec[i+1]=newsample;
   }
y.vec.total=c(y.vec.total,y.vec);
}

I had thought that the second code would be inefficient however it takes only 1 hour to generate K*L=(10000*10000) samples.

It seems that computation cost increases exponentially when vector of relatively long length is handled.

Am I right?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 4
    Have you pre-declared `y.vec` and `y.vec.total` and then running the loop on it? In other words, if you are appending an element to them on each iteration then it will take long, but if you are only updating an element then it should be much quicker. – TheComeOnMan Dec 10 '13 at 03:44
  • 3
    See R inferno by Patrick Burns. – Roman Luštrik Dec 10 '13 at 09:42
  • 1
    Numerous methods that generate sample are vectorised such as you don't need to use the function to generate number for each value alone, but for all values at once. for instance you will do 'rnorm(10)' instead of 'for (i in 1:10) u[i] <- rnorm(1)' – droopy Dec 10 '13 at 10:19

0 Answers0