• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Random numbers in Monte Carlo simulation

Joined
2/26/09
Messages
76
Points
16
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 A

However, I tried following MATLAB code:
C++:
a=zeros(5,4);
for i=1:5
    for j=1:4
        a(i,j)=randn(1,1);
    end
end
a

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.
 
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?
 
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 -1

Since 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?
 
Hi Featips,

If you are serious about using Monte Carlo and not just doing an academic exercise, then you should not be using PRNGs. People who really do MC for a living use low-discrepancy numbers (LDNs), definitely not pseudo RNs. At least, the ones who know better. ;-) When using LDNs for your "random numbers", Monte Carlo is called "Quasi Monte Carlo". Back in the day, all the physics groups in my department that did MC simulations (the pragmatic "I want a job after graduating" kind of physicists like people in solid state, condensed matter, or surface physics) would only use QMC.

There are many, many types of LDNs, but the one most people seem to use the most are Sobol sequences. See here:
https://www.quantnet.com/threads/nvidia-cuda-toolkit-for-options-pricing.1436/

In my own experience, the Halton sequence is quickly generated but has this strange symmetry that makes it unsuitable for certain problems that have inherent symmetry built in. It's like sitting on a park bench with your eyes closed while a cosine wave f(x,t) = A Cos(kx - wt) passes by you and opening your eyes every (kx - n pi) / w seconds. You'll have a very distorted view of what a cosine wave looks like. Probably best to stick with Sobol if you're not out to investigate the merits of the different LDN schemes.

Peter
 
And as luck would have it, there are free Sobol implementations for just about anything you'd care to use, including MATLAB.
 
Thank you so much, Peter. I will learn something about LDN ...

No problemo. I'm a working grunt, so I only scan the forums when my code is compiling or I'm waiting for output. :) Feel free to ask further about LDN and MC, since it's an interest of mine.
 
To some extents, in MC, we need "equally" distributed according to a distribution, not really random. This is why LDN is good candidate.
 
To some extents, in MC, we need "equally" distributed according to a distribution, not really random. This is why LDN is good candidate.

Can you explain in more detail about 'equally' distributed? Thanks a lot.
 
I am learning LDN now.....

But......I am new to this area, so I am still confused why we bother with LDN .......

For example, suppose I need to use Monte Carlo simulation for valuing a 30-year contract. I plan to run 10,000 times of calculation and find average. Using dt=1 day, I need to generate (10000) x (30*365) random numbers.

Intuitively using following code:

C++:
for i=1:10000
   for j=1:30*365
      W=randn(1);
      ... ...
   end
   ... ...
end
The Matlab manual shows that the default random number stream of MATLAB is 'Mersenne twister', and the 'Approximate Period In Full Precision=2^19936-1'.

Since 10000*365*30 is much smaller than 2^19936-1, I assume the generated random number stream using above loop structure will not include repeated number, so putting RANDN() inside loop structure is ok.

Can you point out what's the problem of coding in this way?

I know this is a very simple question to you guys, but it is still a conceptual confusion to me. Anyway, I am learning some Monte Carlo book and LDN.......

Thanks a lot for your answer.
 
There's nothing wrong with that way. However, since your end goal is to get a better price for your contract, and not to get a single random outcome for the contract, there is a better method. That method is to use non-random, i.e. Low-discrepancy, numbers. These are "random" numbers designed to give better convergence characteristics than actual random numbers.

In a sense, we have additional information about the problem that we exploiting beyond simply wanting a large set of outcomes.

I am learning LDN now.....

But......I am new to this area, so I am still confused why we bother with LDN .......

For example, suppose I need to use Monte Carlo simulation for valuing a 30-year contract. I plan to run 10,000 times of calculation and find average. Using dt=1 day, I need to generate (10000) x (30*365) random numbers.

Intuitively using following code:

C++:
for i=1:10000
   for j=1:30*365
      W=randn(1);
      ... ...
   end
   ... ...
end
The Matlab manual shows that the default random number stream of MATLAB is 'Mersenne twister', and the 'Approximate Period In Full Precision=2^19936-1'.

Since 10000*365*30 is much smaller than 2^19936-1, I assume the generated random number stream using above loop structure will not include repeated number, so putting RANDN() inside loop structure is ok.

Can you point out what's the problem of coding in this way?

I know this is a very simple question to you guys, but it is still a conceptual confusion to me. Anyway, I am learning some Monte Carlo book and LDN.......

Thanks a lot for your answer.
 
Can you explain in more detail about 'equally' distributed? Thanks a lot.

What we want (in MC) is to sample the whole outcome space and avoid bias towards certain results. "equally" actually cannot be easily defined, but may be easy to imagine. The key point is in MC, we don't really need randomness. For example, consider the following two sequence of 8 "seeds"

0, 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, 1
0, 3/8, 1/8, 3/4, 1, 1/2, 5/8, 7/8, 1/4

The 2nd is more random than the first one. But in MC, they probably produce the same result.

However, if we assume the distribution is uniform within [0,1], both of the above will produce more reliable result than the following sequence (using 8 paths), even though the following one is much more readom

0.234, 0.12, 0.9999, 0.17, 0.0001, 0.0076, 0.123
 
Back
Top