How to compare 2 time series data points using Grangers test in ML

This recipe helps you compare 2 time series data points using Grangers test in ML

Recipe Objective

How to compare 2 time series data points using Grangers test?

Granger relation check is employed to see if just once series is going to be helpful to forecast another variable by investigation relation between 2 variables in a very statistic. The strategy could be a probabilistic account of causality; it uses discovered knowledge sets to search out patterns of correlation. One smart issue regarding statistic vector auto regression (VAR) is that we have a tendency to check ‘causality’ in some sense.

How Granger Causality works:

It is supported the concept that if X causes Y, then the forecast of Y supported previous values of Y and also the previous values of X ought to the best lead to the forecast of Y supported previous values of Y alone. Granger relation shouldn't be accustomed check if a lag of Y causes Y. Instead, it's typically used on exogenous (not Y lag) variables solely. In easy terms ‘X is alleged to Granger-cause Y if Y is higher expected victimization the histories of each X and Y than it will by using the history of Y alone.

Assumtions we made while performing Granger Causality Test:- 1. Future values cannot cause the past values. 2. A notably distinct data is contained in cause regarding impact which is able to not be accessible elsewhere

Step 1- Importing Library.

We will import all the Libraries including grangercausalitytests.

from statsmodels.tsa.stattools import grangercausalitytests import pandas as pd import numpy as np

Step 2- Reading California house Dataset.

df = pd.read_csv("/content/sample_data/california_housing_train.csv") df.head()

Step 3- Applying Granger Causality Test.

This test is a X/Y based test. We have to feed two columns thorugh this test.

res = grangercausalitytests(df[['median_income', 'median_house_value']], maxlag=4)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.