Page 1 of 1

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 4:34 pm
by psyduck
I'm running some rather computationally intensive monte carlo simulations of SDEs, and I am wondering what is more efficient:a) for a sample path of N steps, generate N normal random variables and then call their values at each step. e.g. (in Matlab)dW = sqrt(dt) * rand(1,N)for i = 1:NX = X + dW(1,i);endb) for a sample path of N steps, generate a single normal random variable at each time step. e.g. for i = 1:NdW = sqrt(dt) * rand(1,1)X = X + dW;endthe code is obviously more complicated than that. I just want to know which technique is more efficient. Technique a) is what I am currently using. But, I am concerned that I am using up more memory than I need to by storing a bunch of random variables that only get used one time.

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 8:05 pm
by Cuchulainn
For the Euler method with Boost Random with single-threaded C++ option b) is ~ 20% faster. I suppose calling a function N times is better than filling a vector and accessing it along the path. I used no fancy tricks nor addon libs.Matlab is probably difficult to customise, memory-wise.//Multi-thread is a different ball game.

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 8:25 pm
by psyduck
Thankfully, I don't have to do a lot of MC in my daily work. I just do it once or twice a year. So, investing a significant amount of time into optimizing my code isn't really worth the effort. But, easy changes that have a significant impact on computation time -- like those mentioned above -- are worth knowing. I'm running a quick test right now to see which method is faster. When I have the results, I'll post again.

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 8:37 pm
by Cuchulainn
QuoteOriginally posted by: psyduckThankfully, I don't have to do a lot of MC in my daily work. I just do it once or twice a year. So, investing a significant amount of time into optimizing my code isn't really worth the effort. But, easy changes that have a significant impact on computation time -- like those mentioned above -- are worth knowing. I'm running a quick test right now to see which method is faster. When I have the results, I'll post again.That would be interesting as I am interested in general how Matlab and C++ compare. Do you use SDE toolbox? I would extract the RNG from the path evolver which gives a bit more flexibility; maybe also prefetching/interleaving if you can do parallel in Matlab. Maybe the 2 MC frameworks here are useful in some sense, esp. the modular approach in MC101.http://www.wilmott.com/messageview.cfm? ... adid=86850

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 8:48 pm
by psyduck
No. I don't use the SDE toolbox. I have access to it. So, it would probably be the wise thing to do. But, I prefer to just write the code myself so that I am 100% sure I understand it.

generating RVs for MC simulation of SDE

Posted: January 5th, 2012, 11:04 pm
by psyduck
So, it turns out that option (a) is 33% faster than option (b)...or, depending on how you look at it, option (b) is 50% slower than option (a). Definitely useful to know.

generating RVs for MC simulation of SDE

Posted: January 6th, 2012, 9:27 am
by Cuchulainn
QuoteOriginally posted by: psyduckSo, it turns out that option (a) is 33% faster than option (b)...or, depending on how you look at it, option (b) is 50% slower than option (a). Definitely useful to know.This is interesting. It seems function calls in Matlab are slower and/or array access is fast. I suspect it has to do with how functions are interpreted. Since Matlab is based on Fortran(I believe) array access should be good.You could try inline functions for RNG which might be faster.