有没有办法在 Python 中创建相关变量来设置这些变量的标准差?

Is there a way to create correlated variables in Python setting the standard deviation of these variables?

我想为四个变量创建假数据:身高、体重、年龄和收入。

我用过这个脚本:

cov_matrix = [[1, 0.7, 0, 0],
              [0.7, 1, 0, 0],
              [0, 0, 1, 0.4],
              [0, 0, 0.4, 1]]
correlated = np.random.multivariate_normal([165, 65, 30, 15000], cov_matrix, size=250)
data = pd.DataFrame({
 "Height": correlated[:, 0],
 "Weight": correlated[:, 1],
 "Age": correlated[:, 2],
 "Income": correlated[:, 3]
})

但是结果还不够好,四个变量的标准差(sd)大约为1,我希望我的数据有更多的离散度。例如,变量“Height”的 sd 为 30。

有没有可能在Python中实现这个?

要获得每个特征的方差,只需将这些值放在协方差矩阵的对角线上即可。但是,需要缩放非对角线元素以解决特征差异。

a1 = 0.7*np.sqrt(30*12)
a2 = 0.4*np.sqrt(19*50)
cov_matrix = np.array([[30.0,   a1,  0.0, 0.0],
                       [  a1, 12.0,  0.0, 0.0],
                       [ 0.0,  0.0, 19.0, a2],
                       [ 0.0,  0.0,   a2, 50.0]])

correlated = np.random.multivariate_normal([165, 65, 30, 15000], cov_matrix, size=1000)
print(correlated.var(axis=0))
print(np.corrcoef(correlated.T))

差异:

[28.02834149 11.14644597 18.68960579 49.46234297]

特征间的互相关系数矩阵:

[[ 1.          0.67359842 -0.02016119 -0.02607946]
 [ 0.67359842  1.         -0.00338224 -0.01021924]
 [-0.02016119 -0.00338224  1.          0.37187791]
 [-0.02607946 -0.01021924  0.37187791  1.        ]]

或者,根据原始协方差矩阵生成数据,然后缩放和平移每个特征以获得所需的均值和标准差。这将保留最初预期的相关系数。请注意,在 缩放后添加平均值 ,否则缩放将改变平均值。

# generate correlated features with zero-mean and unit variance
correlated = np.random.multivariate_normal(np.zeros(4), cov_matrix, size=1000)

# multiply by the desired standard deviation to scale the data and add the mean
correlated = correlated.dot(np.diag(np.sqrt([30.0, 12.0, 19.0, 50.]))) + np.array([165, 65, 30, 15000])