Sitemap
Syed Mohd Nazar Ali

Syed Mohd Nazar Ali

Data Analyst -Data Science| Machine Learning Enthusiast | Statistics , Masters in Computer Science | Aligarh Muslim University.

Time Series Analysis Using Python

5 min readFeb 5, 2023

--

This topic is intended for data scientists or machine learning (ML) engineers who want to level up their time series analysis skills by learning new and advanced techniques from the ML world.

Press enter or click to view image in full size
Time Series Analysis Fig-1.

Introduction

To keep it simple, a time series is a set of observations taken sequentially in time. If we keep taking the same observation at different points in time, we will get a time seriesA few examples of time series are the weekly closing price of a stock that you follow, daily rainfall or snow in your city, or hourly readings of your heartbeat from your smartwatch.

What is time series?

Time series analysis is a specific way of analyzing a sequence of data points collected over an interval of time. In time series analysis, analysts record data points at consistent intervals over a set period of time rather than just recording the data points intermittently or randomly.

Types of time series

There are two types of time series data, as outlined here:

Regular time series: This is the most common type of time series where we have observations coming in at regular intervals of time, such as every hour or every month.

Irregular time series: There are a few time series where we do not have observations at a regular interval of time. For example, consider we have a sequence of readings from lab tests of a patient. We see an observation in the time series only when the patient heads to the clinic and carries out the lab test, and this may not happen in regular intervals of time.

Main areas of application for time series analysis:

There are broadly three important areas of application for time series analysis, outlined as follows:

Time series forecasting: Predicting the future values of a time series, given the past values — for example, predict the next day’s temperature using the last 5 years of temperature data.

Time series classification: Sometimes, instead of predicting the future value of the time series, we may also want to predict an action based on past values. For example, given a history of an electroencephalogram (EEG; tracking electrical activity in the brain) or an electrocardiogram (EKG; tracking electrical activity in the heart), we need to predict whether the result of an EEG or an EKG is normal or abnormal.

Interpretation and causality: Understand the whats and whys of the time series based on the past values, understand the interrelationships among several related time series, or derive causal inference based on time series data.

Data-generating process (DGP)

We saw that time series data is a collection of observations made sequentially along the time dimension. Any time series is, in turn, generated by some kind of mechanism.

Get Syed Mohd Nazar Ali’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, time series data of daily shipments of your favorite chocolate from the manufacturing plant is affected by a lot of factors such as the time of the year, the holiday season, the availability of cocoa, the uptime of the machines working on the plant, and so on. In statistics, this underlying process that generated the time series is referred to as the DGP. Often, time series data is generated by a stochastic process.

Stationary and non-stationary time series

A time series whose statistical properties, such as mean, variance, etc., remain constant over time, are called a stationary time series.

In other words, a stationary time series is a series whose statistical properties are independent of the point in time at which they are observed. A stationary time series has a constant variance and it always returns to the long-run mean.

Press enter or click to view image in full size
Stationary Time Series

A time series whose statistical properties change over time is called a non-stationary time series. Thus a time series with a trend or seasonality is non-stationary in nature. This is because the presence of trend or seasonality will affect the mean, variance and other properties at any given point in time. In statistics, there is a scary name for this phenomenon — heteroscedasticity.

Press enter or click to view image in full size
Non-Stationary Time Series

What can we forecast?

There is another aspect of time series forecasting that we have to understand — the predictability of a time series. The most basic assumption when we forecast a time series is that the future depends on the past. But not all time series are equally predictable.

Timeseries forecasting in simple words means to forecast or to predict the future value(eg-stock price) over a period of time. There are different approaches to predict the value, consider an example there is a company XYZ records the website traffic in each hour and now wants to forecast the total traffic of the coming hour. If I ask you what will your approach to forecasting the upcoming hour traffic?

Forecasting terminology

There are a few terminologies that will help you follow content as well as other literature on time series. These are described in more detail here:

Forecasting is the prediction of future values of a time series using the known past values of the time series and/or some other related variables. This is very similar to prediction in ML where we use a model to predict unseen data.

Multivariate forecasting Multivariate time series consist of more than one time series variable that is not only dependent on its past values but also has some dependency on the other variables. For example, a set of macroeconomic indicators such as gross domestic product (GDP), inflation, and so on of a particular country can be considered as a multivariate time series. The aim of multivariate forecasting is to come up with a model that captures the interrelationship between the different variables along with its relationship with its past and forecast all the time series together in the future.

Explanatory forecasting In addition to the past values of a time series, we might use some other information to predict the future values of a time series. For example, for predicting retail store sales, information regarding promotional offers (both historical and future ones) is usually helpful. This type of forecasting, which uses information other than its own history, is called explanatory forecasting.

Backtesting Setting aside a validation set from your training data to evaluate your models is a practice that is common in the ML world. Backtesting is the time series equivalent of validation, whereby you use the history to evaluate a trained model. We will cover the different ways of doing validation and cross-validation for time series data later.

In-sample and out-sample Again, drawing parallels with ML, in-sample refers to training data and out-sample refers to unseen or testing data. When you hear in-sample metrics, this is referring to metrics calculated on training data, and out-sample metrics is referring to metrics calculated on testing data.

--

--

Syed Mohd Nazar Ali
Syed Mohd Nazar Ali

Written by Syed Mohd Nazar Ali

Data Analyst -Data Science| Machine Learning Enthusiast | Statistics , Masters in Computer Science | Aligarh Muslim University.