Python/Numpy:多变量分布的条件模拟

vs3odd8k  于 2023-05-07  发布在  Python
关注(0)|答案(2)|浏览(163)

使用numpy,我可以无条件地模拟多元正态分布

mean = [0, 0]
cov = [[1, 0], [0, 100]]  # diagonal covariance
x, y = np.random.multivariate_normal(mean, cov, 5000).T

假设我有5000个x的实现,我如何从相同的分布模拟y?我在寻找一个可以扩展到任意维度的通用解决方案。

a5g8bdjr

a5g8bdjr1#

在伊顿向上看,莫里斯L. Multivariate Statistics(1983).向量空间方法,我收集了以下4变量系统的示例解决方案,其中2个因变量(前两个)和2个自变量(后两个)

import numpy as np

mean = np.array([1, 2, 3, 4])
cov = np.array(
    [[ 1.0,  0.5,  0.3, -0.1], 
     [ 0.5,  1.0,  0.1, -0.2], 
     [ 0.3,  0.1,  1.0, -0.3], 
     [-0.1, -0.2, -0.3,  0.1]])  # diagonal covariance

c11 = cov[0:2, 0:2] # Covariance matrix of the dependent variables
c12 = cov[0:2, 2:4] # Custom array only containing covariances, not variances
c21 = cov[2:4, 0:2] # Same as above
c22 = cov[2:4, 2:4] # Covariance matrix of independent variables

m1 = mean[0:2].T # Mu of dependent variables
m2 = mean[2:4].T # Mu of independent variables

conditional_data = np.random.multivariate_normal(m2, c22, 1000)

conditional_mu = m2 + c12.dot(np.linalg.inv(c22)).dot((conditional_data - m2).T).T
conditional_cov = np.linalg.inv(np.linalg.inv(cov)[0:2, 0:2])

dependent_data = np.array([np.random.multivariate_normal(c_mu, conditional_cov, 1)[0] for c_mu in conditional_mu])

print np.cov(dependent_data.T, conditional_data.T)
>> [[ 1.0012233   0.49592165  0.28053086 -0.08822537]
    [ 0.49592165  0.98853341  0.11168755 -0.22584691]
    [ 0.28053086  0.11168755  0.91688239 -0.27867207]
    [-0.08822537 -0.22584691 -0.27867207  0.94908911]]

其可接受地接近预定协方差矩阵。维基百科上也简要介绍了解决方案

7lrncoxx

7lrncoxx2#

为了将 @dms_quant 的答案推广到任意数量的维度和条件分布,我们可以添加一个分区参数 k,它将协方差矩阵分成z1z2的边缘分布。下面的示例计算给定z2z1的条件分布。
用于任意大小的覆盖矩阵和0的条件值的修改代码:

conditional_values = (len(cov)-k)*[0]
c11 = cov[0:k, 0:k]
c12 = cov[0:k, k:len(cov)]
c21 = cov[k:len(cov), 0:k]
c22 = cov[k:len(cov), k:len(cov)]

m1 = mean[0:k].T
m2 = mean[k:len(cov)].T

conditional_mu = m1 + c12.dot(np.linalg.inv(c22)).dot((conditional_values - m2).T).T
conditional_cov = np.linalg.inv(np.linalg.inv(cov)[0:k, 0:k])

相关问题