What is the difference between this code and this formula?

211 views Asked by At

I am writing an algorithm and calculating the kurtosis of the distribution of daily returns. I am trying to get my calculation of kurtosis to match that of Excel's. Excel's calculation supposedly uses the formula at the top of this webpage: http://www.macroption.com/kurtosis-excel-kurt/

Here is my code used to emulate that formula (returns is a numpy array consisting of the series of daily returns):

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 

Apparently there is a difference between the formula used by excel and my code... Excel gives a kurtosis of 1.94, while my code gives a value of 2.81.

Does anyone have a clue as to why the two values are different?

1

There are 1 answers

0
hpaulj On BEST ANSWER

Rewriting my comment:

Providing a ddof=1 parameter to np.std() changes its calculation from population to sample (n-1). Usually the change in std is small, but with the s**4 use, small changes in s will be amplified.