I have several data sets that fit separately very well to a vonMises distribution. I am looking for a way of fitting all of them sharing mu
but with different kappas
without to care about the election of the bins.
When one wants to fit with only one model it is quite trivial: scipy
here does the fit with the raw data. But I have been looking for global fitting using symfit
or lmfit
, or in some posts (here and here), and in all cases we have to specify x-coordinates and y-coordinates, which means with have previously to choose some bin size for the distribution.
This is some artificial data for only two data sets that could be useful as a example of what I need, though fitting each individually, using scipy
. (Note that I don't need to care about the election of the bins).
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
# creating the data
mu1, mu2 = .05, -.05
sigma1, sigma2 = 3.1, 2.9
n1, n2 = 8000, 9000
y1 = np.random.vonmises(mu1, sigma1, n1)
y2 = np.random.vonmises(mu2, sigma2, n2)
# fitting
dist = st.vonmises
*args1, loc1, scale1 = dist.fit(y1, fscale=1)
*args2, loc2, scale2 = dist.fit(y2, fscale=1)
x1 = np.linspace(np.min(y1), np.max(y1), 200)
x2 = np.linspace(np.min(y2), np.max(y2), 200)
pdf_fitted1 = dist.pdf(x1, *args1, loc=loc1, scale=scale1)
pdf_fitted2 = dist.pdf(x2, *args2, loc=loc2, scale=scale2)
# plotting
plt.hist(y1, bins=40, density=True, histtype='step', color='#1f77b4')
plt.hist(y2, bins=40, density=True, histtype='step', color='#ff7f0e')
plt.plot(x1, pdf_fitted1, color='#1f77b4')
plt.plot(x2, pdf_fitted2, color='#ff7f0e')
plt.show()
I'd be glad if someone could help with that, thanks in advance. Any answer or comment would be appreciated.