
Efficiency Loss: While OLS estimators remain unbiased in the face of heteroskedastic errors, they forfeit their BLUE (Best Linear Unbiased Estimator) status. This means there could be other estimators with lower variance, making OLS estimates less reliable.

Inference Inaccuracy: The standard errors of the regression coefficients, crucial for hypothesis testing and constructing confidence intervals, become biased. This misleads the statistical significance tests, potentially leading to erroneous conclusions about the relationship between variables.
Identifying heteroskedasticity is a critical first step before taking corrective measures. This diagnosis typically involves both visual inspection and formal statistical tests:
 Visual Inspection: Plotting residuals against fitted values or one of the independent variables can reveal patterns indicative of heteroskedasticity. A fan or funnel shape in these plots suggests increasing or decreasing variance of residuals.
 Statistical Tests: Several tests, such as the BreuschPagan and White tests, offer a formal mechanism to detect heteroskedasticity. These tests assess the null hypothesis of homoskedasticity against the alternative of heteroskedasticity, providing a pvalue to guide conclusions.
Upon detecting heteroskedasticity, you can employ several strategies to mitigate its effects:
 Robust Standard Errors (RSE): One of the most straightforward approaches involves adjusting the standard errors of the regression coefficients to account for heteroskedasticity, enabling valid hypothesis testing without altering the original OLS estimates. This is the approach we'll be applying in this blog, using Python.
 Transformation of Variables: Applying transformations to the dependent variable, such as taking logarithms or square roots, can sometimes stabilize the variance of the errors.
 Generalized Least Squares (GLS): This method involves transforming the original equation to "weight" observations differently, effectively neutralizing the problem of unequal variances.
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.stats.diagnostic import het_breuschpagan
from statsmodels.formula.api import ols
# Set seed for reproducibility
np.random.seed(123)
# Generate heteroskedastic data
x = np.linspace(1, 100, 100)
errors = np.random.normal(0, 0.1*x, size=100)
y = 2 + 3*x + errors
# Fit an OLS regression model
X = sm.add_constant(x) # Adds a constant term to the predictor
model = sm.OLS(y, X).fit()
# Summary of the model
print(model.summary())
# Plot residuals vs fitted values to visually check for heteroskedasticity
plt.scatter(model.fittedvalues, model.resid)
plt.axhline(y=0, color='r', linestyle='')
plt.xlabel('Fitted values')
plt.ylabel('Residuals')
plt.title('Residuals vs Fitted Values')
plt.show()
# Perform BreuschPagan test
bp_test = het_breuschpagan(model.resid, model.model.exog)
labels = ['LM Statistic', 'LMTest pvalue', 'FStatistic', 'FTest pvalue']
print(dict(zip(labels, bp_test)))
# Address heteroskedasticity by using robust standard errors
model_robust = model.get_robustcov_results()
print(model_robust.summary())
Beyond its immediate effects on statistical inference, heteroskedasticity signals potential specification errors or opportunities for model improvement. It prompts researchers to revisit their models, considering whether key variables have been omitted, if nonlinear relationships have been inadequately modeled, or if there's an underlying pattern in the data not captured by the current model form.
Heteroskedasticity, with its challenging presence in regression analysis, underscores the importance of diligent model checking and refinement in regression. By understanding heteroskedasticity's implications, diagnosing its presence, and applying appropriate remedies, researchers can enhance the robustness and reliability of their findings.