Time Series Analysis In Python

Time series analysis

WHY TIME SERIES ANALYSIS?

A time series analysis helps in understanding how the pattern of behaviour or a factor changes over time. Industries and organizations have been using Time Series data, which is information collected over a regular interval of time, in their operations to understand growth in revenue, cost, profit etc. We can use examples like Earning Per Share (EPS), retail demand over time to see growth or decline/downturn in business.

Time Series data can be annual (retail sale every year), monthly (subscribers in YouTube every month), quarterly (Earning Per Share of a company every quarter), sometimes weekly or hourly (comparing actual temperature and “feels like “temperature per hour). This article will cover various components on time series data using python programming language.

COMPONENTS OF TIME SERIES

  • Secular Trends – Market activities unfolding over long-term horizon with a tendency of data to grow, decline, or remain constant.
  • Seasonal Variation – A variable element in the time-series analysis of forecasting, and refers to the phenomenon where the production changes on a certain seasonal trend depending on the characteristics of the product.
  • Cyclical Fluctuations – It describes oscillations that occur over long periods about the secular trend line or curve of a time series
  • Irregular Variations – Shows the variation which cannot be described by trend, cycle movement, and seasonal variation. It generally refers to the variation which incidentally occurs in a number of factors difficult to be identified and their relationship with each other. These are short term fluctuations that are neither systematic nor predictable.

SEASONAL VARIATION

Seasonal variations are periodic movements which occurs on regular intervals of time. These are short term fluctuations occurring regularly like yearly, half – yearly, quarterly, monthly or weekly. Since these variations are repeated in short period of time like 6 months, 3 months, week, day etc. so they can be predicted more accurately.

Time-series analysis can be done using line chart in python programming language. The following libraries can be used in python :

  • pandas
  • numpy
  • plotly.express

The above graph is a time series analysis based on the data collected by calculating the sale of beers of a company on a monthly basis for the years 1975 – 1990.

The above graph is a time series analysis for Johnson & Johnson, collected on the basis of their quarterly Earnings Per Share (EPS) for the years 1960 – 1980.

Python code

import pandas as pd

import numpy as np

import plotly.express as px

fig_bs = px.line(data_BS, x=”year_mo”, y=”beersales”)

fig_bs

Note: fig_bs = name of the dataset, “year_mo” & “beersales” = name of the variables, px=plotly.express library for visualisation.

By focusing on the graph, we can see that there is an increasing trend from 1975 – 1980. And the trend is flat in the following years. We can also observe that sale of beer increases from April to August and decreases from September to March in a year. So, we can say that there is a rise in sale of beer in the summer season and fall in the sale in winter season hence there is seasonality on a regular interval every year.

SECULAR TREND

A trend exists when there is a long-term increase or decrease in the data. It does not have to be linear. Sometimes a trend is referred to as “changing direction” when it might go from an increasing trend to a decreasing trend.

The above graph is a time series analysis for Johnson & Johnson, collected on the basis of their quarterly Earnings Per Share (EPS) for the years 1960 – 1980. Python code:

import pandas as pd

import numpy as np

import plotly.express as px

fig1 = px.line(data_jj, x=”yr_qr”, y=”EPS”)

fig1

Note: data_jj = name of the dataset, “yr_qr” & EPS = name of the variables under consideration, px = plotly.express library for visualisation.

 

By focusing on the graph, we can say that over the years the Earning Per Share have increased thus showing us an Increasing Trend in EPS. We can also observe that every fourth quarter there is a fall in the Earning Per Share which shows a seasonal variation on a regular interval for that quarter in every year.

 

This was a basic analysis on Time Series using Python. However, there are steps like stationarity, autocorrelation, which are applied for further analysis for uncovering trends and seasonality. Models like ARIMA, ARIMAX, SARIMA, SARIMAX can be used for forecasting or predicting future values. Time Series analysis and forecasting are techniques that almost every Data Scientist uses in their career.

Facebook
Twitter
Pinterest
Email