我使用Python中scikit学习软件包的Kernel Density Estimator对修复成本的分布进行建模。我已经根据我的观察创建了密度函数,但是当从该分布中随机抽取样本时,会出现负值。由于观察结果总是积极的成本,因此样本值应该是非负的。
我已经读过,通过转换数据可以达到这个结果。这些源使用日志转换将分布截断为0(Log-transform kernel density estimation of income distribution,Kernel Density Estimation for Random Variables with Bounded Support — The Transformation Trick)。问题是我不知道如何结合scikit学习Kernal密度函数使用我的观察结果的对数转换。
没有转换的KDE代码如下:
import numpy as np
from sklearn.neighbors import KernelDensity
import math as math
'Dataframe with costs'
x = costs
maxVal = x.max()
minVal = x.min()
upperBound = math.ceil(maxVal/1000)*1000
x_grid = np.linspace(0, upperBound, 1000)
'Create pdf with Kernel Density'
kde = KernelDensity(kernel='gaussian', bandwidth=612).fit(x_grid[:, np.newaxis])
log_pdf = kde.score_samples(x_grid[:, np.newaxis])
pdf=np.exp(log_pdf)
我的代码包括转换:
'Log tranformation and creation of pdf'
x_pseudo = x.apply(np.log)
kde_psuedo = KernelDensity(kernel='gaussian', bandwidth=612).fit(x_pseudo[:, np.newaxis])
log_pdf_pseudo = kde_psuedo.score_samples(x_pseudo[:, np.newaxis])
pdf_pseudo=np.exp(log_pdf_pseudo)
x_grid_log = np.linspace(minVal, maxVal, 1000)
density = np.zeros(len(x_grid_log))
for i in range(len(x_grid_log)):
xx=x_grid_log[i]
density[i]=pdf_pseudo[xx.apply(np.log)/xx]
output = list(x=x_grid_log, y=density)
此代码基于源代码2中的示例,该代码在R中生成。我知道代码错误,但我不知道如何解决此问题。任何帮助将不胜感激!