I have implemented a function in python to compute the autocorrelation of a time series at a specific lag k. It is implemented under the assumption that some of the time series might not be stationary. However I'm finding that for some of these I am getting values greater than 1, specially on the last lags. So I guess I must be getting wrong some calculation.
I am implementing the following:
Where for the terms corresponding to the lagged series I'm computing the mean and standart deviation from lag k onwards.
I have implemented the following code in python, which computes the autocorrelation for a specific lag k:
def custom_autocorrelation(x, lag = 12):
n = len(x)
std = x.std()
mu = x.mean()
autocov = 0
mu_lag = x[lag:].mean()
std_lag = x[lag:].std()
for j in range(n-lag):
autocov += (x[j] - mu)*(x[j+lag] - mu_lag)
autocorr = autocov/(std*std_lag*(n-lag))
return autocorr
As an example I'm trying with the following sequence, for k = 12, an getting a coefficient of 1.03:
np.array([20623., 11041., 5686., 2167., 2375., 2057., 3141., 504.,
152., 6562., 8199., 15103., 16632., 7190., 6987., 2652.,
1949., 2223., 1703., 2163., 1850., 6932., 5932., 13124.,
14846., 7850., 4526., 1277., 1036., 1500., 1648., 1384.,
1446., 3477., 6818., 12446., 9734.])
Any help would be very appreciated!