# Controlling random number generation in simulation

3 views (last 30 days)
Nick on 17 Oct 2017
Answered: Nick on 26 Oct 2017
For two iid normal distributions: x and y with
mu_x = 1000, sigma_x = 100, mu_y = 500 and sigma_y = 50
We know that Z = x + y is also normally distributed with
mu_z = mu_x+mu_y = 1500, sigma_z = sigma_x + sigma_y = 150
The one-sided 80% quantile of a normal distribution has z-score = 0.84, thus finding the quantile of the joint distribution z should simply yield:
1500 + 0.84*150 = 1626
But when trying to find this with matlab via monte carlo simulation I'm facing some error caused by the random number generator. Note that this is an easy example i'm trying to implement this idea in a larger, more complicated setting. When generating random instances and computing the 80% quantile I find the following:
>> clear
>> x(:,1) = rand(10000,1);
>> x(:,2) = rand(10000,1);
>> r(1) = makedist('normal',1000,100); r(2) = makedist('normal',500,50);
>> y = [icdf(r(1),x(:,1)) icdf(r(2),x(:,2))];
>> quantile(sum(y,2),0.8)
ans =
1.5941e+03
Which is obviously wrong. Resetting the random seed in between computing vector x yields the desired result:
>> clear
>> rng(1)
>> x(:,1) = rand(1000000,1);
>> rng(1)
>> x(:,2) = rand(1000000,1);
>> r(1) = makedist('normal',1000,100); r(2) = makedist('normal',500,50);
>> y = [icdf(r(1),x(:,1)) icdf(r(2),x(:,2))];
>> quantile(sum(y,2),0.8)
ans =
1.6259e+03
I've tried multiple ways of computing this, i.e. via gaussian copula and the function random and mvnrnd but nothing seems to work. How can I fix this for large simulations where I call such a random number generating function 100000+ times?
Edit: I know this works when I'd use x(:,1) to construct both icdf functions, but specifically don't want this as for other distributions I want to be able to simulate the correlation structure, hence need different random numbers.

Nick on 26 Oct 2017
I figured it out. You need the following which works (and is super logical too)
quantile(x(:,1),0.8)+quantile(x(:,2),0.8) = 1.626

Sanjana Ramakrishnan on 20 Oct 2017
The following are the quantile values with different sample sizes: Sample size: 10000 Quantile value: -1.5971e+03 Sample size: 100000 Quantile value:-1.6262e+03 Sample size:1000000 Quantile value:-1.6259e+03
As we increase the sample size the quantile value gets closer to the expected value. Hence the issue is that 10000 is not enough sample size to get the accurate result.
Nick on 26 Oct 2017
Hi Sanjana, First of all thanks for taking the time. But I don't really get your answer. If I take the first code and increase the sample size I still don't get my desired answer. My question really is in the reasoning behind the seed rather than the sample size. How come I need to use the same seed to get the required amount of 1626? Since both distributions are independent why do they need the same seed?