[英]how to create multiple observations from multivariate normal distribution?
I'm attempting to create n = 100 observations x from multivariate normal distribution (mean, I), while x is a (2,) vector, and it is known that mean follows another multivariate distribution (([0,0],sigma)).我试图从多元正态分布 (mean, I) 中创建 n = 100 个观测值 x,而 x 是 (2,) 向量,并且众所周知,均值遵循另一个多元分布 (([0,0],sigma ))。 How can I achieve these?
我怎样才能实现这些? I was stuck with the size part in np.random.multivariate_normal function and it seems that I never set the dimensions I want.
我被 np.random.multivariate_normal 函数中的尺寸部分困住了,似乎我从未设置过我想要的尺寸。
mean_mean = np.array([0,0])
mean_sigma = np.array([[0.1,0],[0,0.1]])
mu = np.random.multivariate_normal(m0,S0,1).reshape(2,)
u = np.array([0,0])
I = np.array([[1,0],[0,1]])
x = np.random.multivariate_normal(u,I,100)
Since we don't know the value of the mean and instead only know that the mean follows some distribution, we first need to make n=100
samples of the mean, which we can do as so:由于我们不知道均值的值,而只知道均值遵循某种分布,因此我们首先需要制作
n=100
均值样本,我们可以这样做:
import numpy as np
samples = 100
mean_mean = np.array([0, 0])
mean_sigma = np.array([[0.1, 0], [0, 0.1]])
mu = np.random.multivariate_normal(mean_mean, mean_sigma, samples)
With this we can then sample the x
values we desired.有了这个,我们就可以对我们想要的
x
值进行采样。 Unfortunately, the mean we pass to multivariate_normal
must be 1 dimensional, so we can't vectorise the draws over the different means.不幸的是,我们传递给
multivariate_normal
的均值必须是一维的,因此我们无法对不同均值的绘制进行矢量化。 Here, instead, I loop over the means to give our x
values.在这里,相反,我遍历了给出
x
值的方法。
I = np.array([[1, 0], [0, 1]])
x = np.zeros([samples, 2])
for i in range(samples):
x[2*i:2*(i+1)] = np.random.multivariate_normal(mu[i], I, 1)
After a little thought it's easy to vectorise the draws of x
.经过一番思考,很容易对
x
的绘制进行矢量化。 To do so we will draw from the standard multivariate normal and transform the output.为此,我们将从标准的多元正态中提取并转换输出。
x = np.random.multivariate_normal(np.zeros(2), I, samples)
x += mu
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.