在Python中计算累积密度函数的导数 [英] Calculating the derivative of cumulative density function in Python

查看:256
本文介绍了在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屋!

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