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?