Python - Trying to calculate ACF by hand and comparing with acf from statsmodels

31 Views Asked by At

I am getting different results when calculating ACF using my own code in 2 versions and acf from statsmodels.

The code is given by the following function where x is the series and lag is the chosen lag to calculate for

def acf_by_hand(x, lag):
    # Slice the relevant subseries based on the lag
    y1 = x[:(len(x)-lag)]
    y2 = x[lag:]
    # Subtract the mean of the whole series x to calculate Cov
    sum_product = np.sum((y1-np.mean(x))*(y2-np.mean(x)))
    # Normalize with var of whole series
    return sum_product / ((len(x) - lag) * np.var(x))

The difference is not that radical, but i was expecting it to match each other. Any toughts?

0

There are 0 best solutions below