从python中的多元概率密度函数采样 [英] Sampling from a multivariate probability density function in python

查看:579
本文介绍了从python中的多元概率密度函数采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多元概率密度函数P(x,y,z),我想从中取样.通常,我将使用numpy.random.choice()进行此类任务,但是此函数仅适用于一维概率密度.多元pdf有等效功能吗?

解决方案

这里可以遵循一些不同的路径.

(1)如果P(x,y,z)的因子为P(x,y,z)= P(x)P(y)P(z)(即x,y和z独立)那么您可以分别对每个样品进行采样.

(2)如果P(x,y,z)具有更一般的因式分解,则可以将需要采样的变量数量减少到其他变量的条件.例如.如果P(x,y,z)= P(z | x,y)P(y | x)P(x),则可以依次对x,y(给定x)和z(分别给定y和x)进行采样.

(3)对于某些特定分布,有已知的采样方法.例如.对于多元高斯,如果x是来自均值0和恒等方差高斯的样本(即,仅将每个x_i采样为N(0,1)),则y = L x + m的均值为m,协方差S = LL',其中L是S的下三角Cholesky分解,必须为正定.

(4)对于许多多元分布,以上都不适用,并且应用了更复杂的方案,例如马尔可夫链蒙特卡罗.

也许您对这个问题说的更多,可以给出更具体的建议.

I have a multivariate probability density function P(x,y,z), and I want to sample from it. Normally, I would use numpy.random.choice() for this sort of task, but this function only works for 1-dimensional probability densities. Is there an equivalent function for multivariate pdfs?

解决方案

There a few different paths one can follow here.

(1) If P(x,y,z) factors as P(x,y,z) = P(x) P(y) P(z) (i.e., x, y, and z are independent) then you can sample each one separately.

(2) If P(x,y,z) has a more general factorization, you can reduce the number of variables that need to be sampled to whatever's conditional on the others. E.g. if P(x,y,z) = P(z|x, y) P(y | x) P(x), then you can sample x, y given x, and z given y and x in turn.

(3) For some particular distributions, there are known ways to sample. E.g. for multivariate Gaussian, if x is a sample from a mean 0 and identity covariance Gaussian (i.e. just sample each x_i as N(0, 1)), then y = L x + m has mean m and covariance S = L L' where L is the lower-triangular Cholesky decomposition of S, which must be positive definite.

(4) For many multivariate distributions, none of the above apply, and a more complicated scheme such as Markov chain Monte Carlo is applied.

Maybe if you say more about the problem, more specific advice can be given.

这篇关于从python中的多元概率密度函数采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆