如何通过冯米塞斯分布找到周期区间和周期均值?



我有一些时间数据(一天中的几个小时(。我想将冯米塞斯分布拟合到这些数据中,并找到周期平均值。如何在 python 中使用 scipy 来做到这一点?

例如:

from scipy.stats import vonmises
data = [1, 2, 22, 23]
A = vonmises.fit(data)

我不确定如何使用拟合或均值或区间方法获取此数据的分布(可能是区间(和周期平均值。

在查找 VM 分布方面做得很好。这是成功的一半。 但是,除非我被scipy.stats.vonmises docs中的公式弄错了,否则该公式假定数据以0为中心,但事实可能并非如此。因此,我们可能应该构建自己的 VM 发行版。对于我们的 Vm 发行版,我们将确保它在 24 小时内是周期性的,而不是传统的 2pi 范围。请参阅下面的代码和注释。另外,我假设您的数据是您看到某些事件发生的时间,如果不是这种情况,您将需要重新调整。

from scipy.optimize import curve_fit
import numpy as np
from matplotlib import pyplot as plt
# Define the von mises kernel density estimator
def circular_von_mises_kde(x,mu,sigma):
# Adjust data to take it to range of 2pi
x = [(hr)*2*np.pi/24 for hr in x]
mu*=2*np.pi/24
sigma*=2*np.pi/24
# Compute kappa for vm kde
kappa = 1/sigma**2
return np.exp((kappa)*np.cos((x-mu)))/(2*np.pi*i0(kappa))
# Assuming your data is occurences of some event at the given hour of the day
frequencies= np.zeros((24))
frequencies[data]=1
hr_data = np.linspace(1,24, 24)
fit_params, cov = curve_fit(circular_von_mises_kde, hr_data, data_to_fit, bounds=(0,24))
plt.plot(hr_data, frequencies, 'k.',label='Raw data')
plt.plot(np.linspace(1,25, 1000), circular_von_mises_kde(np.linspace(1,25, 1000), *fit_params), 'r-',label='Von Mises Fit')
plt.legend()
plt.xlabel('Hours (0-24)')
plt.show()
print('The predicted mean is {mu} and the standard deviation is {sigma}'.format( mu=round(fit_params[0],3), sigma=round(fit_params[1], 3)))

点击查看以上代码的结果 *作为快速警告,您可能需要一个更大的数据集来进行一些适当的拟合并真正建立人口趋势。

最新更新