Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Squares Function Plotting

Given some data y1,…,yny_1, \dots, y_n that are not all equal, consider the squares function:

S(θ):=∑i=1n(yi−θ)2.\begin{align*} S(\theta) := \sum_{i=1}^n (y_i - \theta)^2. \end{align*}

This function will be minimized at θ^=(y1+⋯+yn)/n\hat{\theta} = (y_1 + \dots + y_n)/n. The code below plots this function for some data y1,…,yny_1, \dots, y_n.

<Figure size 700x500 with 1 Axes>

Next we plot the function S(θ^)/S(θ)S(\hat{\theta})/S(\theta) as a function of θ\theta.

<Figure size 700x500 with 1 Axes>

Below we plot (S(θ^)/S(θ))m\left(S(\hat{\theta})/S(\theta) \right)^m for m≥1m \geq 1. When mm becomes large, this function becomes quite sharply concentrated around θ^\hat{\theta}.

<Figure size 700x500 with 1 Axes>

Dataset One: US Population

    observation_date  POPTHM
0         1959-01-01  175818
1         1959-02-01  176044
2         1959-03-01  176274
3         1959-04-01  176503
4         1959-05-01  176723
..               ...     ...
806       2026-03-01  342627
807       2026-04-01  342680
808       2026-05-01  342746
809       2026-06-01  342822
810       2026-07-01  342909

[811 rows x 2 columns]
<Figure size 640x480 with 1 Axes>

Let us fit the simple linear regression model to this data (with time as covariate).

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 POPTHM   R-squared:                       0.997
Model:                            OLS   Adj. R-squared:                  0.997
Method:                 Least Squares   F-statistic:                 2.794e+05
Date:                Thu, 10 Sep 2026   Prob (F-statistic):               0.00
Time:                        19:35:53   Log-Likelihood:                -7554.3
No. Observations:                 811   AIC:                         1.511e+04
Df Residuals:                     809   BIC:                         1.512e+04
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const       1.746e+05    189.054    923.472      0.000    1.74e+05    1.75e+05
x1           213.2157      0.403    528.562      0.000     212.424     214.008
==============================================================================
Omnibus:                      562.602   Durbin-Watson:                   0.000
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               69.512
Skew:                          -0.398   Prob(JB):                     8.05e-16
Kurtosis:                       1.807   Cond. No.                         938.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

The fitted regression line is plotted below along with the observed data.

<Figure size 640x480 with 1 Axes>

We have seen that the posterior distribution of β0,…,βm\beta_0, \dots, \beta_m is given by:

β0,…,βm∣data∼tm+1(β^,S(β^)n−m−1(XTX)−1,n−m−1)\begin{align*} \beta_0, \dots, \beta_m \mid \text{data} \sim t_{m+1} \left(\hat{\beta}, \frac{S(\hat{\beta})}{n-m-1} (X^T X)^{-1}, n-m-1 \right) \end{align*}

We can generate samples from this posterior distribution and plot the resulting lines on the observed data. This will give us an idea of the uncertainty in the parameters.

const    174585.548486
x1          213.215735
dtype: float64
5851610121.093412
[[ 3.57412307e+04 -6.60651215e+01]
 [-6.60651215e+01  1.62721974e-01]]

This matrix S(β^)n−m−1(XTX)−1\frac{S(\hat{\beta})}{n-m-1} (X^T X)^{-1} is actually calculated as part of the sm.OLS output, and can be obtained as follows:

              const         x1
const  35741.230736 -66.065122
x1       -66.065122   0.162722
[[ 3.57412307e+04 -6.60651215e+01]
 [-6.60651215e+01  1.62721974e-01]]

Now we need to generate observations from the tt-distribution. For this, we shall use an inbuilt function from scipy:

[[174721.3080949     213.12657353]
 [174305.73941779    213.6399766 ]
 [174822.94796507    212.62380814]
 ...
 [174662.70781635    213.3719372 ]
 [174460.98218758    213.5398607 ]
 [174747.83235296    212.89862161]]
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

Dataset Two: Lake Huron Levels

    Unnamed: 0       x
0            1  580.38
1            2  581.86
2            3  580.97
3            4  580.80
4            5  579.79
..         ...     ...
93          94  578.52
94          95  579.74
95          96  579.31
96          97  579.89
97          98  579.96

[98 rows x 2 columns]
<Figure size 640x480 with 1 Axes>
[[ 1.  1.]
 [ 1.  2.]
 [ 1.  3.]
 [ 1.  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.]
 [ 1. 98.]]
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      x   R-squared:                       0.272
Model:                            OLS   Adj. R-squared:                  0.265
Method:                 Least Squares   F-statistic:                     35.95
Date:                Thu, 10 Sep 2026   Prob (F-statistic):           3.55e-08
Time:                        19:42:35   Log-Likelihood:                -150.05
No. Observations:                  98   AIC:                             304.1
Df Residuals:                      96   BIC:                             309.3
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const        580.2020      0.230   2521.398      0.000     579.745     580.659
x1            -0.0242      0.004     -5.996      0.000      -0.032      -0.016
==============================================================================
Omnibus:                        1.626   Durbin-Watson:                   0.439
Prob(Omnibus):                  0.444   Jarque-Bera (JB):                1.274
Skew:                          -0.039   Prob(JB):                        0.529
Kurtosis:                       2.447   Cond. No.                         115.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[[ 5.79986362e+02 -2.16290863e-02]
 [ 5.79651569e+02 -1.97575824e-02]
 [ 5.80205715e+02 -2.35230973e-02]
 ...
 [ 5.80111509e+02 -1.98210127e-02]
 [ 5.80207061e+02 -2.52525258e-02]
 [ 5.80214907e+02 -2.28572556e-02]]
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

Dataset Three: A Simulated Dataset

<Figure size 640x480 with 1 Axes>

Let us fit a line (not a quadratic) to this dataset.

<Figure size 640x480 with 1 Axes>
[[ 1.24847900e+04 -7.45209402e+00]
 [ 9.66084443e+03  5.88415623e+00]
 [ 1.14314746e+04 -4.28814832e+00]
 ...
 [ 1.10088743e+04 -1.72996011e+00]
 [ 1.04394835e+04  1.05566039e+00]
 [ 1.13698106e+04  1.56511066e+00]]
<Figure size 640x480 with 1 Axes>

The correct thing here is to fit a quadratic. This is done by multiple linear regression as shown below.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.989
Model:                            OLS   Adj. R-squared:                  0.989
Method:                 Least Squares   F-statistic:                 1.849e+04
Date:                Thu, 10 Sep 2026   Prob (F-statistic):               0.00
Time:                        19:45:58   Log-Likelihood:                -3325.9
No. Observations:                 400   AIC:                             6658.
Df Residuals:                     397   BIC:                             6670.
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         3.2e+04    149.517    214.033      0.000    3.17e+04    3.23e+04
x1          -319.9986      1.722   -185.840      0.000    -323.384    -316.613
x2             0.7997      0.004    192.309      0.000       0.792       0.808
==============================================================================
Omnibus:                        0.637   Durbin-Watson:                   1.911
Prob(Omnibus):                  0.727   Jarque-Bera (JB):                0.732
Skew:                          -0.004   Prob(JB):                        0.693
Kurtosis:                       2.791   Cond. No.                     2.16e+05
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.16e+05. This might indicate that there are
strong multicollinearity or other numerical problems.
[[ 3.18471308e+04 -3.17976656e+02  7.94430399e-01]
 [ 3.19486063e+04 -3.20504111e+02  8.01512967e-01]
 [ 3.20846821e+04 -3.20623326e+02  8.01345909e-01]
 ...
 [ 3.22390951e+04 -3.21993886e+02  8.03728479e-01]
 [ 3.18593038e+04 -3.18269400e+02  7.95333099e-01]
 [ 3.18704431e+04 -3.19046782e+02  7.98341252e-01]]
<Figure size 640x480 with 1 Axes>

Standard Errors and Intervals in Regression

Consider the simple linear regression model:

yt=β0+β1t+ϵt\begin{align*} y_t = \beta_0 + \beta_1 t + \epsilon_t \end{align*}

that we used in Lecture 2 for the US population dataset.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 POPTHM   R-squared:                       0.997
Model:                            OLS   Adj. R-squared:                  0.997
Method:                 Least Squares   F-statistic:                 2.794e+05
Date:                Thu, 10 Sep 2026   Prob (F-statistic):               0.00
Time:                        19:46:45   Log-Likelihood:                -7554.3
No. Observations:                 811   AIC:                         1.511e+04
Df Residuals:                     809   BIC:                         1.512e+04
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const       1.746e+05    189.054    923.472      0.000    1.74e+05    1.75e+05
x1           213.2157      0.403    528.562      0.000     212.424     214.008
==============================================================================
Omnibus:                      562.602   Durbin-Watson:                   0.000
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               69.512
Skew:                          -0.398   Prob(JB):                     8.05e-16
Kurtosis:                       1.807   Cond. No.                         938.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

The output above reports that the standard error corresponding to the estimate for β1\beta_1 is 0.403 and the 95% interval for β1\beta_1 is [212.424,214.008][212.424, 214.008]. These quantities can also be obtained from the regression output as follows.

const    174585.548486
x1          213.215735
dtype: float64
const    189.053513
x1         0.403388
dtype: float64
                   0              1
const  174214.455222  174956.641750
x1        212.423924     214.007546

We can obtain these manually using the formulae that we derived and check if we are getting exactly the same answers.

[174585.54848609    213.21573493]
const    174585.548486
x1          213.215735
dtype: float64
[189.05351289   0.40338812]
const    189.053513
x1         0.403388
dtype: float64
[[174214.45522237 174956.64174982]
 [   212.42392413    214.00754573]]
                   0              1
const  174214.455222  174956.641750
x1        212.423924     214.007546