gpt4 book ai didi

python - 在Python中计算累积密度函数的导数

转载 作者:太空狗 更新时间:2023-10-29 20:26:07 26 4
gpt4 key购买 nike

累积密度函数的精确导数是概率密度函数 (PDF) 吗?我正在使用 numpy.diff() 计算导数,这是正确的吗?请参见下面的代码:

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()

Compariosn of CDF, PDF and derivative

如果是这样,我如何缩放导数以等效于 PDF?

最佳答案

CDF 的导数是 PDF。

这是 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()

产量

enter image description here

请注意,与 deriv 关联的 x-locations 已被移动通过 dx/2 因此近似值位于用于计算它的值之间。

关于python - 在Python中计算累积密度函数的导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17937979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com