在Python中计算累积密度函数的导数 [英] Calculating the derivative of cumulative density function in Python
问题描述
是不是累积密度函数的精确导数是概率密度函数(PDF)?我正在使用numpy.diff()
计算导数,这正确吗?参见下面的代码:
Is it the case that the exact derivative of a cumulative density function is the probability density function (PDF)? I am calculating the derivative using the numpy.diff()
, is this correct? See below code below:
import scipy.stats as s
import matplotlib.pyplot as plt
import numpy as np
wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object
sample = wei.rvs(1000)
shape, loc, scale = s.weibull_min.fit(sample, floc=0)
x = np.linspace(np.min(sample), np.max(sample))
plt.hist(sample, normed=True, fc="none", ec="grey", label="frequency")
plt.plot(x, wei.cdf(x), label="cdf")
plt.plot(x, wei.pdf(x), label="pdf")
plt.plot(x[1:], np.diff(wei.cdf(x)), label="derivative")
plt.legend(loc=1)
plt.show()
如果是这样,我如何缩放导数以使其等同于PDF?
If so, how do I scale the derivative to be equivalent to the PDF?
推荐答案
CDF的派生词是PDF.
The derivative of the CDF is the PDF.
这里是CDF的导数的近似值:
Here is an approximation of the derivative of the CDF:
dx = x[1]-x[0]
deriv = np.diff(wei.cdf(x))/dx
import scipy.stats as s
import matplotlib.pyplot as plt
import numpy as np
wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object
sample = wei.rvs(1000)
shape, loc, scale = s.weibull_min.fit(sample, floc=0)
x = np.linspace(np.min(sample), np.max(sample))
dx = x[1]-x[0]
deriv = np.diff(wei.cdf(x))/dx
plt.hist(sample, normed=True, fc="none", ec="grey", label="frequency")
plt.plot(x, wei.cdf(x), label="cdf")
plt.plot(x, wei.pdf(x), label="pdf")
plt.plot(x[1:]-dx/2, deriv, label="derivative")
plt.legend(loc=1)
plt.show()
收益
请注意,与deriv
关联的x-locations
已转移
用dx/2
表示,因此近似值位于用于计算它的值之间.
Note that the x-locations
associated with deriv
have been shifted
by dx/2
so the approximation is centered between the values used to compute it.
这篇关于在Python中计算累积密度函数的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!