A Comprehensive Guide To Time Series Analysis
A Comprehensive Guide To Time Series Analysis
Time Series Analysis is the way of studying the characteristics of the response
variable with respect to time, as the independent variable. To estimate the target
variable in the name of predicting or forecasting, use the time variable as the
point of reference. In this article we will discuss in detail TSA Objectives,
Assumptions, Components (stationary, and Non- stationary). Along with the TSA
algorithm and specific use cases in Python.
Table of Contents
A time series is nothing but a sequence of various data points that occurred in a successive order
for a given period of time
Objectives:
• To understand how time series works, what factors are affecting a certain
variable(s) at different points of time.
• Time series analysis will provide the consequences and insights of features of the
given dataset that changes over time.
• Supporting to derive the predicting the future values of the time series variable.
• Assumptions: There is one and the only assumption that is “stationary”, which
means that the origin of time, does not affect the properties of the process under
the statistical factor.
With help of “Time Series” we can prepare numerous time-based analyses and results.
• Forecasting
• Segmentation
• Classification
• Descriptive analysis`
• Intervention analysis
• Trend: In which there is no fixed interval and any divergence within the given
dataset is a continuous timeline. The trend would be Negative or Positive or Null
Trend
• Seasonality: In which regular or fixed interval shifts within the dataset in a
continuous timeline. Would be bell curve or saw tooth
• Cyclical: In which there is no fixed interval, uncertainty in movement and its
pattern
• Irregularity: Unexpected situations/events/scenarios and spikes in a short time
span.
What are the limitations of Time Series Analysis?
Time series has the below-mentioned limitations, we have to take care of those during our analysis,
• Similar to other models, the missing values are not supported by TSA
• The data points must be linear in their relationship.
• Data transformations are mandatory, so a little expensive.
• Models mostly work on Uni-variate data.
• Stationary
• Non- Stationary
6.1 Stationary: A dataset should follow the below thumb rules, without having Trend, Seasonality,
Cyclical, and Irregularity component of time series
• The MEAN value of them should be completely constant in the data during the
analysis
• The VARIANCE should be constant with respect to the time-frame
• The COVARIANCE measures the relationship between two variables.
7.1 Statistical Test: There are two tests available to test if the dataset is Stationary or NOT.
7.1.1 Augmented Dickey-Fuller (ADF) Test or Unit Root Test: The ADF test is the most popular
statistical test and with the following assumptions.
7.1.2 Kwiatkowski–Phillips–Schmidt–Shin (KPSS): these tests are used for testing a NULL Hypothesis
(HO), that will perceive the time-series, as stationary around a deterministic trend against the
alternative of a unit root. Since TSA looking for Stationary Data for its further analysis, we have to
make sure that the dataset should be stationary.
Converting Non- stationary into stationary
Let’s discuss quickly how to convert Non- stationary into stationary for effective time series
modeling. There are two major methods available for this conversion.
• Detrending
• Differencing
• Transformation
8.1 Detrending: It involves removing the trend effects from the given dataset and
showing only the differences in values from the trend. it always allows the cyclical
patterns to be identified.
8.2 Differencing: This is a simple transformation of the series into a new time series, which we use to
remove the series dependence on time and stabilize the mean of the time series, so trend and
seasonality are reduced during this transformation.
Yt= Yt – Yt-1
The Moving Average (MA) (Or) Rolling Mean: In which MA has calculated by taking averaging data
of the time-series, within k periods.
df_temperature.info()
The CMA is the unweighted mean of past values, till the current time.
EMA is mainly used to identify trends and to filter out noise. The weight of elements is decreased
gradually over time. This means It gives weight to recent data points, not historical ones. Compared
with SMA, the EMA is faster to change and more sensitive.
α –>Smoothing Factor.
Before we get to know about Arima, first you should understand the below terms
better.
10.2 Partial Auto-Correlation (PACF): PACF is similar to Auto-Correlation Function and is a little
challenging to understand. It always shows the correlation of the sequence with itself with some
number of time units per sequence order in which only the direct effect has been shown, and all
other intermediary effects are removed from the given time series.
plot_acf(df_temperature)
plt.show()
plot_acf(df_temperature, lags=30)
plt.show()
Observation: The previous temperature influences the current temperature, but the significance of
that influence decreases and slightly increases from the above visualization along with the
temperature with regular time intervals.
Plot drop instantly Plot drop instantly You wouldn’t perform any model
Remember that both ACF and PACF require stationary time series for analysis.
This is a simple model, that predicts future performance based on past performance. mainly used for
forecasting, when there is some correlation between values in a given time series and the values
that precede and succeed (back and forth).
An AR model is a Linear Regression model, that uses lagged variables as input. The Linear Regression
model can be easily built using the scikit-learn library by indicating the input to use. Statsmodels
library is used to provide autoregression model-specific functions where you have to specify an
appropriate lag value and train the model. It is provided in the AutoTeg class to get the results, using
simple steps
Key Parameters
• p=past values
• Yt=Function of different past values
• Ert=errors in time
• C=intercept
OUTPUT
predicted=15.893972, expected=16.275000
predicted=15.917959, expected=16.600000
predicted=15.812741, expected=16.475000
predicted=15.787555, expected=16.375000
predicted=16.023780, expected=16.283333
predicted=15.940271, expected=16.525000
predicted=15.831538, expected=16.758333
Test RMSE: 0.617
Observation: Expected (blue) Against Predicted (red). The forecast looks good on
the 4th and the deviation on the 6th day.
AR+I+MA= ARIMA
Step 4: Difference log transform to make as stationary on both statistic mean and variance
Step 5: Plot ACF & PACF, and identify the potential AR and MA model
Step 7: Forecast/Predict the value, using the best fit ARIMA model
Step 8: Plot ACF & PACF for residuals of the ARIMA model, and ensure no more information is left.
Implementation of ARIMA
Already we have discussed steps 1-5, let’s focus on the rest here.
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(df_temperature, order=(0, 1, 1))
results_ARIMA = model.fit()
results_ARIMA.summary()
results_ARIMA.forecast(3)[0]
Output
array([16.47648941, 16.48621826, 16.49594711])
results_ARIMA.plot_predict(start=200)
plt.show()
Process flow (Re-gap)
In recent years, the use of Deep Learning for Time Series Analysis and Forecasting
has been increased to resolve the problem statements, where we couldn’t be
handled using Machine Learning techniques. Let’s discuss briefly.
Recurrent Neural Networks is the most traditional and accepted architecture,
fitment for Time-Series forecasting based problems.
• Input
• Hidden
• Output
Each layer has equal weight and every neuron has to be assigned to fixed time steps. And remember
that every one of them is fully connected with a hidden layer (Input and Output) with the same time
steps and the hidden layers are forwarded and time-dependent in direction.
Components of RNN
Internally weight matrix W is formed by the hidden layer neurons of time t-1 and t+1.
followed by this the hidden-layer with to the output vector y(t) of time t by a V (weight
matrix); all the weight matrices U, W, and V are constant for each time step.
Advantages Disadvantages
It has the special feature that it would remember every each The big challenge is during the
information, so RNN is much useful for time series prediction training period.
Perfect for creating complex patterns from the input time series
Expensive computation cost
dataset.
Fast in prediction/forecasting