使用Python将指数修正的高斯曲线拟合到数据 [英] Fitting an exponential modified gaussian curve to data with Python

查看:473
本文介绍了使用Python将指数修正的高斯曲线拟合到数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集和这些数据的内核密度估计.我相信经指数修改的高斯应该可以很好地描述KDE.从KDE进行采样,并使用该类型的函数拟合这些采样.但是,当我尝试使用scipy.optimize.curve_fit拟合时,我的拟合度与数据完全不匹配.我的代码是

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

我的数据集"(通过采样我的KDE)是

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]

这是我的真实数据直方图,红色的KDE,以及我尝试用黑色拟合KDE的尝试-

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

推荐答案

经过指数修改的高斯定义为左侧的偏斜分布,因此shape参数不会更改该偏斜的方向.

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.

这是我尝试过的.

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

数据图和拟合曲线

这篇关于使用Python将指数修正的高斯曲线拟合到数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆