Fundamentals of Time Series Data and Forecasting
We come across many instances in real life where we try to forecast what will happen in
future(tomorrow,next week,next month,next year or may be in coming years etc).Few common
examples are :
1.What will be the stock price after a month?
2.What revenue business will make next year?
3.What will be the air temperature tomorrow? and many more…
If you analyse these questions,one common factor in all the questions is TIME. Thus,when data is
recorded on a timely basis It is called as a time series and analysis on this data is known as Time
Series Analysis.
Time series forecasting is the use of statistical methods to predict future behavior based on
historical data.
This is similar to other statistical learning approaches, such as supervised or unsupervised
learning. However, time series forecasting has many nuances that make it different from regular
machine learning. From data processing all the way to model validation, time series forecasting
is a different beast.
Time Series Data
A time series is a sequence of data points recorded through time.
Thus, when dealing with time series data, order matters. Specifically, values in a time series
express a dependency on time. Consequently, if we change the order of a time series, we may
change the meaning of the data.
Usually, time series data have two important properties.
Data is measured sequentially and equally spaced in time.
Each time unit has at most one data measurement.
In addition, when doing time series forecasting, we usually have two goals.
First, we want to identify patterns that explain the behavior of the time series.
Second, we want to use these patterns to forecast (predict) new values.
Components of Time Series :
The factors that are responsible for bringing about changes in a time series, also called the
components of time series, are as follows:
1. Irregular Component : These are sudden changes occurring in a time series which are
unlikely to be repeated. This is residual after we have removed all other components from
time-series data
2. Cyclic component : These are long term oscillations occurring in a time series.
It is a kind of change that is not related to seasonal factors. These are rises and falls with
non-fixed magnitudes that can last for more than a calendar year. Cyclical patterns are not
repetitive. Usually, they result from external factors which make them much harder to
predict.
3. Trend Component : Trend is the main component of a time series.Trend may show the
growth or decline in a time series over a long period. The trend in Time Series data can be
linear or non-linear that changes over time and does not repeat itself within the known time
range. There is repetition in data over systematic intervals of time.
The trend describes the general behavior of a time series. If a time series manifests a
positive long-term slope over time, it has an upward trend. If instead, it describes a general
negative slope, it has a downtrend.
The overall trend may also change direction. There can be an up-to-downtrend or a down-to-
up trend. Lastly, a stationary or horizontal trend defines a time series with neither positive
nor negative long-term patterns.
4. Seasonal Component : These are short term movements occurring in data due to seasonal
factors.
These events can be the time of year (like winter or summer), or the time of day or the week.
Seasonality always has fixed frequencies. That is, a seasonal pattern always starts and ends
in the same period of a week, year, etc. eg. a clothing store that sells heavy coats might
observe higher selling rates during winter, as opposed to the summer
Time Series decomposition-
The decomposition of time series is a statistical task that deconstructs a time series into
several components. Each component represents one of the underlying categories of patterns
Time Series decomposition can be additive or multiplicative
Additive Model
Y= T + S + C + R
Additive decomposition should be used when
The magnitude of seasonal variation around the trend cycle does not vary with the level of
time series
Multiplicative Model
Y= T * S * C * R
Multiplicative decomposition should be used when
variation in trend is proportional to the level of time series
Additive Time series decomposition
How do we forecast a time series?
Most of the time series data will be non-stationary but a common assumption in many time series
technique is that the data is stationary. Time Series data will have some pattern or behavior over
a period of time. If we understand the underlying pattern then we can say with a high probability
that the time series will follow the same pattern in the future too
Property of stationary data is that it has
constant mean,
constant variance and
autocorrelation does not change over time.
Stationarity and Correlation in time series models
Stationarity
Another important element in the Time Series is Stationarity. So let’s discuss this in a little
more detail.
A time series is said to be stationary, when it’s statistical properties do not change over time.
That is, it’s mean, variance and autocorrelation are equally distributed over time.
Stationary data with constant mean and Non Stationary data with changing mean
Non Stationary data with high variance
In the figures above, the green graph represents a stationary time series, whereas the red graph
— a non-stationary one. In the first row, we can observe the red graph is not stationary, as its
mean increases over time. In the second row, we observe the variance is not constant in the red
graph. And finally, in the third row, we see that the co-variance is seen as a function of time, that
is spread becomes closer as the time increases.
Stationarity is seen as a prerequisite of time series models. The reason for this is, when we try to
make predictions on a time series, the statistical properties of the time series, that is the mean,
variance and correlation should not be different than the ones currently observed. If the time
series was non-stationary, making models and predictions on these properties would not give us
an accurate result, as these properties would have changed.
Autocorrelation
Autocorrelation can be seen as the measure of internal correlation in a time series. It is a way
of measuring and explaining the internal association between observations. You could have a
very strong and positive association, that the time series at one point is going to be the same as a
point in some time in the future. Or it could be a very strong and negative association, that is the
time series at one point is going to be completely different at a point in the future.
Autocorrelation is always measured between +1 and -1. With +1 indicating a strong positive
association, -1 a strong negative association and 0 indicating no association.
In order to choose two variables for a time series model, we have to perform statistical
correlation analysis on them. This can be done by using the Gaussian Curve and Pearson’s
Coefficient to identify the correlation between the variables.
In the autocorrelation chart, if the autocorrelation crosses the dashed blue line, it means that specific lag is significantly
correlated with the current series. For example, in autocorrelation chart of Air Passengers - the top-left chart (below),
there is significant auto correlation for all the lags shown on x-axis.
Autocorrelation measures historic time data called lags. Autocorrelation functions are used to
plot such correlations with respect to lag. It shows if the previous state has an effect on the
current state. Autocorrelation is also used to determine if the series is stationary or not. If the
series is stationary, the autocorrelation will fall to zero almost instantaneously, however, if it is
non-stationary, it will gradually decrease. There is also something called, Partial Auto
Correlation Functions (PACF). PACF is defined as time series which have a partial or
incomplete correlation between its variables. This concept is used in Autoregressive models.
Time Series Analysis :
Below diagram shows different types of time series analysis(I haven't considered all types of
analysis though tried to cover the common ones)
Time domain based methods : Two most commonly used time domain methods are :
1. Auto correlation
2. Cross correlation
Auto Correlation :
One of the basic assumption of Linear Regression(Ordinary Least Squares Procedure)is that the
observations of error terms are independent of each other.If this assumption is violated and the
error terms are correlated with each other,Auto Correlation exists.Auto correlation is a very
common problem in time series regression.
When auto correlation is present,error terms follow a pattern and such patterns tell us that
something is wrong. The presence of auto correlation does not mean that the values of one
independent variable are correlated over time. Also, it does not mean that independent variables
are correlated with each other, as occurs with multicollinearity.
Why Auto correlation is bad for the model?
For each observation, the error term represents the distance between the actual value of the
dependent variable and the predicted value. Think of the error term as the model’s “mistake.”
These mistakes must not follow a pattern. If there is such a pattern, then there must be some
way to improve the model so that the regression does a better job of predicting the dependent
variable. A model that exhibits auto correlation can perform better than it is.
There are few methods which check whether auto correlation exists or not,one of which
is Durbin-Watson statistic.
Cross Correlation :
If you have two time series x and y,Series y may be related to past lags of x series.Cross
correlation helps us in identifying lags of x variables that might help in predicting y variables.
Parameter Based Methods :
Parametric methods : I ll be covering two parametric methods in this blog
1. Auto Regression
2. Moving Averages
Auto Regression :
Linear Regression model predicts the dependent variable based on linear combination of
independent variables.
for example : y = ax + c
where y is the dependent variable and x is independent variable .
Linear Regression can also be used in time series analysis where input variables at previous time
steps can be used as one of the features called as lag feature to predict the output variable
For example, we can predict the value for the next time step (t+1) given the observations at the
last two time steps (t-1 and t-2). As a regression model, this would look as follows:
X(t+1) = a*X(t-1) +b*X(t-2)
Because the regression model uses data from the same input variable at previous time steps, it is
referred to as an auto regression (regression of self).
Moving Averages :
Moving Averages are an extension to average.
Moving averages are calculated by taking arithmetic mean of a given set of values. For example,
to calculate a basic 10-day moving average of closing prices you would add up the closing prices
from the past 10 days and then divide the result by 10. Some of the primary functions of a
moving average are to identify trends.