This document discusses time series forecasting. It begins with an introduction to time series analysis and its components, including trend, seasonality, cyclicity, and irregularity. It then provides an example of using a moving average method to smooth and forecast quarterly car sales data over five years. The moving average helps extract the trend from the raw time series data by removing the seasonal and irregular components. This smoothed data can then be used to forecast future time periods.
2. What’s in it for you?
Why Time Series?
What is Time Series?
Components of a Time Series
When NOT to use Time Series?
Why does a Time Series have to be stationary?
How to make a Time Series stationary?
Example: Forecast car sales for 5th year
3.
4. Well, you can actually do that
with Time Series forecasting
5. Well, you can actually do that
with Time Series forecasting
Daily Stock Price
Sales figures
where the outcome (independent variable) is dependent on time
In such scenarios, we use Time Series forecasting
Weekly interest rates
We can predict:
7. What is Time Series?
A Time Series data for stock price analysis may look like this:
The stock prices change everyday!
8. What is Time Series?
A simple plot shows that it is increasing with time!
9. What is Time Series?
It is time-dependent
These data points (past values) are analyzed to forecast a future
A Time Series is a sequence of data being recorded at specific time intervals
13. Time Series is affected by
four main components
Trend is the increase or decrease in the series over a period of time, it persists over a
long period of time
Example: Population growth over the years can be seen as an upward trend
Trend
14. Time Series is affected by
four main components
Regular pattern of up and down fluctuations
It is a short-term variation occurring due to seasonal factors
Example: Sales of ice-cream increases during summer season
Seasonality
15. Time Series is affected by
four main components
It is a medium-term variation caused by circumstances, which repeat in irregular
intervals
Example: 5 years of economic growth, followed by 2 years of economic recession,
followed be 7 years of economic growth followed by 1 year of economic recession
Cyclicity
16. Time Series is affected by
four main components
It refers to variations which occur due to unpredictable factors and also do not repeat in
particular patterns
Example: Variations caused by incidents like earthquake, floods, war etc.
Irregularity
18. 1
When NOT to use Time Series Analysis?
There are various conditions where you should not use Time Series:
When the values are constant over a period of time:
19. When NOT to use Time Series Analysis?
When values can be represented by known functions
like cosx, sinx etc:
There are various conditions where you should not use Time Series:
2
24. Time Series is affected by
four main components
Trend Seasonality Cyclicity Irregularity
25. If these components are
present in Time Series data,
it is a Non-Stationary Time
Series Data.
26. Time Series is affected by
four main components
For example, look at this graph:
0
1
2
3
4
5
6
7
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Year 1 Year 2 Year 3 Year 4
Time Series Forecasting
Here, the mean is non-constant and there
is clearly an upward trend
28. Stationarity of Time Series
When a Time Series is stationary, we can identify previously unnoticed components to
strengthen their forecasting
A Non-stationary Time Series has trend and seasonality components, which will affect the
forecasting of Time Series
29. How do you differentiate
between a stationary and
Non-Stationary time series?
30. How do you differentiate
between a stationary and
Non-Stationary time series?Stationarity of Time Series depends on:
Mean
Variance
Co-Variance
31. Stationarity of Time Series
The mean of the series should not be a function of time rather should be a constant
Here, mean is
constant with time
Here, mean is
increasing with
time
Stationary Non-Stationary
32. Stationarity of Time Series
The variance of the series should not be a function of time
Stationary Non-Stationary
Notice, the varying spread of distribution in the red grapgh, this shows that variance is
changing with time
33. Stationarity of Time Series
The covariance of the i th term and the (i + m) th term should not be a function of time
Stationary Non-Stationary
The spread becomes closer as the time increases. Hence, the covariance is not constant with
time for the ‘red series’ and is Non-Stationary!
34. Before I tell you how to make it
stationary and model it, let’s look
at Moving Average method
36. What is that?
A moving average is a technique to get an overall idea of the trends in a data set; it is an
average of any subset of numbers
Let’s say that you have been in business for three months, January through March, and wanted to forecast
April’s sales. Your sales for the last three months look like this:
37. What is that?
Moving Average(3) would be to take the average of January through March and use that to estimate
April’s sales:
(129 + 134 + 122)/3 = $128.333
38. What is that?
Hence, based on the sales of January through March, you predict that sales in April will be $128.333
Once April’s actual sales come in, you would then compute the forecast for May, this time using February through
April
40. What is that?
Here, we have Quarterly sales data for car sales.
Let’s look at the data to see how it moves through time
Year Quarter Sales(1000s)
Year 1 1 2.8
2 2.1
3 4
4 4.5
Year 2 1 3.8
2 3.2
3 4.8
4 5.4
Year 3 1 4
2 3.6
3 5.5
4 5.8
Year 4 1 4.3
2 3.9
3 6
4 6.4
41. What is that?
Year Quarter Sales(1000s)
Year 1 1 2.8
2 2.1
3 4
4 4.5
Year 2 1 3.8
2 3.2
3 4.8
4 5.4
Year 3 1 4
2 3.6
3 5.5
4 5.8
Year 4 1 4.3
2 3.9
3 6
4 6.4
Year 5 1 ?
2 ?
3 ?
4 ?
We want to forecast the sales for the next four quarters (or the fifth year)
42. Example to forecast Time Series
Plot of our Time Series data for the first four years looks like:
0
1
2
3
4
5
6
7
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Year 1 Year 2 Year 3 Year 4
Time Series Forecasting
43. Example to forecast Time Series
Clearly, we see that the curve repeats in a pattern after every quarter (yearly) indicating seasonality:
44. Example to forecast Time Series
And the overall direction of the plot is increasing, known as the “trend component”:
45. And even though the pattern is
clearly visible there is no data as
perfect data, it will have some
variability
47. What is that?
t Year Quarter Sales(1000s)
1 Year 1 1 2.8
2 2 2.1
3 3 4
4 4 4.5
5 Year 2 1 3.8
6 2 3.2
7 3 4.8
8 4 5.4
9 Year 3 1 4
10 2 3.6
11 3 5.5
12 4 5.8
13 Year 4 1 4.3
14 2 3.9
15 3 6
16 4 6.4
We will give a time code(t) variable to each row indicating each time period, we will use this variable later:
48. What is that?t Year Quarter Sales(1000s) MA(4)
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4
4 4 4.5 3.6
5 Year 2 1 3.8 3.9
6 2 3.2 4.1
7 3 4.8 4.3
8 4 5.4 4.4
9 Year 3 1 4 4.5
10 2 3.6 4.6
11 3 5.5 4.7
12 4 5.8 4.8
13 Year 4 1 4.3 4.9
14 2 3.9 5.0
15 3 6 5.2
16 4 6.4
Here, we have calculate Moving Average(4) for the first four quarters:
(2.8+2.1+4+4.5)/4
49. What is that?t Year Quarter Sales(1000s) MA(4)
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4
4 4 4.5 3.6
5 Year 2 1 3.8 3.9
6 2 3.2 4.1
7 3 4.8 4.3
8 4 5.4 4.4
9 Year 3 1 4 4.5
10 2 3.6 4.6
11 3 5.5 4.7
12 4 5.8 4.8
13 Year 4 1 4.3 4.9
14 2 3.9 5.0
15 3 6 5.2
16 4 6.4
Now, for next four the values shift and we calculate as follows:
(2.1+4+4.5+3.8)/4
50. What is that?t Year Quarter Sales(1000s) MA(4)
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4
4 4 4.5 3.6
5 Year 2 1 3.8 3.9
6 2 3.2 4.1
7 3 4.8 4.3
8 4 5.4 4.4
9 Year 3 1 4 4.5
10 2 3.6 4.6
11 3 5.5 4.7
12 4 5.8 4.8
13 Year 4 1 4.3 4.9
14 2 3.9 5.0
15 3 6 5.2
16 4 6.4
And so on..
Similarly, calculate the remaining values:
51. What is that?t Year Quarter Sales(1000s) MA(4) CMA
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4 3.5
4 4 4.5 3.6 3.7
5 Year 2 1 3.8 3.9 4.0
6 2 3.2 4.1 4.2
7 3 4.8 4.3 4.3
8 4 5.4 4.4 4.4
9 Year 3 1 4 4.5 4.5
10 2 3.6 4.6 4.7
11 3 5.5 4.7 4.8
12 4 5.8 4.8 4.8
13 Year 4 1 4.3 4.9 4.9
14 2 3.9 5.0 5.1
15 3 6 5.2
16 4 6.4
Now, since the moving average is not centered because of even number of data points, we will have to calculate “Centered
Moving Average” as shown:
52. What is that?
We will give a time code(t) variable to each row indicating each time period, let’s this variable later:
t Year Quarter Sales(1000s) MA(4) CMA
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4 3.5
4 4 4.5 3.6 3.7
5 Year 2 1 3.8 3.9 4.0
6 2 3.2 4.1 4.2
7 3 4.8 4.3 4.3
8 4 5.4 4.4 4.4
9 Year 3 1 4 4.5 4.5
10 2 3.6 4.6 4.7
11 3 5.5 4.7 4.8
12 4 5.8 4.8 4.8
13 Year 4 1 4.3 4.9 4.9
14 2 3.9 5.0 5.1
15 3 6 5.2
16 4 6.4
53. What is that?
We will give a time code(t) variable to each row indicating each time period, let’s discuss this variable later:
t Year Quarter Sales(1000s) MA(4) CMA
1 Year 1 1 2.8
2 2 2.1
3 3 4 3.4 3.5
4 4 4.5 3.6 3.7
5 Year 2 1 3.8 3.9 4.0
6 2 3.2 4.1 4.2
7 3 4.8 4.3 4.3
8 4 5.4 4.4 4.4
9 Year 3 1 4 4.5 4.5
10 2 3.6 4.6 4.7
11 3 5.5 4.7 4.8
12 4 5.8 4.8 4.8
13 Year 4 1 4.3 4.9 4.9
14 2 3.9 5.0 5.1
15 3 6 5.2
16 4 6.4
55. Let’s visualize this to see the
difference between the original
data and the smoothened data
56. Example to forecast Time Series
The blue line which represents “Centered Moving Average” is smoothened as compared to the original data, as we
can see in the plot:
0
1
2
3
4
5
6
7
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Year 1 Year 2 Year 3 Year 4
Time Series Forecasting
Time Series Data
CMA
57. Example to forecast Time Series
Smoothening means taking out the seasonal component and the irregular component:
0
1
2
3
4
5
6
7
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Year 1 Year 2 Year 3 Year 4
Time Series Forecasting
Time Series Data
CMA
58. Example to forecast Time Series
So, we can now extract the seasonal and irregular components from the original data:
0
1
2
3
4
5
6
7
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Year 1 Year 2 Year 3 Year 4
Time Series Forecasting
Time Series Data
CMA
59. Why are we extracting these
components anyway?
67. But first, let’s calculate the
intercept and slope of the data as
it is required to calculate the trend
component
68. Example to forecast Time Series
Using simple regression, we will calculate the regression statistics using ‘Data Analysis’ feature in excel!
69. Example to forecast Time Series
Using simple regression, we will calculate the regression statistics using ‘Data Analysis’ feature in excel!
Intercept
Slope
70. So, to calculate trend, we will
need intercept and slope as seen
in previous slide
72. Example to forecast Time Series
Trend = Intercept + Slope * Time code
Trend Component = Intercept + Slope * Time Code
73. What is that?
So, using the multiplicative model, we can make the predictions.
To do that, we have to combine all the components that we separated
Predicted (Yp) = Seasonal component (St ) * Trend Component (Tt )
74. Example to forecast Time Series
We see that the predicted values(Yp) are similar to the actual values(Yt)
Hence, our calculations are correct
t Year Quarter Yt St De-Seasonalize Tt
Predicted
(Yp)
1 Year 1 1 2.8 0.90 3.10 3.21 2.90
2 2 2.1 0.77 2.74 3.36 2.58
3 3 4 1.14 3.51 3.51 4.00
4 4 4.5 1.21 3.72 3.66 4.43
5 Year 2 1 3.8 0.90 4.21 3.81 3.44
6 2 3.2 0.77 4.17 3.96 3.04
7 3 4.8 1.14 4.22 4.11 4.68
8 4 5.4 1.21 4.46 4.26 5.15
9 Year 3 1 4 0.90 4.43 4.40 3.98
10 2 3.6 0.77 4.69 4.55 3.49
11 3 5.5 1.14 4.83 4.70 5.35
12 4 5.8 1.21 4.79 4.85 5.87
13 Year 4 1 4.3 0.90 4.76 5.00 4.51
14 2 3.9 0.77 5.08 5.15 3.95
15 3 6 1.14 5.27 5.30 6.03
16 4 6.4 1.21 5.29 5.45 6.59
84. Example to forecast Time Series
Here we can see that the predicted values overlap with the actual values and continues till year
5 which shows the accuracy of our forecasted values
85. So, we have finally forecasted the
car sales for the four quarters of
the 5th year