Skip to content

Heteroskedasticity: Breusch-Pagan and White Tests in Python Videos

Last Update: March 22, 2022

My online video tutorials are hosted at YouTube channel.

For learning this concept, you can view my online video tutorials: Heteroskedasticity: Breusch-Pagan and White Tests in Python (Spyder) and Heteroskedasticity: Breusch-Pagan and White Tests in Python (Jupyter).

Videos Code

1. Package

import statsmodels.api as sm
import statsmodels.formula.api as smf
import statsmodels.tools.tools as smt
import statsmodels.stats.diagnostic as smd

2. Data

houseprices_object = sm.datasets.get_rdataset(dataname="HousePrices",
                                              package="AER", cache=True)
houseprices = houseprices_object.data
print(houseprices.iloc[:, 0:3].head())
print(houseprices_object.__doc__)

3. Model

mlr = smf.ols(formula="price ~ lotsize + bedrooms", 
              data = houseprices).fit()

4. Heteroskedasticity

ivar = houseprices.iloc[:, 1:3]
ivarc = smt.add_constant(data=ivar, prepend=True)

Breusch-Pagan Test

\hat{residuals^2} = \hat{\gamma}_{0} + \hat{\gamma}_{1}*lotsize + \hat{\gamma}_{2}*bedrooms

bptest = smd.het_breuschpagan(resid=mlr.resid, exog_het=ivarc)
print("lm:", bptest[0], "lm_pvalue:", bptest[1])

White Test (Cross Terms)

\hat{residuals^2} = \hat{\gamma}_{0} + \hat{\gamma}_{1}*lotsize + \hat{\gamma}_{2}*(lotsize^2) + \hat{\gamma}_{3}*(lotsize*bedrooms) + \hat{\gamma}_{4}*bedrooms + \hat{\gamma}_{5}*(bedrooms^2)

wtest = smd.het_white(resid=mlr.resid, exog=ivarc)
print("lm:", wtest[0], "lm_pvalue:", wtest[1])

Courses

My online courses are hosted at Teachable website.

For more details on this concept, you can view my Linear Regression in Python Course.

My online courses are closed for enrollment.
+