Time Series Analysis and Forecasting
Time Series Analysis and Forecasting
Fall - 2024
1
These course slides should not be reproduced nor used by others (without
permission).
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 1 / 271
Lecture Outline I
1 Chapter 1: Introduction
4.3 Correlogram
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 7 / 271
Syllabus
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 8 / 271
Text Book & References List
Text Book
Chapter 1: Introduction
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 10 / 271
Chapter 1: Introduction
1 Chapter 1: Introduction
data
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 12 / 271
Chapter 1: Introduction What is a Time Series?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 13 / 271
Chapter 1: Introduction What is a Time Series?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 14 / 271
Chapter 1: Introduction What is a Time Series?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 15 / 271
Chapter 1: Introduction What is a Time Series?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 17 / 271
Chapter 1: Introduction Time series data vs. Cross-sectional data
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 18 / 271
Chapter 1: Introduction Pooled Data vs Longitudinal Data
. Pooled (or panel) data is where the two are combined together. i.e. a
salary dataset can contain observations collected at one point in time,
as well as across dierent time periods
. Longitudinal data is a set of observations on dierent characteristics
of the single observational unit collected at dierent times e.g.
observations on a set of nancial ratios for a single company over a
10-year period
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 19 / 271
Chapter 1: Introduction Component of Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 20 / 271
Chapter 1: Introduction Component of Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 21 / 271
Chapter 1: Introduction Component of Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 22 / 271
Chapter 1: Introduction Component of Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 23 / 271
Chapter 1: Introduction Mathematical Models for Time Series
yt = Tt + Ct + St + It (1)
yt = Tt × Ct × St × It (2)
3 mixed model:
yt = Tt Ct + St It ⎫
⎪
⎪
⎪
yt = Tt + Ct St It ⎬ (3)
⎪
⎪
⎪
yt = Tt + St + Ct It ⎭
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 24 / 271
Chapter 1: Introduction Mathematical Models for Time Series
the model (1) or (2) can be use to obtain a measure of one or more
of the components by elimination, viz., subtraction or division
yt original values
St × Ct × It = =
Tt trend values
thus, for the annual data, for which the seasonal component St is not
here, we have
yt
yt = Tt × Ct × It ⇒ Ct × It =
Tt
remarks: an additive model would be used when the variations around the
trend do not vary with the level of the time series whereas a multiplicative
model would be appropriate if the trend is proportional to the level of the
time series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 25 / 271
Chapter 1: Introduction Mathematical Models for Time Series
3 additive model is used when the variance in the graph doesn't change
over dierent values of the time series.
4 if the variance is higher when the time series is higher then it often
means we should use a multiplicative models.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 26 / 271
Chapter 1: Introduction Mathematical Models for Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 27 / 271
Chapter 1: Introduction Mathematical Models for Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 28 / 271
Chapter 1: Introduction Mathematical Models for Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 29 / 271
Chapter 1: Introduction Mathematical Models for Time Series
1 the rst thing to do in any data analysis task is to plot the data
Data Frequency
Annual 1
Quarterly 4
Monthly 12
Weekly 52
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 30 / 271
Chapter 1: Introduction Time Series plot in Python
Getting Python!
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 31 / 271
Chapter 1: Introduction Time Series plot in Python
import pandas as pd
df = pd.read_csv("AirPassengers.csv")
print(df.head(14))
# print(df.tail())
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 32 / 271
Chapter 1: Introduction Time Series plot in Python
df.index = df['Month']
del df['Month']
print(df.head())
next, let's generate a time series plot using Seaborn and Matplotlib.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 33 / 271
Chapter 1: Introduction Time Series plot in Python
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 34 / 271
Chapter 1: Introduction Time Series plot in Python
the decomposition
import statsmodels.api as sm
result = sm.tsa.seasonal_decompose(df['Passengers'],
model='multiplicative')
result.plot()
result.trend.plot()
result.seasonal.plot()
result.plot()
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 35 / 271
Chapter 1: Introduction Time Series plot in Python
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 36 / 271
Chapter 1: Introduction Time Series plot in Python
Decomposition in Python
In Python, the sm.tsa.seasonal_decompose function from the
statsmodels library is commonly used for decomposing time series data.
Trend Estimation:
▸ The trend component is estimated using a moving average method.
▸ The default window size for the moving average is based on the
frequency of the time series. For instance, if the data is monthly, the
default window size will typically be 12.
Seasonal Estimation:
▸ The seasonal component is computed as the average of the detrended
data for each season (e.g., each month).
▸ This calculation considers the periodicity of the data, allowing it to
capture repeating patterns eectively.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 37 / 271
Chapter 1: Introduction Time Series plot in Python
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 38 / 271
Chapter 1: Introduction Time Series plot in Python
Flights dataset
To plot a single vector, pass it to data. If the vector is a
pandas.Series, it will be plotted against its index:
sns.lineplot(data=flights_wide["May"])
Passing the entire wide-form dataset to data plots a separate line for
each column:
sns.lineplot(data=flights_wide)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 39 / 271
Chapter 1: Introduction Time Series plot in R
Getting started R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 40 / 271
Chapter 1: Introduction Time Series plot in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 41 / 271
Chapter 1: Introduction Time Series plot in R
Alternatively,
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 42 / 271
Chapter 1: Introduction Time Series plot in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 43 / 271
Chapter 1: Introduction R Packages for Time Series Analysis
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 44 / 271
Chapter 1: Introduction R Packages for Time Series Analysis
Data: AirPassengers
▸▸Data AirPassengers: monthly totals of Box Jenkins international
airline passengers, 1949 to 1960. It has 144(=12 Ö12) values
## load time series data
plot(AirPassengers)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 45 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 46 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 47 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 48 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 49 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 50 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 51 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 52 / 271
Chapter 1: Introduction Time Series Decomposition in R
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 53 / 271
Chapter 1: Introduction Time Series Forecasting
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 54 / 271
Chapter 1: Introduction Time Series Forecasting
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 55 / 271
Chapter 1: Introduction Time Series Forecasting
SAS
Python
Eviews
STRATA
Minitab
SPSS
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 56 / 271
Chapter 1: Introduction Starting in Eviews
Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 57 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 58 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 59 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 60 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 61 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 62 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 63 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 64 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 65 / 271
Chapter 1: Introduction Starting in Eviews
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 66 / 271
Chapter 1: Introduction Starting in Eviews
Homework
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 67 / 271
Chapter 2: Trend Analysis
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 68 / 271
Chapter 2: Trend Analysis
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 69 / 271
Chapter 2: Trend Analysis A General Approach to Time Series Modeling
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 70 / 271
Chapter 2: Trend Analysis A General Approach to Time Series Modeling
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 71 / 271
Chapter 2: Trend Analysis A General Approach to Time Series Modeling
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 72 / 271
Chapter 2: Trend Analysis A General Approach to Time Series Modeling
yt = Tt + εt
how to estimate Tt ?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 73 / 271
Chapter 2: Trend Analysis A General Approach to Time Series Modeling
yt = Tt + St + εt
where St is a function of seasonal component
how to estimate Tt and St ?
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 74 / 271
Chapter 2: Trend Analysis Trends in Time Series
types of trends
1 Deterministic Trends: these are trends that consistently increase or
decrease
2 Stochastic Trends: these are trends that increase and decrease
inconsistently
for each type of trends, we can get
1 Global Trends: these are trends that apply to the whole time series
2 Local Trends: these are trends that apply to parts or subsequences of
a time series
generally, global trends are easier to identify and address
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 75 / 271
Chapter 2: Trend Analysis Trends in Time Series
identifying a trend
▸ you can plot time series data to see if a trend is obvious or not
▸ the diculty is that in practice, identifying a trend in a time series can
be a subjective process
▸ create line plots of your data and inspect the plots for obvious trends
▸ add linear and nonlinear trend lines to your plots and see if a trend is
obvious
removing a trend
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 76 / 271
Chapter 2: Trend Analysis Trends in Time Series
Measurement of Trend
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 77 / 271
Chapter 2: Trend Analysis Graphic (or free-hand curve tting) method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 78 / 271
Chapter 2: Trend Analysis Graphic (or free-hand curve tting) method
it gives us an idea about the rise and fall of the time series
for every long time series, the graph of the original data enables us to
decide on the application of more mathematical models for the
measurement of a trend
disadvantages
this method is not mathematical in nature, so dierent people may
draw a dierent trend
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 79 / 271
Chapter 2: Trend Analysis Method of semi-averages
Method of semi-averages
the data is divided into two equal parts. In case of odd number of
data, two equal parts can be made simply by omitting the middle year
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 80 / 271
Chapter 2: Trend Analysis Method of semi-averages
Example
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 81 / 271
Chapter 2: Trend Analysis Method of semi-averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 82 / 271
Chapter 2: Trend Analysis Method of semi-averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 83 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 84 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Example:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 85 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
∂Q ∂ n 2
= ∑ (yt − β0 − β1 t) = 0 (4)
∂β0 ∂β0 t=1
and
∂Q ∂ n 2
= ∑(yt − β0 − β1 t) = 0 (5)
∂β1 ∂β1 i=1
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 86 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
LS Estimator
from (4), we have
n
∑(yt − β0 − β1 t)(−1) = 0
i= 1
n n
⇒ ∑ yt − nβ0 − β1 ∑ t = 0
i= 1 i= 1
or
β0 = ȳ − β1 t̄
from (5), we have
n
∑(yt − β0 − β1 t)(−t) = 0
i= 1
n n n
⇒ ∑ tyt − β0 ∑ t − β1 ∑ t 2 = 0 (6)
i= 1 i= 1 1
i=
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 87 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
n n n
2
∑ tyt − (ȳ − β1 t̄) ∑ t − β1 ∑ t = 0
t= 1 t= 1 t= 1
n n n n
⇒ ∑ tyt − ȳ ∑ t + β1 t̄ ∑ t − β1 ∑ t 2
t= 1 1
t= t= 1 1
t=
n n
⇒ ∑ tyt − nȳ t̄ + nβ1 t̄ 2 − β1 ∑ t 2 = 0
t= 1 t= 1
n n
⇒ ∑ tyt − nȳ t̄ − β1 ( ∑ t 2 − nt̄ 2 ) = 0
t= 1 t= 1
n
∑ tyi − nt̄ ȳ
⇒β1 =
t=1
∑ t 2 − nt̄ 2
n
t= 1
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 88 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
β̂0 = ȳ − β̂1 t̄
n
∑ tyi − nt̄ ȳ
β̂1 =
t=1
∑ t 2 − nt̄ 2
n
t= 1
the expression of β̂1 can also be written as
n
∑ (t − t̄)(yt − ȳ )
β̂1 =
t= 1
∑ (t − t̄)2
n
t= 1
because
n n n n
2 2
∑ (t − t̄) = ∑ t − nt̄
2 and ∑ (t − t̄)(yt − ȳ ) = ∑ tyi − nt̄ ȳ
t= 1 t= 1 t= 1 t=1
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 89 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Example:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 90 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 91 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 92 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 93 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 94 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 95 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 96 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 97 / 271
Chapter 2: Trend Analysis Estimating a trend by least squares
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 98 / 271
Chapter 2: Trend Analysis Method of Moving Average Smoothing
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 99 / 271
Chapter 2: Trend Analysis Method of Moving Average Smoothing
yn−k+1 + yn−k+2 ⋯ + yn 1 n
SMAk = = ∑ yi
k k i=n−k+1
the EMA for a series Y may be calculated recursively:
⎧
⎪
⎪y1 , t=0
St = ⎨
⎪
⎩αyt + (1 − α) ⋅ St−1 , t > 0
⎪
where:
▸ the coecient α represents the degree of weighting decrease, a
constant smoothing factor between 0 and 1
▸ a higher α discounts older observations faster
▸ yt is the value at a time period t
▸ St is the value of the EMA at any time period t
this is known as simple exponential smoothing and Holt's exponential
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU)
smoothing MS in Data Science Fall - 2024 100 / 271
Chapter 2: Trend Analysis Method of Moving Average Smoothing
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 101 / 271
Chapter 2: Trend Analysis Method of Moving Average Smoothing
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 102 / 271
Chapter 2: Trend Analysis Method of Moving Average Smoothing
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 103 / 271
Chapter 2: Trend Analysis Exponential Moving Average (Exponential Smoothing)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 104 / 271
Chapter 2: Trend Analysis Exponential Moving Average (Exponential Smoothing)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 106 / 271
Chapter 2: Trend Analysis Moving Average in Python
import pandas as pd
import numpy as np
# Create a numpy array of years and sales
arr = np.array([['2000', 4], ['2001',7],
['2002',4], ['2003',9],
['2004',7], ['2005',10]])
# Transpose array
arr_tp = arr.transpose()
print(arr_tp)
# Create a dataframe
df = pd.DataFrame({'Years': arr_tp[0], 'Sales': arr_tp[1]})
print(df)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 107 / 271
Chapter 2: Trend Analysis Moving Average in Python
Airpassenger dataset
df['SMA_3'] = df['#Passengers'].rolling(window=3).mean()
df['SMA_4'] = df['#Passengers'].rolling(window=4).mean()
df['SMA_12'] = df['#Passengers'].rolling(window=12).mean()
df.head(10)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 108 / 271
Chapter 2: Trend Analysis Moving Average in Python
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 109 / 271
Chapter 2: Trend Analysis Moving Average in Python
Airpassenger dataset
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 110 / 271
Chapter 2: Trend Analysis Moving Average in Python
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 111 / 271
Chapter 2: Trend Analysis Detrend of Time Series
Manual Dierencing
write a function of dierences and forecast value
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 113 / 271
Chapter 2: Trend Analysis Detrend of Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 114 / 271
Chapter 2: Trend Analysis Detrend of Time Series
rolling windows
Sieve estimators
polynomial tting
smoothing technique
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 115 / 271
Chapter 2: Trend Analysis Detrend of Time Series
Homework-2
4 Draw a time series plot of the DSEX Index for 2021 and then draw a
linear and quadratic trend lines. Comments on your ndings.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 116 / 271
Chapter 3: Measurement of Seasonal Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 117 / 271
Chapter 3: Measurement of Seasonal Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 118 / 271
Chapter 3: Measurement of Seasonal Variation Seasonal Variation
Seasonal Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 119 / 271
Chapter 3: Measurement of Seasonal Variation Seasonal Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 120 / 271
Chapter 3: Measurement of Seasonal Variation Seasonal Variation
Motivation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 121 / 271
Chapter 3: Measurement of Seasonal Variation Seasonal Index
Seasonal Index
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 122 / 271
Chapter 3: Measurement of Seasonal Variation Method of Simple Averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 123 / 271
Chapter 3: Measurement of Seasonal Variation Method of Simple Averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 124 / 271
Chapter 3: Measurement of Seasonal Variation Method of Simple Averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 125 / 271
Chapter 3: Measurement of Seasonal Variation Method of Simple Averages
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 126 / 271
Chapter 3: Measurement of Seasonal Variation Method of Simple Averages
advantages
method of simple average is easy and simple to execute
disadvantages
this method is based on the additive model of the time series
this method is based on the basic assumption that the data do not
contain any trend and cyclic components
since most of the economic and business time series have trends and
as such this method though simple is not of much practical utility
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 127 / 271
Chapter 3: Measurement of Seasonal Variation Ratio to trend method
1200 400
k= and k=
Total of the indices Total of the indices
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 129 / 271
Chapter 3: Measurement of Seasonal Variation Ratio to trend method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 130 / 271
Chapter 3: Measurement of Seasonal Variation Ratio to trend method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 131 / 271
Chapter 3: Measurement of Seasonal Variation Ratio to trend method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 132 / 271
Chapter 3: Measurement of Seasonal Variation Ratio to trend method
Ratio-to-moving-average method
1 nd the centered 12 monthly (or 4 quarterly) moving averages of the
original data values in the time-series
4 nd the averages over all months or quarters of the given years.
5 if the sum of these indices is not 1200 (or 400 for quarterly gures),
multiply then by a correction factor = 1200 / (sum of monthly
indices) otherwise, the 12 monthly averages will be considered as
seasonal indices
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 134 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 135 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 136 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 137 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 138 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 139 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 140 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 141 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 142 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 143 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 144 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 145 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 146 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 147 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 148 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 149 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
advantages
of all the methods of measuring seasonal variations, the ratio to
moving average method is the most satisfactory, exible and widely
used method
disadvantages
this method does not completely utilize the data for example in
case of 12-monthly moving average seasonal indices cannot be
obtained for the rst 6 months and last 6 months
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 150 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 151 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 152 / 271
Chapter 3: Measurement of Seasonal Variation Ratio-to-moving-average method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 153 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 154 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 155 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 156 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 157 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Assignment-2
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 158 / 271
Chapter 3: Measurement of Seasonal Variation Link Relative Method
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 159 / 271
Chapter 4: Stationary Processes (with Quiz # 1)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 160 / 271
Chapter 4: Stationary Processes (with Quiz # 1)
4.3 Correlogram
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 162 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
There are several methods for measuring cyclical variation. One widely
used approach is the Residual Method, which we will discuss below.
Some other methods are:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 163 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
Trend (Tt )
Cyclical (Ct )
Irregular (It )
However, when working with monthly or quarterly data, seasonal
uctuations need to be considered in addition to the trend and cyclical
components.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 164 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
yt
= Ct × I t .
Tt × St
Here, the term Ct × I t represents the combined cyclical and irregular
components.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 165 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 166 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
To illustrate the Residual Method, let's take a look at the example in the
table below. The table presents theoretical data that has been processed
using the residual method, with the following steps applied:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 167 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Measures of Cyclical Variation
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 168 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Means, Variances, Covariances and Autocorrelation Function
µt = E [yt ]
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 169 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Means, Variances, Covariances and Autocorrelation Function
Var(yt ) = E [(yt − µt )2 ]
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 170 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Means, Variances, Covariances and Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 171 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Means, Variances, Covariances and Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 172 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Correlogram
Correlogram
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 173 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Correlogram
# Alternatively
# import the plotting functions for act and pacf
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# plot_acf(df['#Passengers']);
plot_acf(df['#Passengers'], lags=40);
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 175 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Python & R Code: Autocorrelation Plot (Correlogram)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 176 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Types of Stationarity
time series are stationary if they do not have trend or seasonal eects
Types of Stationarity
strict stationarity
▸ the time series {yt } is said to be strictly stationary if for any nite
sequence of integers t1 , t2 , ⋯, tk and shift h the distribution of
(yt1 , yt2 , ⋯, ytk ) and (yt1+h , yt2+h , ⋯, ytk+h ) are the same
▸ in your general notation for denities, this strict stationarity requirement
can be written as
fYn1 ,Yn2 ,...,YnK (y1 , y2 , . . . , yK ) = fYn1 +h ,Yn2 +h ,...,YnK +h (y1 , y2 , . . . , yK )
White Noise
White Noise is a time series with the following characteristics:
▸ Zero Mean: The expected value of the series is zero: E [yt ] = 0.
▸ Constant Variance: The variance of the series is constant over time:
Var(yt ) = σ 2 .
▸ No Autocorrelation: The autocorrelation function (ACF) is zero for
all lags h ≠ 0, i.e., ρ(h) = 0 for all h ≠ 0.
if a time series is white noise, it is a sequence of random numbers and
cannot be predicted
if the series of forecast errors are not white noise, it suggests
improvements could be made to the predictive model
In a correlogram, white noise would show:
▸ No signicant peaks in the autocorrelation plot.
▸ The autocorrelation function should be close to zero for all lags.
White noise is considered the opposite of a time series with trends or
seasonality.
if the variables in the series are drawn from a Gaussian distribution,
the series is called Gaussian white noise
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 178 / 271
Chapter 4: Stationary Processes (with Quiz # 1) White Noises
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 179 / 271
Chapter 4: Stationary Processes (with Quiz # 1) White Noises
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 180 / 271
Chapter 4: Stationary Processes (with Quiz # 1) White Noises
Key characteristics:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 182 / 271
Chapter 4: Stationary Processes (with Quiz # 1) The Random Walk Model
yt = yt−1 + t
where:
In a correlogram:
▸ The autocorrelation function (ACF) of a random walk typically shows a
slow decay and does not approach zero quickly, reecting the
persistence of the shock.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 184 / 271
Chapter 4: Stationary Processes (with Quiz # 1) The Random Walk Model
where:
Interpretation of Autocorrelation:
▸ The autocorrelation function (ACF) for a random walk with drift
decays more slowly than that of a simple random walk.
▸ The process tends to "drift" in the direction of the drift term, µ,
leading to long-term trends.
Applications:
▸ Stock prices: Modeling stock prices or asset returns that generally
increase or decrease over time.
▸ Economics: Modeling GDP growth or other economic indicators that
trend in a certain direction.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 186 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Integrated Processes
yt = yt−1 + t
Applications:
▸ Used for modeling economic data such as GDP, ination, and stock
prices.
▸ Helps in modeling long-term trends and non-stationary data.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 188 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Mathematical Denition:
φhh = correlation(yt , yt+h ∣ yt−1 , yt−2 , . . . , yt+h−1 )
Interpretation:
▸ PACF at lag 1 is the same as the ACF at lag 1.
▸ PACF at higher lags removes the inuence of intermediate lags,
showing the direct relationship.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 189 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 190 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
the use of this function was introduced as part of the BoxJenkins approach
to time series modelling, whereby plotting the partial autocorrelative
functions one could determine the appropriate lags p in an AR(p) model or
in an extended ARIMA(p, d, q) model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 191 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 192 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 193 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 194 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 195 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 196 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 197 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Partial Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 198 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Python & RCode: Partial Autocorrelation Plot
import pandas as pd
from statsmodels.graphics.tsaplots import plot_pacf
from statsmodels.tsa.stattools import pacf
import matplotlib.pyplot as plt
df = pd.read_csv("AirPassengers.csv")
plot_pacf(df['#Passengers'], title='PACF: Number of Passengers')
print(pacf(df['#Passengers']))
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 199 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Python & RCode: Partial Autocorrelation Plot
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 200 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Eviews: Autocorrelation Function (ACF) and Correlogram
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 201 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Eviews: Autocorrelation Function (ACF) and Correlogram
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 202 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Statistical Signicance of Autocorrelation Coecients
the 95% of the sample auto correlations (ρ̂) should fall between
−1.96 1.96
( √ , √ )
n n
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 203 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Statistical Signicance of Autocorrelation Coecients
h
Q = n ∑ ρ̂2 (j)
j= 1
we know that,
1 √
ρ̂ ∼ N (0, ) i.e., nρ̂ ∼ N (0, 1)
n
Q is approximately distributed as the sum of sequences of the
independent N ( 0, 1 ) random variables
√
nρ̂ (j) ; j = 1, 2, ⋯, h
i.e., as Chi-squared with h degrees of freedom
we therefore reject the iid hypothesis at level α if Q > χ21−α (h), where
χ21−α (h) is the 1 − α quantile of the chi-squared distribution with
h d.f .
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 204 / 271
Chapter 4: Stationary Processes (with Quiz # 1) Statistical Signicance of Autocorrelation Coecients
h
ρ̂2 (j)
QLB = n (n + 2) ∑
j=1 (n − j)
h
ρ̂2ww (k)
Q̃ = n (n + 2) ∑
k=1 (n − k)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 206 / 271
Chapter 5: Models for Stationary Time Series
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 207 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 208 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 209 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Estimation of Parameters
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 210 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Model Diagnostics
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 211 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 212 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
where:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 213 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Yule-Walker Equations
Method of Moments
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 214 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
The OLS method minimizes the sum of squared residuals to estimate the
parameters φ1 , φ 2 , . . . , φ p . The AR(p) model is:
n p 2
∑ (yt − ∑ φi yt−i )
t=p+ 1 1
i=
φ̂ = (X T X )−1 X T y
where X is the matrix of lagged values and y is the vector of observations.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 215 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
p
γk = ∑ φi γk−i for k = 1 , 2, . . . , p
i= 1
where:
Γφ = γ
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 216 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
⎛ γ0 γ1 γ2 ⋯ γp−1 ⎞
⎜ γ1 γ0 γ1 ⋯ γp−2 ⎟ ⎛φ1 ⎞ ⎛γ1 ⎞
⎜ ⎟ ⎜φ2 ⎟ ⎜γ2 ⎟
⎜ γ2 1 γ0 ⋯ γp−3 ⎟
⎜ γ ⎟⎜ ⎟=⎜ ⎟
⎜ ⋮ ⎟ ⎜⋮⎟
⎜ ⋮ ⋮ ⋮ ⋱ ⎟
⋮ ⎟⎝ ⎠ ⎝ ⎠
⎜
⎝γp−1 γp−2 γp−3 φ γp
⋯ γ0 ⎠ p
Here:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 217 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
φ = Γ−1 γ
This gives the solution for the AR coecients:
− 1
⎛ γ0 γ1 γ2 ⋯ γp−1 ⎞
⎛ φ1 ⎞ ⎜ γ1 γ0 γ1 ⋯ γp−2 ⎟ ⎛ γ1 ⎞
⎜ φ2 ⎟ ⎜ ⎟ ⎜ γ2 ⎟
⎜ ⎟=⎜ γ 2 γ 1 γ0 ⋯ γp−3 ⎟ ⎜ ⎟
⎜ ⋮ ⎟ ⎜⎜ ⋮
⎟ ⎜⋮⎟
⎝φp ⎠ ⎜ ⋮ ⋮ ⋱ ⋮ ⎟⎟ ⎝γp ⎠
⎝γp−1 γp−2 γp−3 ⋯ γ0 ⎠
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 218 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
( 0 1) ( 1) = ( 1)
γ γ φ γ
γ1 γ0 φ2 γ2
To solve for φ1 and φ2 , we invert the 2 × 2 autocovariance matrix Γ and
multiply by the vector γ:
− 1
( 1) = ( 0 1) ( 1)
φ γ γ γ
φ2 γ1 γ0 γ2
After computing the matrix inverse, we can nd the values of φ1 and φ2 .
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 219 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
n
L(φ1 , φ2 , . . . , φp ) = ∏ f (yt ∣ yt−1 , yt−2 , . . . , yt−p ; φ1 , φ2 , . . . , φp )
t=p+ 1
The log-likelihood function is usually maximized:
n
log L(φ1 , φ2 , . . . , φp ) = ∑ log f (yt ∣ yt−1 , yt−2 , . . . , yt−p ; φ1 , φ2 , . . . , φp )
t=p+ 1
In practice, we assume that the errors t are normally distributed, so f (yt )
is the normal density function. This maximization often requires numerical
optimization techniques.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 220 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
4. Method of Moments
The Method of Moments is used primarily for simple models like AR(1). In
this method, the sample moments (e.g., autocovariances) are equated to
the theoretical moments.
For an AR(1) model:
yt = φ1 yt−1 + t
where:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 221 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
γ0 = Var(yt )
From the AR(1) model:
yt = φ1 yt−1 + t
Assuming that yt and t are independent, the variance of yt is:
γ0 = φ21 γ0 + σ 2
Rearranging this:
γ0 (1 − φ21 ) = σ 2
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 222 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
σ2
γ0 = 2
1 − φ1
γ1 = Cov(yt , yt−1 )
Substituting the AR(1) model:
γ1 = φ1 Var(yt−1 ) = φ1 γ0
Thus, the theoretical autocovariance at lag 1 is:
γ1 = φ1 γ0
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 223 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
The sample autocovariances are computed directly from the observed data.
The sample autocovariance at lag 1 is:
n
1
γ̂1 = ∑ (yt − ȳ )(yt−1 − ȳ )
n t=2
The sample variance at lag 0 is:
n
γ̂0 =
1
∑ (yt − ȳ )
2
n t=1
According to the Method of Moments, we equate the sample
autocovariances with the theoretical values:
γ̂1 = φ1 γ̂0
Solving for φ1 , we get:
γ̂1
φ̂1 =
γ̂0
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 224 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
φ̂1 =
Cov(yt , yt− 1)
Var(yt− 1)
For higher-order AR models, the method becomes impractical, and
methods like OLS or Yule-Walker are preferred.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 225 / 271
Chapter 5: Models for Stationary Time Series The AR(p) Autoregressive Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 226 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 227 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
Autocovariance at Lag 0
For lag 0, the autocovariance is the variance of yt :
γ0 = Var(yt )
Substituting the AR(p) model into the equation for yt :
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 228 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
σ2
γ0 = p 2
1−∑
i=1 φi
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 229 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
Autocovariance at Lag 1
γ1 = Cov(yt , yt−1 )
Substituting the AR(p) model for yt and yt−1 :
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 230 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
γ1 = φ1 γ0 + φ2 γ1 + φ3 γ2 + ⋅ ⋅ ⋅ + φp γp−1
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 231 / 271
Chapter 5: Models for Stationary Time Series Autocovariance Function for AR(p)
γk = Cov(yt , yt−k )
This can be written as:
p
γk = ∑ φi γk−i
i= 1
This is a recursive relationship for computing autocovariances at higher
lags. Once γ0 is known, the other autocovariances can be computed using
this recurrence.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 232 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Autocorrelation Function
γk
ρk =
γ0
For lag k, we have:
p
∑ φi γk−i
ρk = i=1
γ0
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 233 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
yt = φ1 yt−1 + t
The autocovariance at lag 0 is:
σ2
γ0 = 2
1 − φ1
γ1 = φ1 γ0
For k ≥ 2:
γk = φk1 γ0
Therefore, the autocorrelation function for the AR(1) model is:
ρk = φk1
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 234 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 235 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 236 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 238 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 239 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 240 / 271
Chapter 5: Models for Stationary Time Series Autocorrelation Function
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 241 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 242 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 243 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
yt = µ + t + θ1 t−1 + θ2 t−2
Here, the current value yt is inuenced by the:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 244 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 245 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 246 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 247 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 248 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 249 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 250 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 251 / 271
Chapter 5: Models for Stationary Time Series The MA(q) Moving average Model
Usually, ACF denes error serial correlation well and is thus used to
detect MA(q); however, as the order increases and other time series
components such as seasonality,trend, or stationary come into picture,
this makes interpretation harder
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 252 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 253 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Where:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 254 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 255 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
yt = µ + φ1 yt−1 + t + θ1 t−1
In this case, the current value yt depends on:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 256 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 257 / 271
Chapter 5: Models for Stationary Time Series The Autocorrelation Function of ARMA(1)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 258 / 271
Chapter 5: Models for Stationary Time Series The Autocorrelation Function of ARMA(1)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 259 / 271
Chapter 5: Models for Stationary Time Series The Autocorrelation Function of ARMA(1)
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 260 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
In an ARMA(p, q) model:
Cross-validation.
Model diagnostics.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 261 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 262 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 263 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
AIC:
AIC = 2k − 2 ln(L)
where k is the number of parameters and L is the likelihood.
BIC:
BIC = ln(n)k − 2 ln(L)
where n is the number of observations.
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 264 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Cross-Validation
Cross-validation involves splitting the data into training and test sets to
evaluate the model's forecasting performance.
Assess the forecasting accuracy on the test set (e.g., using Mean
Squared Error).
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 265 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Model Diagnostics
After selecting p and q, check the residuals to ensure the model ts well:
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 266 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
print("RMSE: {}".format(np.mean(rmse)))
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 267 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 268 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 269 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 270 / 271
Chapter 5: Models for Stationary Time Series The ARMA(p, q) Model
Dr. Md. Rezaul Karim (Professor, Dept. of Statistics and Data Science, JU) MS in Data Science Fall - 2024 271 / 271