简体   繁体   English

使用scipy.stats进行pdf估算

[英]pdf estimation with scipy.stats

Say I compute the density of Beta(4,8): 假设我计算Beta(4,8)的密度:

from scipy.stats import beta
rv = beta(4, 8)
x = np.linspace(start=0, stop=1, num=200)
my_pdf = rv.pdf(x)

Why does the integral of the pdf not equal one? 为什么pdf的积分不等于1?

> my_pdf.sum()
199.00000139548044

The integral over the pdf is one. pdf上的积分是一个。 You can see this by using numerical integration from scipy 你可以通过使用scipy的数值积分来看到这一点

>>> from scipy.integrate import quad
>>> quad(rv.pdf, 0, 1)
(0.9999999999999999, 1.1102230246251564e-14)

or by writing your own ad-hoc integration (with a trapezoidal rule in this example) 或者通过编写自己的ad-hoc集成(在此示例中使用梯形规则)

>>> x = numpy.linspace(start=0, stop=1, num=201)
>>> (0.5 * rv.pdf(x[0]) + rv.pdf(x[1:-1]).sum() + 0.5 * rv.pdf(x[-1])) / 200.0
1.0000000068732813

rv.pdf returns the value of the pdf at each value of x . rv.pdfx每个值处返回pdf的值。 It doesn't sum to one because your aren't actually computing an integral. 它并不总和,因为你实际上并不是计算积分。 If you want to do that, you need to divide your sum by the number of intervals, which is len(x) - 1 , which is 199. That would then give you a result very close to 1. 如果你想这样做,你需要将你的总和除以间隔的数量,即len(x) - 1 ,即199.这将给你一个非常接近1的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM