How to find periodic interval and periodic mean through Von mises distribution?

蓝咒 提交于 2020-12-07 04:45:29

问题


I have some data of time (hours of the day). I would like to fit a von mises distribution to this data, and find the periodic mean. How do I do this using scipy in python ?

for example :

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

I am not sure how do I get the distribution (interval probably) and periodic mean of this data using fit or mean or interval methods.


回答1:


Good job on finding the VM distribution. That's half of the battle. But unless I'm mistaken by the formula in the scipy.stats.vonmises docs, the formula assumes that the data is centered at 0, which may not be the case. So we should probably build our own VM distribution. And for our Vm distribution, we'll make sure it is periodic over a 24 hr range instead of the traditional 2pi range. See the code and comments below. Also, I assumed your data were the times that you saw some event happening, you will need to readjust if that is not the case.

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

Click to see the result of the above code * As a quick caveat, you're likely going to need a larger dataset to do some proper fitting and to really establish a population trend.



来源:https://stackoverflow.com/questions/50693921/how-to-find-periodic-interval-and-periodic-mean-through-von-mises-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!