│ │ │ │ │ │
│ │ │

Weighted Least Squares

│ │ │
│ │ │ -
[1]:
│ │ │ +
[ ]:
│ │ │  
│ │ │
│ │ │
%matplotlib inline
│ │ │  
│ │ │
│ │ │
│ │ │
│ │ │ -
[2]:
│ │ │ +
[ ]:
│ │ │  
│ │ │
│ │ │
import matplotlib.pyplot as plt
│ │ │  import numpy as np
│ │ │  import statsmodels.api as sm
│ │ │  from scipy import stats
│ │ │  from statsmodels.iolib.table import SimpleTable, default_txt_fmt
│ │ │ @@ -89,15 +89,15 @@
│ │ │  

Model assumptions:

│ │ │
    │ │ │
  • Misspecification: true model is quadratic, estimate only linear

  • │ │ │
  • Independent noise/error term

  • │ │ │
  • Two groups for error variance, low and high variance groups

  • │ │ │
│ │ │
│ │ │ -
[3]:
│ │ │ +
[ ]:
│ │ │  
│ │ │
│ │ │
nsample = 50
│ │ │  x = np.linspace(0, 20, nsample)
│ │ │  X = np.column_stack((x, (x - 5) ** 2))
│ │ │  X = sm.add_constant(X)
│ │ │  beta = [5.0, 0.5, -0.01]
│ │ │ @@ -111,83 +111,42 @@
│ │ │  
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │

WLS knowing the true variance ratio of heteroscedasticity

│ │ │

In this example, w is the standard deviation of the error. WLS requires that the weights are proportional to the inverse of the error variance.

│ │ │ -
│ │ │ -
[4]:
│ │ │ +
│ │ │ +
[ ]:
│ │ │  
│ │ │
│ │ │
mod_wls = sm.WLS(y, X, weights=1.0 / (w ** 2))
│ │ │  res_wls = mod_wls.fit()
│ │ │  print(res_wls.summary())
│ │ │  
│ │ │
│ │ │
│ │ │ -
│ │ │ -
│ │ │ -
│ │ │ -
│ │ │ -
│ │ │ -                            WLS Regression Results
│ │ │ -==============================================================================
│ │ │ -Dep. Variable:                      y   R-squared:                       0.927
│ │ │ -Model:                            WLS   Adj. R-squared:                  0.926
│ │ │ -Method:                 Least Squares   F-statistic:                     613.2
│ │ │ -Date:                Sun, 10 Aug 2025   Prob (F-statistic):           5.44e-29
│ │ │ -Time:                        13:13:47   Log-Likelihood:                -51.136
│ │ │ -No. Observations:                  50   AIC:                             106.3
│ │ │ -Df Residuals:                      48   BIC:                             110.1
│ │ │ -Df Model:                           1
│ │ │ -Covariance Type:            nonrobust
│ │ │ -==============================================================================
│ │ │ -                 coef    std err          t      P>|t|      [0.025      0.975]
│ │ │ -------------------------------------------------------------------------------
│ │ │ -const          5.2469      0.143     36.790      0.000       4.960       5.534
│ │ │ -x1             0.4466      0.018     24.764      0.000       0.410       0.483
│ │ │ -==============================================================================
│ │ │ -Omnibus:                        0.407   Durbin-Watson:                   2.317
│ │ │ -Prob(Omnibus):                  0.816   Jarque-Bera (JB):                0.103
│ │ │ -Skew:                          -0.104   Prob(JB):                        0.950
│ │ │ -Kurtosis:                       3.075   Cond. No.                         14.6
│ │ │ -==============================================================================
│ │ │ -
│ │ │ -Notes:
│ │ │ -[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
│ │ │ -
│ │ │ -
│ │ │
│ │ │