本文介绍了如何在Python中采样多元对数正态分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python,如何从多元对数正态分布中采样数据?例如,对于多元法线,有两个选择.假设我们有一个3 x 3协方差矩阵和一个3维平均向量mu.

Using Python, how can I sample data from a multivariate log-normal distribution? For instance, for a multivariate normal, there are two options. Let's assume we have a 3 x 3 covariance matrix and a 3-dimensional mean vector mu.

# Method 1
sample = np.random.multivariate_normal(mu, covariance)

# Method 2
L = np.linalg.cholesky(covariance)
sample = L.dot(np.random.randn(3)) + mu

我发现numpy的 numpy.random.lognormal ,但这似乎仅适用于单变量样本.我还注意到scipy的 scipy .stats.lognorm .这似乎有可能用于多变量样本.但是,我不知道该怎么做.

I found numpy's numpy.random.lognormal, but that only seems to work for univariate samples. I also noticed scipy's scipy.stats.lognorm. This does seem to have the potential for multivariate samples. However, I can't figure out how to do this.

推荐答案

多元对数正态分布随机变量Rv应该具有此属性:log(Rv)应该遵循正态分布.因此,问题实际上仅在于生成多元正态分布的随机变量并np.exp.

A multivariate lognormal distributed random variable Rv should have this property: log(Rv) should follow a normal distribution. Therefore, the problem is really just to generation a random variable of multivariate normal distribution and np.exp it.

In [1]: import numpy.random as nr

In [2]: cov = np.array([[1.0, 0.2, 0.3,],
                        [0.2, 1.0, 0.3,],
                        [0.3, 0.3, 1.0]])

In [3]: mu  = np.log([0.3, 0.4, 0.5])

In [4]: mvn = nr.multivariate_normal(mu, cov, size=5)

In [5]: mvn   # This is multivariate normal
Out[5]:
array([[-1.36808854, -1.32562291, -1.9706876 ],
       [-2.13119289,  1.28146425,  0.66000019],
       [-2.82590272, -1.22500654, -0.32635701],
       [-0.4967589 , -0.34469589, -2.04084115],
       [-0.85590235, -1.27133544, -0.70959595]])

In [6]: mvln = np.exp(mvn)

In [7]: mvln   # This is multivariate log-normal
Out[7]:
array([[ 0.25459314,  0.26563744,  0.139361  ],
       [ 0.11869562,  3.60190996,  1.9347927 ],
       [ 0.05925514,  0.29375578,  0.72154754],
       [ 0.60849968,  0.70843576,  0.12991938],
       [ 0.42489961,  0.28045684,  0.49184289]])

这篇关于如何在Python中采样多元对数正态分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:53