# Controlling random number generation in simulation

조회 수: 3 (최근 30일)
Nick 2017년 10월 17일
답변: Nick 2017년 10월 26일
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 2017년 10월 26일
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

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Sanjana Ramakrishnan 2017년 10월 20일
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.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Nick 2017년 10월 26일
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?

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Binomial Distribution에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by