从python中的时间序列数据计算概率分布

MSN*_*MSN 2 python scipy

我有一个关于概率分布函数的问题,我有一个时间序列数据,我想计算不同时间窗口中数据的概率分布。

我已经开发了以下代码,但是我找不到该函数的概率分布值。

a = pd.DataFrame([0.0,
21.660332407421638,
20.56428943581567,
20.597329924045983,
19.313207915827956,
19.104973174542806,
18.031361568112377,
17.904747973652125,
16.705687654209264,
16.534206966165637,
16.347782724271802,
13.994284547628721,
12.870120434556945,
12.794530081249571,
10.660675400742669])
Run Code Online (Sandbox Code Playgroud)

这是我的数据的直方图和密度图:

a.plot.hist()
a.plot.density()
Run Code Online (Sandbox Code Playgroud)

但我不知道如何计算密度曲线下的面积值。

jda*_*amp 5

您可以直接调用scipy.stats.gaussian_kdepandas plot_density方法也使用的方法(请参阅源代码)。此方法返回所需的功能。然后,您可以调用的方法之一scipy.integrate来计算核密度估计值以下的面积,例如

from scipy import stats, integrate

kde = stats.gaussian_kde(a[0])

# Calculate the integral of the kde between 10 and 20:
xmin, xmax = 10, 20
integral, err = integrate.quad(kde, xmin, xmax)

x = np.linspace(-5,20,100)
x_integral = np.linspace(xmin, xmax, 100)

plt.plot(x, kde(x), label="KDE")
plt.fill_between(x_integral, 0, kde(x_integral),
                 alpha=0.3, color='b', label="Area: {:.3f}".format(integral))
plt.legend()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明