4/5/22, 3:05 PM AdvanceTS1handson - Jupyter Notebook
Welcome to the first hands on advanced time series analysis.
Follow the instruction provided for each cell to complete the handson. We have created this Python
Notebook with all the necessary things needed for completing this exercise.
To run the code in each cell click on the cell and press shift + enter
Load the data
In this hands-on you will be finding the auto correlation and partial auto correlation on an array
of numbers.
Run the below cell to load the data and covert it to series object
In [1]: import pandas as pd
from pandas import Series
timeSeries = [30,21,29,31,40,48,53,47,37,39,31,29,17,9,20,24,27,35,41,38,
27,31,27,26,21,13,21,18,33,35,40,36,22,24,21,20,17,14,17,19,
26,29,40,31,20,24,18,26,17,9,17,21,28,32,46,33,23,28,22,27,
18,8,17,21,31,34,44,38,31,30,26,32]
ts = Series(timeSeries)
Auto correlation
import acf from stats model
Determine the acf values upto lag 5, use ts as timeseries data, set unbiased = True and
assign to variable acf_corr .
In [2]: from statsmodels.tsa.stattools import acf
acf_corr = acf(ts, unbiased=True, nlags=5)
print(acf_corr)
[ 1. 0.72120588 0.42688471 0.09746397 -0.18409287 -0.31332648]
/home/user/.local/lib/python3.5/site-packages/statsmodels/tsa/stattools.py:572:
FutureWarning: fft=True will become the default in a future version of statsmod
els. To suppress this warning, explicitly set fft=False.
FutureWarning
Partial auto correlation
import pacf from stats model
Determine the pacf values upto lag 5, use ts as timeseries data and assign it to variable
pacf_corr .
https://serverco4rg9vb-ws-dev-server-8000.in-dc-5.projects.hackerrank.net/notebooks/AdvanceTS1handson.ipynb 1/3
4/5/22, 3:05 PM AdvanceTS1handson - Jupyter Notebook
In [3]: from statsmodels.tsa.stattools import pacf
pacf_corr = pacf(ts, nlags=5)
print(pacf_corr)
[ 1. 0.72120588 -0.19433338 -0.28172395 -0.18857053 0.02711725]
Questions
what is the auto correlation for lag 1?, assign this rounded off to 2-decimal value to variable
acf_lag_1
what is the partial auto correlation for lag 3?, assign this rounded off to 2-decimal value to
variable pacf_lag_3
In [7]: acf_lag_1 = round(0.72120588,2)
pacf_lag_3 = round(-0.28172395,2)
print(acf_lag_1)
print(pacf_lag_3)
0.72
-0.28
Run the below cell without modifying to save your answers
In [8]: import hashlib
import pickle
def gethex(ovalue):
hexresult=hashlib.md5(str(ovalue).encode())
return hexresult.hexdigest()
def pickle_ans1(value):
hexresult=gethex(value)
with open('ans/output1.pkl', 'wb') as file:
hexresult=gethex(value)
print(hexresult)
pickle.dump(hexresult,file)
def pickle_ans2(value):
hexresult=gethex(value)
with open('ans/output2.pkl', 'wb') as file:
hexresult=gethex(value)
print(hexresult)
pickle.dump(hexresult,file)
pickle_ans1(acf_lag_1)
pickle_ans2(pacf_lag_3)
18f154fce2b7eef7981dd860d1fa6dc0
34f45f03be2059ba3f824e7ddda60505
In [ ]:
https://serverco4rg9vb-ws-dev-server-8000.in-dc-5.projects.hackerrank.net/notebooks/AdvanceTS1handson.ipynb 2/3
4/5/22, 3:05 PM AdvanceTS1handson - Jupyter Notebook
https://serverco4rg9vb-ws-dev-server-8000.in-dc-5.projects.hackerrank.net/notebooks/AdvanceTS1handson.ipynb 3/3