# Monte Carlo simulation with two random variables with correlation

39 ビュー (過去 30 日間)
Angelavtc 2023 年 2 月 3 日
コメント済み: Angelavtc 2023 年 2 月 9 日
Dear community,
I am currently running 1000 Monte Carlo simulations of two random variables (Q_d and Q_g), changing the mean and standard deviation. I specify 1000 values for each mean and standard deviation for each random variable. As shown below.
n=10000
x1 = 75:125;
y1 = 0:40;
Q_d=zeros(n,length(x1),length(y1));
for i = 1:length(x1) % mean
for j = 1:length(y1) % sd
Q_d(:,i,j)=normrnd(x1(i),y1(j),n,1);
end
end
x2 = 55:105;
y2 = 0:40;
Q_g=zeros(n,length(x2),length(y2));
for i = 1:length(x2) % mean
for j = 1:length(y2) % sd
Q_g(:,i,j)=normrnd(x2(i),y2(j),n,1);
end
end
I want to modify my simulations to introduce a correlation between the variables that I simulate (Q_d and Q_g), for example, a rho=-.8 Any idea how I can modify my code?
I have read that copulas could solve my problem, but I don't quite understand how to do it. Thank you very much for any hints!

サインインしてコメントする。

### 採用された回答

Paul 2023 年 2 月 3 日
Because Qd and Qg are normal, perhaps mvnrnd is all that's needed.
##### 5 件のコメント表示非表示 4 件の古いコメント
Angelavtc 2023 年 2 月 5 日
Thank you very much, @Paul. It is true that with n-D arrays, it is much simpler. Thank you very much for showing me the way :)

サインインしてコメントする。

### その他の回答 (1 件)

Angelavtc 2023 年 2 月 8 日

Dear @Paul. Sorry to bother you again, but I'm trying to change this code and struggling with how to do it.
Now, I want to fix the Mean of the two distributions to, say, Mean [28,13] and generate simulations (size nSamples) where only the standard deviations (std_g, std_d)and the correlation between the two change (going from -1 to 1).
For each std_g and std_d value, I must generate nMonteCarlo covariance matrices with different "rho." But, I am struggling with the for loop that allows me to create these covariance matrices.
We had said that using a cell array is not the best. However, I can't find a way to simplify the problem. The worst of it is that my code doesn't run. This is what I have done:
nMonteCarlo = 50;
newMean = [28 13];
g = 0;
h = 10;
r5= (h-g).*rand(nMonteCarlo,1) + g;
std_g = sort (r5, 'ascend'); % got rid of the transpose operator
g = 0;
h = 4;
r5= (h-g).*rand(nMonteCarlo,1) + g;
std_d = sort (r5, 'ascend'); % got rid of the transponse operator
nSamples = 100;
g = -1;
h = 1;
r6= (h-g).*rand(nMonteCarlo,1) + g;
rho = sort (r6, 'ascend');
%For the set of nMonteCarlo simulations I did this:
SSigma =repmat({[NaN NaN;NaN NaN]}, 1, nMonteCarlo, nMonteCarlo);
for i=1:nMonteCarlo
for j=nMonteCarlo
SSigma{:,i,j}(1,1)=std_d(j).^2;
SSigma{:,i,j}(2,2)=std_g(j).^2;
SSigma{:,i,j}(1,2)=std_d(j).*std_g(j).*rho(i);
SSigma{:,i,j}(2,1)=SSigma{1,i,j}(1,2);
end
end
For some reason, the loop is not changing the entries in the SSigma matrices. Also, afterward, how could I jump into the simplest way to generate the simulations with bivariate distribution corresponding to each matrix?
Any help, again, would be greatly appreciated :)
##### 4 件のコメント表示非表示 3 件の古いコメント
Angelavtc 2023 年 2 月 9 日
Thank you @Paul your explanation was pretty clear. So when I said "separate the two simulations" I I meant this:
Q_d = zeros(nSamples,nMonteCarlo, nMonteCarlo);
for i = 1:nMonteCarlo
for j = 1:nMonteCarlo
Q_d(:,i,j)= squeeze(Q(i,j,:,1));
end
end
Q_g = zeros(nSamples,nMonteCarlo, nMonteCarlo);
for i = 1:nMonteCarlo
for j = 1:nMonteCarlo
Q_g(:,i,j)= squeeze(Q(i,j,:,2));
end
end
Your explanation regarding the ¨squeeze command helped me a lot. Again, I thank you for all the support :)

サインインしてコメントする。

### カテゴリ

Find more on Extreme Value Distribution in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by