I am trying to simulate the trajectory of the pension assets of one person. In C-like syntax, it looks like this:
daily.wage.growth = 1.001 # deterministic contribution.rate = 0.08 # deterministic 8% Wage = 10 # initial Asset = 0 # initial for (10,000 days) { Asset += contribution.rate * Wage # accreting contributions Wage *= daily.wage.growth * Wage # wage growth Asset *= draw from a normal distribution # Asset returns } cat("Terminal asset = ", Asset, "\n") How can one do this well in R? What I tried so far is to notice that the wage trajectory is deterministic, it does not change from one run to the next, and it can be done in one line. The asset returns trajectory can be obtained using a single call to rnorm(). Both these can be done nicely using R functions (if you're curious, I can give you my code). Using these, I efficiently get a vector of contributions c[] and a vector of returns r[]. But that still leaves the loop: Asset <- 0 for (t in 1:T) { Asset <- c[t] + r[t]*Asset } How might one do this better? I find that using this code, it takes roughly 0.3 seconds per computation of Asset (on my dinky 500 MHz Celeron). I need to do 50,000 of these every now and then, and it's a pain to have to wait 3 hours. It'll be great if there is some neat R way to rewrite the little loop above. -- Ajay Shah Consultant [EMAIL PROTECTED] Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi ______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html