这段代码和这个公式有什么区别?
What is the difference between this code and this formula?
我正在编写算法并计算每日 returns 分布的峰度。我试图让我的峰态计算与 Excel 的峰态相匹配。 Excel 的计算据推测使用了此网页顶部的公式:http://www.macroption.com/kurtosis-excel-kurt/
这是我用来模拟该公式的代码(returns 是一个由每日 returns 系列组成的 numpy 数组):
def kurtosis(returns):
n = len(returns)
avg = np.average(returns)
std = np.std(returns)
coefficient = 1.0 * n * (n+1) / ((n-1) * (n-2) * (n-3) * std**4.0)
term = (3 * (n-1)**2.0) / ((n-2) * (n-3))
summation = 0
for x in returns:
summation += ( (x - avg) ) ** 4.0
kurt = coefficient * summation - term
return kurt
显然 excel 和我的代码使用的公式之间存在差异...Excel 给出的峰度为 1.94,而我的代码给出的值为 2.81。
有人知道为什么这两个值不同吗?
重写我的评论:
向 np.std()
提供 ddof=1
参数会将其计算从总体更改为样本 (n-1)。通常std
的变化很小,但是随着s**4
的使用,s
的微小变化会被放大。
我正在编写算法并计算每日 returns 分布的峰度。我试图让我的峰态计算与 Excel 的峰态相匹配。 Excel 的计算据推测使用了此网页顶部的公式:http://www.macroption.com/kurtosis-excel-kurt/
这是我用来模拟该公式的代码(returns 是一个由每日 returns 系列组成的 numpy 数组):
def kurtosis(returns):
n = len(returns)
avg = np.average(returns)
std = np.std(returns)
coefficient = 1.0 * n * (n+1) / ((n-1) * (n-2) * (n-3) * std**4.0)
term = (3 * (n-1)**2.0) / ((n-2) * (n-3))
summation = 0
for x in returns:
summation += ( (x - avg) ) ** 4.0
kurt = coefficient * summation - term
return kurt
显然 excel 和我的代码使用的公式之间存在差异...Excel 给出的峰度为 1.94,而我的代码给出的值为 2.81。
有人知道为什么这两个值不同吗?
重写我的评论:
向 np.std()
提供 ddof=1
参数会将其计算从总体更改为样本 (n-1)。通常std
的变化很小,但是随着s**4
的使用,s
的微小变化会被放大。