LAB 1: Welcome to Advanced Time Series Analysis - 1
Solution 1: Welcome to Advanced Time Series Analysis - 1
#Task 1: 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
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)
#Task 2: 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.
from matplotlib import pyplot
from statsmodels.tsa.stattools import acf, pacf
acf_corr = acf(ts, fft=True ,nlags=5, unbiased=True )
print(acf_corr)
pacf_corr = statsmodels.tsa.stattools.pacf(ts, nlags=5)
print(pacf_corr)
#Task 3: 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.
pacf_corr = statsmodels.tsa.stattools.pacf(ts, nlags=5)
print(pacf_corr)
#Task4:
# 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
acf_lag_1 = [ round(i,2) for i in statsmodels.tsa.stattools.pacf(ts, nlags=1)]
pacf_lag_3 = [ round(i,2) for i in statsmodels.tsa.stattools.pacf(ts, nlags=3)]
acf_lag_1 = round( acf_corr[1],2)
pacf_lag_3 = round(pacf_corr[3],2)
LAB 2: Welcome to Advanced Time Series Analysis - 2
Solution 2: Welcome to Advanced Time Series Analysis - 2
# Task1: Load Data
# In this hands-on you will build an auto regressive model on train set and forecast on the test set
# Run the below cell to load the data and split it to train and test set
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]
train, test = timeSeries[1:len(timeSeries)-10], timeSeries[len(timeSeries)-10:]
# Task 2: Build Auto Regressive Model
# Import AR from statsmodels
# Initialize the model with train data and assign it to variable model .
# Fit the model and return the result to variable model_fit.
from statsmodels.tsa.ar_model import AR
model = AR(train)
model_fit = model.fit()
print('Lag: %s' % model_fit.k_ar)
print('Coefficients: %s' % model_fit.params)
# Task 3: Prediction
# Using model_fit forecast (predict) the values by using start and end index of test data
# (follow the code snippet provided in the course) and assign the forecasted values to variable predictions.
from sklearn.metrics import mean_squared_error
predictions = model_fit.predict(start=len(train), end=len(train)+len(test)-1, dynamic=False)
# Print values
for i in range(len(predictions)):
print('predicted=%f, expected=%f' % (predictions[i], test[i]))
error = mean_squared_error(test, predictions)
print('Test MSE: %.3f' % error)