Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
How to Perform a Breusch-Pagan Test in Python
Next article icon

How to Perform a Breusch-Pagan Test in Python

Last Updated : 02 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Heteroskedasticity is a statistical term and it is defined as the unequal scattering of residuals. More specifically it refers to a range of measured values the change in the spread of residuals. Heteroscedasticity possesses a challenge because ordinary least squares (OLS) regression considers the residuals thrown out from a population having homoscedasticity which means constant variance. If there is a heteroscedasticity present for a regression analysis then the outcome of the analysis cannot be trusted easily.

Breusch-Pagan test is a way to check whether heteroscedasticity exists in regression analysis. A Breusch-Pagan test follows the below hypotheses:

Hypothesis:

  • The null hypothesis (H0): Signifies that Homoscedasticity is present.
  • The alternative hypothesis: (Ha): Signifies that the Homoscedasticity is not present (i.e. heteroscedasticity exists)

Syntax to install the numNumPypy, pandas and statsmodels library:

pip3 install numpy pandas statsmodels

Performing a Breusch-Pegan Test:

Performing a Breusch-Pegan test is a step-by-step process. These have been discussed below. 

Step 1: Import libraries.

The very first step is to import the libraries that we have installed above.

Python3
# Importing libraries import numpy as np import pandas as pd import statsmodels.formula.api as smf 

  

Step 2: Create a dataset.


 

Then we need to create a dataset.


 

Python3
# Create a dataset dataframe = pd.DataFrame({'rating': [92, 84, 87, 82, 98,                                      94, 75, 80, 83, 89],                           'points': [27, 30, 15, 26, 27,                                       20, 16, 18, 19, 20],                           'runs': [5000, 7000, 5102, 8019,                                    1200, 7210, 6200, 9214,                                    4012, 3102],                           'wickets': [110, 120, 110, 80, 90,                                       119, 116, 100, 90, 76]}) 

Step 3: Fit a multiple linear regression model.

The next step is to fit a multiple linear regression model. As an example, we are considering rating as the response variable and points, runs, and wickets as the explanatory variables.

Python3
# Importing libraries import numpy as np import pandas as pd import statsmodels.formula.api as smf  # Create a dataset dataframe = pd.DataFrame({'rating': [92, 84, 87, 82,                                       98, 94, 75, 80,                                       83, 89],                           'points': [27, 30, 15, 26,                                       27, 20, 16, 18,                                      19, 20],                           'runs': [5000, 7000, 5102,                                     8019, 1200, 7210,                                    6200, 9214, 4012,                                     3102],                           'wickets': [110, 120, 110,                                        80, 90, 119,                                       116, 100, 90,                                        76]})  # fit regression model fit = smf.ols('rating ~ points+runs+wickets', data=dataframe).fit() print(fit.summary()) 

Output:

Step 4: Conduct the Breusch-Pagan test.

The next step is to conduct the Breusch-Pagan test in order to determine whether heteroscedasticity is present.

Python3
# Importing libraries import numpy as np import pandas as pd import statsmodels.formula.api as smf from statsmodels.compat import lzip import statsmodels.stats.api as sms  # Creating a dataset dataframe = pd.DataFrame({'rating': [92, 84, 87, 82,                                       98, 94, 75, 80,                                       83, 89],                           'points': [27, 30, 15, 26,                                      27, 20, 16, 18,                                      19, 20],                           'runs': [5000, 7000, 5102,                                     8019, 1200, 7210,                                     6200, 9214, 4012,                                     3102],                           'wickets': [110, 120, 110,                                        80, 90, 119,                                        116, 100, 90,                                        76]})  # Fit the regression model fit = smf.ols('rating ~ points+runs+wickets', data=dataframe).fit()  # Conduct the Breusch-Pagan test names = ['Lagrange multiplier statistic', 'p-value',          'f-value', 'f p-value']  # Get the test result test_result = sms.het_breuschpagan(fit.resid, fit.model.exog)  lzip(names, test_result) 

Output:

Output Interpretation:

Here, the Lagrange multiplier statistic for the test comes out to be equal to 4.364 and the corresponding p-value comes out to be equal to 0.224. Since the p-value is greater than 0.05 so we couldn't reject the null hypothesis. Hence, We do not have enough proof to say that heteroscedasticity is present in the regression model.

How to fix Heteroscedasticity:

In the above example, heteroscedasticity was absent in the regression model. But for the case when heteroscedasticity actually exists then there are three ways to fix this:

  • Transform the dependent variable: We can alter the dependent variable using some technique. For example, we can take the log of the dependent variable.
  • Redefine the dependent variable:  We can redefine the dependent variable. For example, using a rate for the dependent variable than the flawed value.
  • Use weighted regression: The last way is to use weighted regression. In this type of regression, the weight is assigned to each data point on the basis of the variance of its fitted value. Using proper weights can eliminate the problem of heteroscedasticity.

Next Article
How to Perform a Breusch-Pagan Test in Python

B

bhuwanesh
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Python-numpy
  • Python-pandas
  • Python-statsmodels
Practice Tags :
  • python

Similar Reads

    How to Perform a Breusch-Pagan Test in R
    The Breusch-Pagan test is a statistical test used to detect heteroscedasticity in a regression model. Heteroscedasticity occurs when the variance of the errors is not constant across all levels of the independent variables, which can lead to inefficient estimates and affect the reliability of hypoth
    3 min read
    How to Perform an F-Test in Python
    In statistics, Many tests are used to compare the different samples or groups and draw conclusions about populations. These techniques are commonly known as Statistical Tests or hypothesis Tests. It focuses on analyzing the likelihood or probability of obtaining the observed data that they are rando
    10 min read
    How to Perform Grubbs’ Test in Python
    Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing  In this article, we will be discussing the different approaches to perform Grubbs’ Test in Python programming language.  Grubbs’ Test is also known as the maximum normalized residual test or extreme studentized deviate test is
    3 min read
    How to Perform a Brown – Forsythe Test in Python
    Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing  In this article, we will be looking at the approach to perform a brown-Forsythe test in the Python programming language. Brown–Forsythe test is a statistical test for the equality of group variances based on performing an Anal
    4 min read
    How to Perform Dunn’s Test in Python
    Dunn's test is a statistical procedure used for multiple comparisons following a Kruskal-Wallis test. Here's a breakdown of what it does and when it's used: Table of Content Dunn’s TestWhat is the Kruskal-Wallis test?Key points about Dunn's testHow to Perform Dunn’s Test with PythonStep-by-Step Guid
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences