简体   繁体   English

如何从多元正态分布创建多个观察?

[英]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)

Edit编辑

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.

相关问题 如何从标准正态值生成多元正态分布? - How to generate multivariate Normal distribution from a standard normal value? 如何使用大熊猫创建多元正态分布的相关矩阵? - how to use pandas to create correlation matrix of multivariate normal distribution? 如何从多元正态分布中采样“n=1000”向量? - How to sample `n=1000` vector from Multivariate Normal distribution? 您将如何在 Torch 中从头开始编写多元正态分布? - How would you write the Multivariate Normal Distribution from scratch in Torch? 是否可以使用 sympy 创建符号多元正态分布? - Is it possible to create a symbolic multivariate normal distribution with sympy? 当我仅在 python 中提供百分比时,如何从多元正态分布中获取上限值和下限值? - How to get the Upper and Lower Values from a Multivariate Normal Distribution when I only supply a percentage in python? 多元正态分布拟合数据集 - Multivariate Normal Distribution fitting dataset 多元正态分布样本上的 Kmean - Kmean on multivariate normal distribution samples 在多元正态分布中找出概率 - finding probability in multivariate normal distribution Python中多元正态分布的整合 - Integration of Multivariate Normal Distribution in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM