VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/introduction-to-time-series-modeling-with-arima/

⇱ Introduction to Time series Modeling With -ARIMA - Analytics Vidhya


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Introduction to Time series Modeling With -ARIMA

snehal_bm Last Updated : 13 Jul, 2021
5 min read

This article was published as a part of the Data Science Blogathon

Introduction

Often wondered if we could know what would the price of bitcoin be 6 months from now or how would your favourite stocks look like in a week, now you can predict all of these with time series modelling. One of the most commonly used data science applications is time series forecasting. As the name suggests, a time series forecast is predicting the future value of a variable by looking into the past history with the help of some additional features which are likely to affect those variables. The algorithms are designed in such a way that it looks at the trends of the past and forecasts the future values.

There are dozens of algorithms which can do time series forecast. There are the basic ones like Auto Regression, Moving average, ARMA, ARIMA, simple exponential smoothing, Holt Winter’s method, linear regression to advanced ones like the multiple linear regression, LSTM (Long Short Term Memory), Artificial neural networks and many more. Based on the data set and the supporting features we can make an informed decision and finalise the model.

Image Source: Google Images https://www.analyticssteps.com/blogs/introduction-time-series-analysis-time-series-forecasting-machine-learning-methods-models

Let us look into time series modelling using ARIMA.

What is ARIMA?

Autoregressive integrated moving average or popularly known as ARIMA is a very widely used time series forecasting technique. Before starting prediction with ARIMA let us understand the concept of stationary. A time-series prediction is done only if the dataset is stationary. A dataset is said to be stationary if its mean and variance remains constant over time. A stationary dataset does not have trend or seasonality.

When forecasting using time series models, we assume that each data point is independent of the other and this can be confirmed if the series is stationary. To check if a time series is stationary or not, just plot the observations against time and you can implement dicker-fuller’s test to check.

We can use the differencing technique to make the time series stationary, just subtract the previous observation for the next observation. This is 1st degree differencing. If the series is still not stationary you can do the differencing again until the series becomes stationary. Generally, 1 or 2 degrees of difference is sufficient to make the series stationary.

Now that we know that the series is stationary, let look at the ARIMA model in detail. The ARIMA algorithm is made of the following components.

The AR stands for Auto Regression which is denoted as P, the value of P determines how the data is regressed on its past values.

The I stands for Integrated or the differencing component which is denoted as d, the value of d determines the degree of difference used to make the series stationary.

The MA stands for Moving Average which is denoted as q, the values of q determines the outcome of the model depends linearly on the past observations and the same goes for the errors in forecasting as they also vary linearly.

ARIMA model in Python

Now that we have an understanding of the ARIMA model, let us try to implement it with the mask sales dataset in April 2020 and predict the sales for the next few days.

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
from math import sqrt
mask_sale = pd.read_excel('Book1.xlsx',header=0, parse_dates=[0], index_col=0, squeeze=True)
#result = seasonal_decompose(mask_sale['no_of_mask'],model ='multiplicative')
print(mask_sale.head())
mask_sale.plot()
plt.show()

Let’s divide the data into a training set and test set so that we can train the model on 70% of the data observations and test it on the remaining 30% of the data.

X = mask_sale.values


size = int(len(X) * 0.7)
train, test = X[0:size], X[size:len(X)]
history = [x for x in train]
predictions = list()
# walk-forward validation
for t in range(len(test)):
 model = ARIMA(history, order=(5,1,0))
 model_fit = model.fit()
 output = model_fit.forecast()
 yhat = output[0]
 predictions.append(yhat)
 obs = test[t]
 history.append(obs)
 print('predicted=%f, expected=%f' % (yhat, obs))
# evaluate forecasts
rmse = sqrt(mean_squared_error(test, predictions))
print('Test RMSE: %.3f' % rmse)
# plot forecasts against actual outcomes
plt.plot(test)
plt.plot(predictions, color='red')
plt.show()

Other times series models…

There are numerous time series models which can be used for forecasting time series data but the choice of the model totally depends on the problem statement. The ARIMA modelling technique which we looked at in this blog is the simple time series technique that makes the predictions without taking into consideration other factors which might be affecting our dependent variable.

The other advanced time series forecasting techniques like multiple linear regression has a dependent variable (our variable of interest) and many other dependent variables which influence the dependent variable. For example in house price dataset, the number of bedrooms or size od rooms, number of bathrooms, age of the house, etc are the independent variable or while predicting the weather, pressure, humidity, wind speed, temperature etc will be the independent variables which influence the sales of the houses. But never forget that not all independent variables will influence the dependent variables, so to make sure that the added independent variable is adding value to our model, just check the r squared and the adjusted r squared of these variables. If the adjusted r squared is less than the r squared then the variable is adding values to our model else we can ignore the other variables.

There are deep learning time series forecasting techniques like recurrent neural networks, long short term memory, gated recurrent unit etc which can overcome the shortcomings of the traditional forecasting techniques. The accuracy realised with these techniques is much higher compared to the traditional algorithms.

Footnote

It is always recommended to look at the data set and the problem statement before narrowing down on any time series modelling technique. Also, it’s better to explore more than just one model and then decide on the modelling techniques by looking at the accuracy of the prediction.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Login to continue reading and enjoy expert-curated content.

Free Courses

Exploratory Data Analysis with Python & GenAI

Learn EDA with Python: Transform data into insights using PandasAI & more.

Data Science Course

Build a powerful 2026-ready data science resume using AI tools.

No Code Predictive Analytics with Orange

No-code AI course for business pros with real-world ML use cases.

Adaptive Email Agents with DSPy

Build adaptive email agents with DSPy using context and smart learning.

Introduction to AI & ML

AI & ML are transforming industries. Learn their impacts in this course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner