In general scipy.optimize.curve_fit
works once we know the equation that best fits our dataset. Since you want to fit a dataset that follows a Gaussian, Lorentz etc, distributions, you can do so by providing their specific equations.
Just as a small example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
xdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])
ydata = np.array([0.69,0.70,0.69,1.0,1.9,2.4,1.9,0.9,-0.7,-1.4])
def func(x, p1,p2):
return p1*np.cos(p2*x) + p2*np.sin(p1*x)
# Here you give the initial parameters for p0 which Python then iterates over
# to find the best fit
popt, pcov = curve_fit(func,xdata,ydata,p0=(1.0,0.3))
print(popt) # This contains your two best fit parameters
# Performing sum of squares
p1 = popt[0]
p2 = popt[1]
residuals = ydata - func(xdata,p1,p2)
fres = sum(residuals**2)
print(fres)
xaxis = np.linspace(-2,3,100) # we can plot with xdata, but fit will not look good
curve_y = func(xaxis,p1,p2)
plt.plot(xdata,ydata,'*')
plt.plot(xaxis,curve_y,'-')
plt.show()
The above was for my specific case, wherein I just used a Harmonic addition formula that fit's my dataset. You can change accordingly, either a Gaussian equation, or any other equation, by providing it in the func
definition.
Your parameters will vary accordingly. If it is a Gaussian distribution, you will have your sigma
(standard deviation) and mean
as the unknown parameters.