What's the difference between pandas ACF and statsmodel ACF?

前端 未结 3 2057
栀梦
栀梦 2021-01-30 16:55

I\'m calculating the Autocorrelation Function for a stock\'s returns. To do so I tested two functions, the autocorr function built into Pandas, and the acf

3条回答
  •  萌比男神i
    2021-01-30 17:55

    As suggested in comments, the problem can be decreased, but not completely resolved, by supplying unbiased=True to the statsmodels function. Using a random input:

    import statistics
    
    import numpy as np
    import pandas as pd
    from statsmodels.tsa.stattools import acf
    
    DATA_LEN = 100
    N_TESTS = 100
    N_LAGS = 32
    
    def test(unbiased):
      data = pd.Series(np.random.random(DATA_LEN))
      data_acf_1 = acf(data, unbiased=unbiased, nlags=N_LAGS)
      data_acf_2 = [data.autocorr(i) for i in range(N_LAGS+1)]
      # return difference between results
      return sum(abs(data_acf_1 - data_acf_2))
    
    for value in (False, True):
      diffs = [test(value) for _ in range(N_TESTS)]
      print(value, statistics.mean(diffs))
    

    Output:

    False 0.464562410987
    True 0.0820847168593
    

提交回复
热议问题