Fitting an exponential modified gaussian curve to data with Python
Question: I have a data set and a kernel density estimate for those data. I believe the
KDE should be reasonably well described by an [exponentinally modified
Gaussian](https://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution),
so I'm trying to sample from the KDE and fit those samples with a function of
that type. However, when I try to fit using scipy.optimize.curve_fit, my fit
doesn't match the data well at all. My code is

    
    
    import scipy.special as sse
    from scipy.optimize import curve_fit
    
    def fit_func(x, l, s, m):
        return 0.5*l*n.exp(0.5*l*(2*m+l*s*s-2*x))*sse.erfc((m+l*s*s-x)/(n.sqrt(2)*s)) # exponential gaussian
    
    popt, pcov = curve_fit(fit_func, n.linspace(0,1,100), data)
    

My "data set" (from sampling my KDE) is

    
    
    data = [1.00733940e-09, 1.36882036e-08, 1.44555907e-07, 1.18647634e-06, 7.56926695e-06, 3.75417381e-05, 1.44836578e-04, 4.35259159e-04, 1.02249858e-03, 1.89480681e-03, 2.83377851e-03, 3.60624100e-03, 4.30392052e-03, 5.33527267e-03, 6.95313891e-03, 8.89175932e-03, 1.05631739e-02, 1.15411608e-02, 1.18087942e-02, 1.16473841e-02, 1.14907524e-02, 1.20296850e-02, 1.42949235e-02, 1.90939074e-02, 2.59260288e-02, 3.27250866e-02, 3.73294844e-02, 3.92476016e-02, 3.94803903e-02, 3.88736022e-02, 3.76397612e-02, 3.65042464e-02, 3.72842810e-02, 4.19404962e-02, 5.12185577e-02, 6.39393269e-02, 7.75139966e-02, 8.97085567e-02, 1.00200355e-01, 1.10354564e-01, 1.22123289e-01, 1.37876215e-01, 1.60232917e-01, 1.90218800e-01, 2.25749072e-01, 2.63342328e-01, 3.01468733e-01, 3.41685959e-01, 3.86769102e-01, 4.38219405e-01, 4.95491603e-01, 5.56936603e-01, 6.20721893e-01, 6.85160043e-01, 7.49797233e-01, 8.17175672e-01, 8.92232359e-01, 9.78276608e-01, 1.07437591e+00, 1.17877517e+00, 1.29376679e+00, 1.42302331e+00, 1.56366767e+00, 1.70593547e+00, 1.84278471e+00, 1.97546304e+00, 2.10659735e+00, 2.23148403e+00, 2.34113950e+00, 2.43414110e+00, 2.52261228e+00, 2.62487277e+00, 2.75168928e+00, 2.89831664e+00, 3.04838614e+00, 3.18625230e+00, 3.30842825e+00, 3.42373645e+00, 3.53943425e+00, 3.64686003e+00, 3.72464478e+00, 3.75656044e+00, 3.74189870e+00, 3.68666210e+00, 3.58686497e+00, 3.42241586e+00, 3.16910593e+00, 2.81976459e+00, 2.39676519e+00, 1.94507169e+00, 1.51241642e+00, 1.13287316e+00, 8.22421330e-01, 5.82858108e-01, 4.07338019e-01, 2.84100125e-01, 1.98750792e-01, 1.37317714e-01, 9.01427225e-02, 5.35761233e-02]
    

and here is my histogram of the real data, the KDE in red, and my attempt at
fitting the KDE in black -

[![histogram with kernel density estimate and my fit
overplot](http://i.stack.imgur.com/5lR8w.png)](http://i.stack.imgur.com/5lR8w.png)


Answer: The exponentially modified Gaussian is defined to be a skewed distribution to
the left and as such, the shape parameter does not change the direction of
this skew.

This is what I have tried.

    
    
        data.reverse()
        popt,pcov=(curve_fit(fit_func, n.linspace(0,1,100), data))
        fitted_curve=list(fit_func(n.linspace(0,1,100),popt[0],popt[1],popt[2]))
        data.reverse()
        fitted_curve.reverse()
    

[Plot of the data and the fitted curve](http://i.stack.imgur.com/JiVou.png)

