繁体   English   中英

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

[英]Python/Numpy: Conditional simulation from a multivatiate distribution

使用numpy我可以通过以下方式无条件地根据多元正态分布进行模拟

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

给定5000个x的实现,如何从同一分布模拟y? 我正在寻找可以缩放到任意维度的通用解决方案。

仰望伊顿,莫里斯·L(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]]

可接受地接近预定义的协方差矩阵。 该解决方案也在Wikipedia上进行了简要描述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM