Skip to content

Instantly share code, notes, and snippets.

@ksv-muralidhar
Last active August 4, 2021 11:35
Show Gist options
  • Save ksv-muralidhar/fbbcaf7515294b3c16ce64328c0dc24b to your computer and use it in GitHub Desktop.
Save ksv-muralidhar/fbbcaf7515294b3c16ce64328c0dc24b to your computer and use it in GitHub Desktop.
tsa
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
nifty = pd.read_csv("Nifty.csv",
usecols=['Date', "Close"],
parse_dates=['Date'])
nifty.set_index("Date", inplace=True) #setting "Date" as index
nifty = nifty.asfreq("b").copy() #setting frequency to business days
nifty['Close'] = nifty['Close'].fillna(method="ffill") #forward filling missing values (due to holidays in India)
nifty.plot(figsize=(25,5))
plt.show()
def my_auto_corr(df, nlags=2):
'''
Returns autocorrelation coefficient for lags [nlags, 0]
df: dataframe
Input dataframe of time series
nlags: int
maximum number of lags, default 2
Returns
array: autocorrelation coefficients for lags [nlags, 0]
'''
def autocorr(y, lag=2):
'''
Calculates autocorrelation coefficient for single lag value
y: array
Input time series array
lag: int, default: 2
'kth' lag value
Returns
int: autocorrelation coefficient
'''
y = np.array(y).copy()
y_bar = np.mean(y) #y_bar = mean of the time series y
denominator = sum((y - y_bar) ** 2) #sum of squared differences between y(t) and y_bar
numerator_p1 = y[lag:] - y_bar #y(t)-y_bar: difference between time series (from 'lag' till the end) and y_bar
numerator_p2 = y[:-lag] - y_bar #y(t-k)-y_bar: difference between time series (from the start till lag) and y_bar
numerator = sum(numerator_p1 * numerator_p2) #sum of y(t)-y_bar and y(t-k)-y_bar
return (numerator / denominator)
acf = [1] #initializing list with autocorrelation coefficient for lag k=0 which is always 1
for i in range(1, (nlags + 1)):
acf.append(autocorr(df.iloc[:, 0].values, lag=i)) #calling autocorr function for each lag 'i'
return np.array(acf)
from statsmodels.graphics import tsaplots
print(f'my_auto_corr:\n{my_auto_corr(df=nifty, nlags=10)}\n\nstatsmodels acf:\n{tsaplots.acf(nifty.iloc[:,0].values,nlags=10)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment