Serving the Quantitative Finance Community

 
User avatar
psyduck
Topic Author
Posts: 0
Joined: January 15th, 2011, 9:12 pm

generating RVs for MC simulation of SDE

January 5th, 2012, 4:34 pm

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.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

generating RVs for MC simulation of SDE

January 5th, 2012, 8:05 pm

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.
Last edited by Cuchulainn on January 4th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
psyduck
Topic Author
Posts: 0
Joined: January 15th, 2011, 9:12 pm

generating RVs for MC simulation of SDE

January 5th, 2012, 8:25 pm

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.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

generating RVs for MC simulation of SDE

January 5th, 2012, 8:37 pm

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
Last edited by Cuchulainn on January 4th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
psyduck
Topic Author
Posts: 0
Joined: January 15th, 2011, 9:12 pm

generating RVs for MC simulation of SDE

January 5th, 2012, 8:48 pm

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.
 
User avatar
psyduck
Topic Author
Posts: 0
Joined: January 15th, 2011, 9:12 pm

generating RVs for MC simulation of SDE

January 5th, 2012, 11:04 pm

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.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

generating RVs for MC simulation of SDE

January 6th, 2012, 9:27 am

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.
Last edited by Cuchulainn on January 5th, 2012, 11:00 pm, edited 1 time in total.