【发布时间】:2017-09-05 16:34:27
【问题描述】:
我目前正在通过this SciPy example 处理内核估计。特别是标记为“单变量估计”的那个。与创建随机数据相反,我使用的是资产回报。不过,我的第二次估计(甚至是我创建的简单规范 pdf 进行比较)显示的密度峰值为 20,这没有任何意义......我的代码如下:
x1 = np.array(data['actual'].values)[1:]
xs1 = np.linspace(x1.min()-1,x1.max()+1,len(x1))
std1 = x1.std()
mean1 = x1.mean()
x2 = np.array(data['log_moves'].values)[1:]
xs2 = np.linspace(x2.min()-.01,x2.max()+.01,len(x2))
#xs2 = np.linspace(x2.min()-1,x2.max()+2,len(x2))
std2 = x2.std()
mean2 = x2.mean()
kde1 = stats.gaussian_kde(x1) # actuals
kde2 = stats.gaussian_kde(x1, bw_method='silverman')
kde3 = stats.gaussian_kde(x2) # log returns
kde4 = stats.gaussian_kde(x2, bw_method='silverman')
fig = plt.figure(figsize=(10,8))
ax1 = fig.add_subplot(211)
ax1.plot(x1, np.zeros(x1.shape), 'b+', ms=12) # rug plot
ax1.plot(xs1, kde1(xs1), 'k-', label="Scott's Rule")
ax1.plot(xs1, kde2(xs1), 'b-', label="Silverman's Rule")
ax1.plot(xs1, stats.norm.pdf(xs1,mean1,std1), 'r--', label="Normal PDF")
ax1.set_xlabel('x')
ax1.set_ylabel('Density')
ax1.set_title("Absolute (top) and Returns (bottom) distributions")
ax1.legend(loc=1)
ax2 = fig.add_subplot(212)
ax2.plot(x2, np.zeros(x2.shape), 'b+', ms=12) # rug plot
ax2.plot(xs2, kde3(xs2), 'k-', label="Scott's Rule")
ax2.plot(xs2, kde4(xs2), 'b-', label="Silverman's Rule")
ax2.plot(xs2, stats.norm.pdf(xs2,mean2,std2), 'r--', label="Normal PDF")
ax2.set_xlabel('x')
ax2.set_ylabel('Density')
plt.show()
作为参考,第一时刻和第二时刻的数据:
print std1
print mean1
print std2
print mean2
4.66416718334
0.0561365678347
0.0219996729055
0.00027330546845
此外,如果我更改第二个图表以生成对数正态 PDF,我会得到一条平线(如果 Y 轴像顶部一样正确缩放,我肯定会显示出我期望的分布)
【问题讨论】: