[R] speed issue in simulating a stochastic process

2014-11-06 Thread Matteo Richiardi
I wish to simulate the following stochastic process, for i = 1...N individuals and t=1...T periods: y_{i,t} = y_0 + lambda Ey_{t-1} + epsilon_{i,t} where Ey_{t-1} is the average of y over the N individuals computed at time t-1. My solution (below) works but is incredibly slow. Is there a faster

Re: [R] speed issue in simulating a stochastic process

2014-11-06 Thread Thomas Adams
Matteo, I tried your example code using R 3.1.1 on an iMac (24-inch, Early 2009), 3.06 GHz Intel Core 2 Duo, 8 GB 1333 MHz DDR3, NVIDIA GeForce GT 130 512 MB running Mac OS X 10.10 (Yosemite). After entering your code, the elapsed time from the time I hit return to when the graphics appeared was

Re: [R] speed issue in simulating a stochastic process

2014-11-06 Thread Thomas Adams
Matteo, Ah — OK, N=20, I did not catch that. You have nested for loops, which R is known to be exceedingly slow at handling — if you can reorganize the code to eliminate the loops, your performance will increase significantly. Tom On Thu, Nov 6, 2014 at 7:47 AM, Matteo Richiardi

Re: [R] speed issue in simulating a stochastic process

2014-11-06 Thread William Dunlap
I find that representing the simulated data as a T row by N column matrix allows for a clearer and faster simulation function. E.g., compare the output of the following two functions, the first of which uses your code and the second a matrix representation (which I convert to a data.frame at the

Re: [R] speed issue in simulating a stochastic process

2014-11-06 Thread William Dunlap
Loops are not slow, but your code did a lot of unneeded operations in each loop. E.g, you computed D$id==i D$t==t for each row of D. That involves 2*nrow(D) equality tests for each of the nrow(D) rows, i.e., it is quadratic in N*T. Then you did a data.frame replacement operation D[k,]$y

Re: [R] speed issue in simulating a stochastic process

2014-11-06 Thread Rolf Turner
SNIP On Thu, Nov 6, 2014 at 2:05 PM, Matteo Richiardi matteo.richia...@gmail.com wrote: SNIP Final question: in your code you have mean(M[t-1L,]): what is the 'L' for? I removed it at apparently the code produces the same output... SNIP The constant 1L is stored as an integer; the