我有这些数据的数据集和内核密度估计值。我相信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 -
答案 0 :(得分:1)
指数修改的高斯被定义为左边的偏斜分布,因此,形状参数不会改变该偏斜的方向。
这是我尝试过的。
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()
答案 1 :(得分:0)
您可以通过重新定义x来改变尾巴的方向:
exponnorm.pdf(x, K, mu, sigma)
可以改写为
exponnorm.pdf((x-mu)/sigma, K)
如果要将其反转以使尾巴在左侧,请使用
exponnorm.pdf(-(x-mu)/sigma, K)