问题描述
我想生成两个具有指定相关性的随机向量.第二个向量的每个元素必须与第一个向量的对应元素相关联,并且彼此独立.
I want to generate two random vectors with a specified correlation. Each element of the second vector must be correlated with the corresponding element of the first vector and independent of others.
如何在MATLAB中执行此操作?
How could I do this in MATLAB?
通过第一个向量的元素不具有相同分布的方式,我的意思是第一个向量的每个元素应具有不同的方差. (向量由7个具有不同方差的变量组成.
By the way the elements of the first vector dont have the same distribution, I mean each element of the first vector should have different variances. (the vector is made of 7 variable with different variances.
推荐答案
如此Mathworks文章,您可以执行以下操作:
As described in this Mathworks article, you can do the following:
-
生成两个随机向量( i.e 一个具有两列的随机矩阵).假设您希望矩阵中每个元素的分布都是高斯分布,均值和单位方差为零:
Generate two random vectors (i.e a random matrix with two columns). Let's say that you want the distribution of each element in the matrix to be Gaussian with zero mean and unit variance:
N = 1000; %// Number of samples in each vector
M = randn(N, 2);
您显然可以根据自己的喜好使用任何发行版.
You can obviously use any distribution to your liking.
诀窍是:将矩阵与通过所需相关矩阵R
的Cholesky分解获得的上三角矩阵相乘:
Now the trick: multiply the matrix with an upper triangular matrix obtained by the Cholesky decomposition of the desired correlation matrix R
:
R = [1 0.75; 0.75 1]; %// Our correlation matrix, taken from the article
M = M * chol(R);
从修改后的矩阵M
中提取随机向量:
Extract your random vectors from the modified matrix M
:
x = M(:, 1);
y = M(:, 2);
这篇关于生成两个相关的随机向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!