gpt4 book ai didi

python - 使用 Python 将指数修正高斯曲线拟合到数据

转载 作者:行者123 更新时间:2023-11-28 17:28:09 24 4
gpt4 key购买 nike

我有一个数据集和这些数据的核密度估计。我相信 KDE 应该由 exponentinally modified Gaussian 合理地很好地描述,所以我试图从 KDE 中抽样,并用那种类型的函数来拟合这些样本。但是,当我尝试使用 scipy.optimize.curve_fit 进行拟合时,我的拟合与数据完全不匹配。我的代码是

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 的采样)是

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 -

histogram with kernel density estimate and my fit overplot

最佳答案

指数修正高斯分布被定义为向左偏斜分布,因此形状参数不会改变这种偏斜的方向。

这是我试过的。

    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

关于python - 使用 Python 将指数修正高斯曲线拟合到数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36900920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com