SiLab-Bonn / pylandau

Simple Landau distribution definitions to be used in Python.
GNU Lesser General Public License v2.1
20 stars 4 forks source link

How to fit langaus to data in Pandas? #16

Closed adendek closed 2 years ago

adendek commented 5 years ago

I am trying to fit the langaus to data that is stored as a pandas series. Here is how my data looks like: image

And, here is the function, that I created in order to perform langaus fitting:

def fit_landau(data, mpv, eta, sigma):
    from scipy.optimize import curve_fit
    import pylandau
    bin_heights, bin_borders, _ = plt.hist(data, bins="auto", label='histogram')
    bin_centers = bin_borders[:-1] + np.diff(bin_borders) / 2
    popt, _ = curve_fit(pylandau.langau, bin_centers, bin_heights,
                        absolute_sigma=True,
                        p0=(mpv, eta, sigma),
                        method='trf',
                        bounds=(1, 10000),
                        verbose=2
                       )
    print(popt)
    x_interval_for_fit = np.linspace(bin_borders[0], bin_borders[-1], 10000)
    plt.plot(x_interval_for_fit, pylandau.landau(x_interval_for_fit, *popt), label='fit')
    plt.legend()

I am not able to find good starting values, that leads to coverage. Can you help me to fix the fitting procedure?

DavidLP commented 2 years ago

Please take a look at the examples for fitting. E.g.: https://github.com/SiLab-Bonn/pylandau/blob/master/examples/fit.py You did not specify an amplitude A, that is rather large for your data points.

adendek commented 2 years ago

Hello @DavidLP, thank you for your answer. I posted this question almost two years ago and I forget to close this issue. Back then, I solve this problem by using pure ROOT instead of scipy.

DavidLP commented 2 years ago

Yes, was a long time ago. Still you got the answer ;-). I close this