求两个直方图的卷积 [英] Finding the convolution of two histograms

查看:35
本文介绍了求两个直方图的卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个随机变量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值。如果有人能在这里纠正我,我将不胜感激。

推荐答案

为了继续前进(走向更模糊的细节),我进一步修改了您的代码,如下所示:

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曲线。

这篇关于求两个直方图的卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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