求两个直方图的卷积

发布于 2024-11-17 21:58:53 字数 621 浏览 3 评论 0原文

两个随机变量 x 和 y 之和的概率分布由各个分布的卷积给出。我在进行数值计算时遇到了一些麻烦。在下面的示例中,x 和 y 是均匀分布的,它们各自的分布近似为直方图。我的推理是,应该对直方图进行卷积以给出 x+y 的分布。

from numpy.random import uniform
from numpy import ceil,convolve,histogram,sqrt
from pylab import hist,plot,show

n = 10**2

x,y = uniform(-0.5,0.5,n),uniform(-0.5,0.5,n)

bins = ceil(sqrt(n))

pdf_x = histogram(x,bins=bins,normed=True)
pdf_y = histogram(y,bins=bins,normed=True)

s = convolve(pdf_x[0],pdf_y[0])

plot(s)
show()

给出以下内容,

在此处输入图像描述

换句话说,如预期的那样,是三角形分布。但是,我不知道如何找到 x 值。如果有人能在这里纠正我,我将不胜感激。

The probability distribution of the sum of two random variables, x and y, is given by the convolution of the individual distributions. I'm having some trouble doing this numerically. In the following example, x and y are uniformly distributed, with their respective distributions approximated as histograms. My reasoning says that the histograms should be convoluted to give the distribution of, x+y.

from numpy.random import uniform
from numpy import ceil,convolve,histogram,sqrt
from pylab import hist,plot,show

n = 10**2

x,y = uniform(-0.5,0.5,n),uniform(-0.5,0.5,n)

bins = ceil(sqrt(n))

pdf_x = histogram(x,bins=bins,normed=True)
pdf_y = histogram(y,bins=bins,normed=True)

s = convolve(pdf_x[0],pdf_y[0])

plot(s)
show()

which gives the following,

enter image description here

In other words, a triangular distribution, as expected. However, I have no idea how to find the x-values. I would appreciate it if someone could correct me here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-11-24 21:58:53

为了继续前进(朝着更模糊的细节),我进一步调整了您的代码,如下所示:

from numpy.random import uniform
from numpy import convolve, cumsum, histogram, linspace

s, e, n= -0.5, 0.5, 1e3
x, y, bins= uniform(s, e, n), uniform(s, e, n), linspace(s, e, n** .75)
pdf_x= histogram(x, normed= True, bins= bins)[0]
pdf_y= histogram(y, normed= True, bins= bins)[0]
c= convolve(pdf_x, pdf_y); c= c/ c.sum()
bins= linspace(2* s, 2* e, len(c))
# a simulation
xpy= uniform(s, e, 10* n)+ uniform(s, e, 10* n)
c2= histogram(xpy, normed= True, bins= bins)[0]; c2= c2/ c2.sum()

from pylab import grid, plot, show, subplot
subplot(211), plot(bins, c)
plot(linspace(xpy.min(), xpy.max(), len(c2)), c2, 'r'), grid(True)
subplot(212), plot(bins, cumsum(c)), grid(True), show()

因此,给出如下图:
在此处输入图像描述
其中上部代表 PDF(蓝线),它确实看起来很三角形,而模拟(红点)则反映了三角形形状。下半部分代表 CDF,它看起来也很好地遵循预期的 S 曲线。

In order to still move on (towards more murky details), I further adapted your code like this:

from numpy.random import uniform
from numpy import convolve, cumsum, histogram, linspace

s, e, n= -0.5, 0.5, 1e3
x, y, bins= uniform(s, e, n), uniform(s, e, n), linspace(s, e, n** .75)
pdf_x= histogram(x, normed= True, bins= bins)[0]
pdf_y= histogram(y, normed= True, bins= bins)[0]
c= convolve(pdf_x, pdf_y); c= c/ c.sum()
bins= linspace(2* s, 2* e, len(c))
# a simulation
xpy= uniform(s, e, 10* n)+ uniform(s, e, 10* n)
c2= histogram(xpy, normed= True, bins= bins)[0]; c2= c2/ c2.sum()

from pylab import grid, plot, show, subplot
subplot(211), plot(bins, c)
plot(linspace(xpy.min(), xpy.max(), len(c2)), c2, 'r'), grid(True)
subplot(212), plot(bins, cumsum(c)), grid(True), show()

Thus, giving plots something like this:
enter image description here
Where the upper part represents the PDF (blue line), which indeed looks quite triangular and the simulation (red dots), which reflects the triangular shape. Lower part represents the CDF, which also looks to follow nicely the expected S-curve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文