生成两个相关的统一变量

问题描述 投票:-1回答:1

就像标题所暗示的那样,我很难理解我们如何生成两个相关的均匀[0,1]随机变量。我对copulas的想法不熟悉。

我正在努力编写一个MATLAB代码,其中我需要生成两个相关的均匀[0,1]随机变量。

matlab random correlation
1个回答
0
投票

用Gaussian Copula生成相关的均匀随机变量

rho = .75; % Desired target correlation
N = 1000;  % Number of samples
Z = mvnrnd([0 0],[1 rho; rho 1], N);
U = normcdf(Z);  % Correlated U(0,1) random variables

scatterhist(U(:,1),U(:,2),'Direction','out') % Visualize (change `rho` to see impact)

注意:方法不能保证完全达到目标相关性,但应该足够接近许多应用程序。

3 Scatter plots with marginal histograms. First plot has target correlation of 0.25; second plot has target correlation of 0.75, third plot has target correlation of -0.95.

这对于使用inverse transform方法(分析或数值)快速生成相关分布非常有用。两种用例都如下所示。

分析方法

lambda = 2; alpha = 2; beta = 3;
rho = -.35; N = 1000;
Z = mvnrnd([0 0],[1 rho; rho 1], N);
U = normcdf(Z);
X = (-1/lambda)*log(U(:,1)); % Inverse Transform for Exponential
Y = beta*(-log(U(:,2))).^(1/alpha); % Inverse Transform for Weibull
corr(X,Y)
scatterhist(X,Y,'Direction','out')  

数值方法

% Parameters
alpha = 6.7; lambda = 3;
mu = 0.1; sigma = 0.5;
rho = 0.75; N = 1000;
% Make distributions
pd_X = makedist('Gamma',alpha,lambda);
pd_Y = makedist('Lognormal',mu,sigma);
Z = mvnrnd([0 0],[1 rho; rho 1], N);
U = normcdf(Z);
% Use Inverse Transform for marginal distributions (numerically)
X = icdf(pd_X,U(:,1)); % Inverse CDF for X
Y = icdf(pd_Y,U(:,2)); % Inverse CDF for Y
corr(X,Y)
scatterhist(X,Y,'Direction','out')  

参考文献: Inverse Transform Copulas

高斯copula: 罗斯,谢尔顿。 (2013年)。模拟。学术出版社,加利福尼亚州圣地亚哥,第5版。 103--105。

修改了here的相关答案。

© www.soinside.com 2019 - 2024. All rights reserved.