Serving the Quantitative Finance Community

 
User avatar
huayen
Topic Author
Posts: 0
Joined: July 26th, 2008, 10:28 pm

Random numbers in Monte Carlo simulation

May 23rd, 2009, 4:09 pm

Hi,I plan to do 10,000 runs and then calculate average as expectation. For each run, I have 10000 steps, therefore I need to generate 10,000*10000 normally distributed numbers. I am using MATLAB for coding.I heard from someone (a class that I attended) that:In Monte Carlo code, do not include RANDN in a loop, because same seed gives same number. Generate all random numbers initially by generating RANDN(A) for a large enough matrix ATextsdfd However, I tried following MATLAB code:-----a=zeros(5,4);for i=1:5 for j=1:4 a(i,j)=randn(1,1); endenda-----I saw the generated value of a are always different, conflicting with above statement.So, my question are:(1) Is above statement correct?(2) when you do MC simulation, how do you treat the situation of generating 10,000*10000 random numbers? create them at one time at beginning or do in loop structures?(3) Can I directly use rand() function in Matlab to generate uniformly distributed numbers? Is the result from rand() fully-period? Do I need to use some method, such as linear congruential, to create by myself?(4) Do you use some method, such as Box-Mueller, to generate normally distributed numbers?Thanks a lot for your answers.
Last edited by huayen on May 22nd, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
huayen
Topic Author
Posts: 0
Joined: July 26th, 2008, 10:28 pm

Random numbers in Monte Carlo simulation

May 23rd, 2009, 5:39 pm

I figure out what the problem with the code now. Even though each loop of the code gives different random number, everytime run the code still give same sequence.So, skip my previous question. Now, my question is: I need to generate 10,000*10000 =10^8 random numbers, how can I make sure this does not reach the limit of the full period?
 
User avatar
huayen
Topic Author
Posts: 0
Joined: July 26th, 2008, 10:28 pm

Random numbers in Monte Carlo simulation

May 23rd, 2009, 6:33 pm

I checked MATLAB manual, it says that the default random stream is Mersenne twister (used by default stream at MATLAB startup), which has the property of:Approximate Period In Full Precision = 2^19936 -1Since this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?
Last edited by huayen on May 22nd, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
jfuqua
Posts: 6
Joined: July 26th, 2002, 11:41 am

Random numbers in Monte Carlo simulation

May 15th, 2012, 1:08 pm

Is anyone aware of software that uses 'Multiply-and-Carry' for random number generation ?Have views on pro/con of the method ?
 
User avatar
eh
Posts: 3
Joined: March 2nd, 2010, 9:26 am

Random numbers in Monte Carlo simulation

May 15th, 2012, 3:09 pm

QuoteOriginally posted by: huayenI checked MATLAB manual, it says that the default random stream is Mersenne twister (used by default stream at MATLAB startup), which has the property of:Approximate Period In Full Precision = 2^19936 -1Since this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?It will work.In MATLAB, though, you can generate all the random numbers at once with: randomNumbers=randn(10000);
 
User avatar
animeshsaxena
Posts: 18
Joined: June 19th, 2008, 2:56 pm

Random numbers in Monte Carlo simulation

May 17th, 2012, 6:12 am

better to use quasi-random coz convergence is good....Good way to check the quality is 1. Generate 1000 random numbers between (0,1)2. Compute N = (Number of randoms < 0.5)3 Divide N by 1000, see if this is equal or approx equal to 0.5. Similarly try for 0.4 or other numbers. If they are fluctuating...then something is wrong with the way random numbers are being generated.
 
User avatar
quartz
Posts: 3
Joined: June 28th, 2005, 12:33 pm

Random numbers in Monte Carlo simulation

September 20th, 2013, 8:15 am

QuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Random numbers in Monte Carlo simulation

September 20th, 2013, 8:22 am

QuoteOriginally posted by: quartzQuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.Here is a discussion on rand() in CSomeone showed that C rand repeats after 20 10^6 draws
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Random numbers in Monte Carlo simulation

September 22nd, 2013, 3:01 am

QuoteOriginally posted by: outrunQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: quartzQuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.Here is a discussion on rand() in CSomeone showed that C rand repeats after 20 10^6 draws Cool, my GPU can loop that 500 times per second!Is speedup == efficiency of GPU?
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Random numbers in Monte Carlo simulation

September 23rd, 2013, 8:05 am

QuoteOriginally posted by: outrunQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: outrunQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: quartzQuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.Here is a discussion on rand() in CSomeone showed that C rand repeats after 20 10^6 draws Cool, my GPU can loop that 500 times per second!Is speedup == efficiency of GPU?No my point is that if you can do 20 10^6 draws every 2 milliseconds then you're very likely to run into this issue,no? Cuda has it's own rng, which is fine. I'm not sure about matlab.I suppose it depends in the period of the RNG? AFAIR C rand() was 16 bit seed while VBA RAND() was 32 bit and 'better'. Nice thing about RNGs is there are so many to choose from.
Last edited by Cuchulainn on September 22nd, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Strandi
Posts: 1
Joined: May 16th, 2011, 12:46 pm

Random numbers in Monte Carlo simulation

September 27th, 2013, 12:02 pm

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: quartzQuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.Here is a discussion on rand() in CSomeone showed that C rand repeats after 20 10^6 draws Hope this is not true for C#! Any idea about that?
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Random numbers in Monte Carlo simulation

September 28th, 2013, 9:22 am

QuoteOriginally posted by: StrandiQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: quartzQuoteSince this full period value is much larger than the number of random number that I need to generate, it shall be safe to use randn() inside loop structure, tight?Yes, it's fine, don't worry.What the guy probably meant was not the randn() itself in a loop but when paired with random seeding. That's a setup that shall only be done once at startup, because if repeated at each randn() in loops these will each time restart from the same stream point. @animeshsaxena: Quasi-Monte Carlo methods *can* have better convergence but require way much more attention and preparation, otherwise there's still risk of slow convergence or -even worse- fast convergence to biased results.Here is a discussion on rand() in CSomeone showed that C rand repeats after 20 10^6 draws Hope this is not true for C#! Any idea about that?To be honest, I don't know. Maybe C# is OK for this, maybe not.It is safe to say that C++ is further ahead in this area, you can call C# -> C++/CLI -> native good RNG C++ (?) Maybe @quartz knows?
Last edited by Cuchulainn on September 27th, 2013, 10:00 pm, edited 1 time in total.