Machine Learning & Signals Learning
7 Uni-Variate Linear Least-Squares
The goal of this chapter is to define and discuss uni-variate linear least-squares (LS) for a linear model.
Note that LS is also termed linear regression. Uni-variate LS is also referred to as a linear trend-line.
7.1 Uni-variate Linear LS
7.1.1 Definitions
Dataset: A random experiment produces a dataset1 of \(M\) paired observations \(\left \{x_k,y_k\right \}_{k=1}^M\).
ML Model: The assumed model underlying the dataset is
\(\seteqnumber{0}{7.}{0}\)\begin{equation} y = h(x) + \epsilon , \end{equation}
where \(h(x)\) is a deterministic function and \(\epsilon \) is zero-mean noise.
The goal is to propose and apply the model,
\(\seteqnumber{0}{7.}{1}\)\begin{equation} \hat {y}_k = f(x_k) \end{equation}
that provide the most appropriate results with relation to \(y_k\).
Challenge: there are many possible \(f(\cdot )\) options.
Error: The model error is
\(\seteqnumber{0}{7.}{2}\)\begin{equation} e_k = y_k - \hat {y}_k \end{equation}
Loss
Loss function: Loss (or cost) function \(\loss (y_k,\hat {y}_k)\) is some distance metric between \(y_k\) and \(\hat {y}_k\). The optimal model parameters of the applied model are found by minimizing a loss function.
Common examples of loss functions include:
-
• the sum of squared errors (SSE),
\(\seteqnumber{0}{7.}{3}\)\begin{equation} \label {eq-sse-loss-sum} \SSE = \sum _{k=1}^{M} e_k^2, \end{equation}
-
• mean-square error (MSE) or (biased) error variance
\(\seteqnumber{0}{7.}{4}\)\begin{equation} \label {eq-mse-loss-sum} \MSE = \frac {1}{M}\sum _{k=1}^{M} e_k^2 = \frac {1}{M}\SSE = s_e^2 \end{equation}
-
• and root-mean-square error (RMSE),
\(\seteqnumber{0}{7.}{5}\)\begin{equation} \RMSE = \sqrt {\MSE }. \end{equation}
Because constant multiplication factors and monotonic transforms do not affect the location of the minimum, all these loss functions share the same optimal parameters:
\(\seteqnumber{0}{7.}{6}\)\begin{equation} \label {eq-optimization-definition} \begin{aligned} w_0,w_1 &= \arg \min \limits _{w_0,w_1} \SSE \\ &= \arg \min \limits _{w_0,w_1}\MSE \\ &= \arg \min \limits _{w_0,w_1} \RMSE . \end {aligned} \end{equation}
Metric: A quantifiable measure used to evaluate how well a model is performing.
For example, the of RMSE is significantly easier to interpret due to same values range and measurement units as an original dataset, than MSE or SSE.
It is easy to confuse metrics with loss functions, but they serve different purposes:
-
• Loss function is used by the algorithm during training to learn optimal model parameters.
-
• Metric is used by humans to evaluate the model’s performance.
Summary
-
1. Select model \(f(\cdot )\)
-
2. Find model parameters by the minimization of the loss function, \(\loss \)
-
3. Use metrics to evaluate model performance.
7.1.2 Mean and Variance
A special case of the linear model is one of the form:
\(\seteqnumber{0}{7.}{7}\)\begin{equation} \hat {y} = w_0 \end{equation}
The corresponding MSE loss (7.5) function is
\(\seteqnumber{0}{7.}{8}\)\begin{equation} \label {eq-ls-one-coeff} \loss (w_0) = \frac {1}{M}\sum _{k=1}^M (y_k-w_0)^2, \end{equation}
with the related minimum at the mean of \(y_k\) values,
\(\seteqnumber{0}{7.}{9}\)\begin{equation} \label {eq-ls-one-coeff-mse} w_0 = \frac {1}{M}\sum _{k=1}^M y_k = \bar {y} \end{equation}
-
Proof. To find the minimum,
\(\seteqnumber{0}{7.}{10}\)\begin{equation} \frac {d}{dw_0}\loss (w_0) = 2\frac {1}{M}\sum _{k=1}^M (y_k-w_0)(-1) = 0 \end{equation}
The corresponding MSE of the model (substituting (7.10) into (7.9)) is
\(\seteqnumber{0}{7.}{11}\)\begin{equation} \MSE = \frac {1}{M} \sum _{k=1}^M\left (y_k - \bar {y}\right )^2 =s_y^2 \end{equation}
which is the sample variance of the \(y_k\) values 2.
To summarize, the best predictor is the sample mean, \(\hat {y} = \bar {y}\), with \(\MSE =s_y^2\).
2 This variance expression is called biased and is used throughout this chapter (see Sec. 5.1)
7.1.3 Linear Model
The example of linear model is
\(\seteqnumber{0}{7.}{12}\)\begin{equation} \hat {y}_k = f(x_k;w_0,w_1)=w_0+w_1 x_k, \end{equation}
where \(w_0\) and \(w_1\) are the model parameters.
The corresponding linear model error is
\(\seteqnumber{0}{7.}{13}\)\begin{equation} e_k = y_k-w_0-w_1x_k. \end{equation}
The minimization of the SSE loss function ((7.4) and Fig. 7.1),
\(\seteqnumber{0}{7.}{14}\)\begin{equation} \loss (w_0,w_1)=\sum _{k=1}^{M}\bigl (y_k-w_0-w_1x_k\bigr )^2, \end{equation}
is performed by setting the partial derivatives of \(\loss \) to zero:
\(\seteqnumber{0}{7.}{15}\)\begin{equation} \begin{cases} \dfrac {\partial }{\partial w_0} \loss (w_0,w_1) = 0\\[10pt] \dfrac {\partial }{\partial w_1} \loss (w_0,w_1) = 0 \end {cases} \end{equation}
which yields:
\(\seteqnumber{0}{7.}{16}\)\begin{equation} \begin{cases} \displaystyle 2\sum _{k=1}^M \left (y_k - w_0 - w_1x_k\right )\cdot (-1) = 0\\[10pt] \displaystyle 2\sum _{k=1}^M \left (y_k - w_0 - w_1x_k\right )\cdot (-x_k) = 0. \end {cases} \end{equation}
Finally, with some basic algebra:
\(\seteqnumber{0}{7.}{17}\)\begin{equation} \label {eq-ls-normal-eq} \begin{cases} w_0 M + w_1\displaystyle \sum _{k=1}^{M}x_k = \displaystyle \sum _{k=1}^{M}y_k, \\[6pt] w_0 \displaystyle \sum _{k=1}^{M}x_k + w_1 \displaystyle \sum _{k=1}^{M}x_k^{2} = \displaystyle \sum _{k=1}^{M}x_k y_k. \end {cases} \end{equation}
This system of equations is termed the normal equations.
7.2 Covariance & Correlation Coefficient
-
Goal: To rewrite the normal equations (7.18) using statistical terms.
7.2.1 Sample Covariance
The (biased) sample covariance between \(x_1,\dots ,x_M\) and \(y_1,\dots ,y_M\) is given by
\(\seteqnumber{0}{7.}{18}\)\begin{equation} s_{xy} = \frac {1}{M}\sum _{k=1}^M (x_k - \bar x)\,(y_k - \bar y) \end{equation}
The sign and magnitude of the sample covariance \(s_{xy}\) indicate the direction and strength of the linear relationship between \(x\) and \(y\):
-
• \(s_{xy}>0\): As \(x\) increases, \(y\) tends to increase (most products \((x_i - \bar x)(y_i - \bar y)\) are positive).
-
• \(s_{xy}<0\): As \(x\) increases, \(y\) tends to decrease.
-
• \(s_{xy}\approx 0\): No linear association.
7.2.2 Correlation Coefficient
The sample Pearson correlation coefficient between \(\bx \) and \(\by \) is the normalized (dimensionless) covariance:
\(\seteqnumber{0}{7.}{19}\)\begin{equation} \begin{aligned} r_{xy} &=\frac {s_{xy}}{s_x\,s_y}\\ &= \frac {\displaystyle \sum _{k=1}^M (x_k - \bar x)\,(y_k - \bar y)} {\displaystyle \sqrt {\sum _{k=1}^M (x_k - \bar x)^2} \;\sqrt {\sum _{k=1}^M (y_k - \bar y)^2}} \end {aligned} \end{equation}
-
• Range: \(-1 \le r_{xy} \le +1\).
-
• \(r_{xy} > 0\): positive linear association (as \(x\) increases, \(y\) tends to increase).
-
• \(r_{xy} < 0\): negative linear association (as \(x\) increases, \(y\) tends to decrease).
-
• \(r_{xy} = 0\): no linear association (a nonlinear relationship may still exist).
-
• \(|r_{xy}| = 1\): perfect linear relationship (\(y_k = a + bx_k\) for all \(k\)).
Note that there is no difference between biased and unbiased definitions for \(r_{xy}\), since the corresponding coefficients cancel out.
-
Example 7.1: Fig. 7.2 shows the results of four two-dimensional experiments. Rank them from the highest to the lowest \(\abs {r_{xy}}\), and justify the ranking without computing anything.
-
Solution: Two cues decide the ranking:
-
• The straighter the cloud of points, the closer \(\abs {r_{xy}}\) is to one.
-
• Among equally straight clouds, the tighter one has the larger \(\abs {r_{xy}}\).
This gives the order (b), (d), (c), (a). The exact values cannot be read off the plots; they are
-
• (b) \(r_{xy}=0.985\), an almost exact straight line;
-
• (d) \(r_{xy}=0.908\), monotone but visibly curved;
-
• (c) \(r_{xy}=0.615\), monotone only over part of the range;
-
• (a) \(r_{xy}=-0.019\).
Panel (a) is the important one. Its points lie on a parabola, so \(y\) is an exact function of \(x\) and the two are as related as they could be, yet \(r_{xy}\) is essentially zero. The parabola is symmetric about \(x=0\): it falls for negative \(x\) and rises for positive \(x\), and the two halves contribute products \((x_k-\bar x)(y_k-\bar y)\) of opposite sign that cancel. The correlation coefficient measures the linear part of a relation and nothing else.
-
-
7.2.3 Normal Equations with Statistical Terms
It is numerically convenient to rewrite the normal equations (7.18) in terms of sample moments
\(\seteqnumber{0}{7.}{20}\)\begin{equation} \label {eq-lr-univariate-coeff-stat} \begin{aligned} w_1&=\frac {s_{xy}}{s_x^2}= r_{xy}\,\frac {s_y}{s_x}\nonumber \\ &=\frac {\sum _k(x_k-\bar x)(y_k-\bar y)}{\sum _k (x_k-\bar x)^2},\\ w_0&=\bar y-w_1\bar x \nonumber \\ &= \bar y-\frac {s_{xy}}{s_x^2}\bar x, \end {aligned} \end{equation}
and the predictive form
\(\seteqnumber{0}{7.}{20}\)\begin{equation} \hat {y}= \bar y + w_1\bigl (x-\bar x\bigr ). \end{equation}
Note that these expressions are independent of whether biased or unbiased definitions are used for \(s_{xy}\) and \(s_x\), since the corresponding normalization factors cancel out.
-
• A valid solution requires \(s_x\neq 0\), i.e. variability in \(x_i\ne \mathrm {const}\).
-
• If both variables are centered (\(\bar x=\bar y=0\)), then (7.21) reduces to
\(\seteqnumber{0}{7.}{21}\)\begin{equation} \label {eq-lr-univariate-coeff-stat-normalized} \begin{aligned} w_0&=0,\\ w_1 &= \frac {\sum _k x_k y_k}{\sum _k x_k^2} \end {aligned} \end{equation}
7.3 Metrics (R2, MSE)
The performance of a model is quantified by a performance metric, which is not necessarily the same as the loss function.
7.3.1 Coefficient of Determination (R2)
To emphasize the difference between loss and metrics, the following example of an LR metric is provided. The coefficient of determination, denoted \(R^2\) (R-squared), is based on the relation:
\(\seteqnumber{0}{7.}{22}\)\begin{equation} \label {eq-sst-ssr-sse} \underbrace {\sum _{k=1}^{M}(y_k-\bar {y})^2}_{\SST } = \underbrace {\sum _{k=1}^{M}(\hat {y}_k-\bar {y})^2}_{\SSR } + \underbrace {\sum _{k=1}^{M}e_k^2}_{\SSE }, \end{equation}
where:
-
• SST: Total sum of squares.
-
• SSR: Sum of squares due to regression.
-
• SSE: Sum of squared errors (or residual sum of squares).
The \(R^2\) metric is defined as:
\(\seteqnumber{0}{7.}{23}\)\begin{equation} R^2 =\frac {\SSR }{\SST } =1 - \frac {\SSE }{\SST }, \end{equation}
providing a unitless goodness-of-fit measure that shares an intuitive \(\,[0,1]\) range:
-
• \(R^2=1\): Perfect fit.
-
• \(R^2=0\): The model is no better than predicting the mean, \(\hat {y}_i = \bar y\).
-
• \(R^2<0\): The model performs worse than the mean (possible for machine learning models on test data).
-
Proof. To proof (7.23), observe that
\[ y_k - \bar y \;=\; (\hat y_k - \bar y) + (y_k - \hat y_k) \;=\; (\hat y_k - \bar y) + e_k. \]
Hence,
\[ \begin {aligned} \sum _{k=1}^M (y_k - \bar y)^2 &= \sum _{k=1}^M \bigl [(\hat y_k - \bar y) + e_k\bigr ]^2\\ &= \sum _{k=1}^M (\hat y_k - \bar y)^2 \;+\;\sum _{k=1}^M e_k^2 \;+\;2\sum _{k=1}^M (\hat y_k - \bar y)\,e_k. \end {aligned} \]
It remains to show the cross-term vanishes:
\[ \sum _{k=1}^M (\hat y_k - \bar y)\,e_k =\sum _{k=1}^M \hat y_k\,e_k \;-\;\bar y\sum _{k=1}^M e_k =0 - 0 = 0, \]
since in LS, \(\sum _{k=1}^M e_k = 0\) and \(\sum _{k=1}^M \hat y_k\,e_k = 0\).
R2 of Uni-variate Linear LS For the uni-variate LR case, it is the fraction of the sample variance of \(y\) explained by the linear fit,
\(\seteqnumber{0}{7.}{24}\)\begin{equation} R^{2}=r_{xy}^{2}. \end{equation}
-
Proof. Starting with \(\hat {y}_k =w_0 + w_1x_k\):
\(\seteqnumber{0}{7.}{25}\)\begin{equation} \begin{aligned} \SSR &=\sum _{k=1}^{M}(\hat {y}_k-\bar {y})^2\\ &=\sum _{k=1}^{M}(w_0 + w_1x_k-\bar {y})^2\\ &=\sum _{k=1}^{M}(\bar y - w_1\bar x + w_1x_k-\bar {y})^2\\ &=w_1^2\sum _{k=1}^{M}(x_k - \bar x)^2\\ &=w_1^2 M s_x^2 = M \left (\frac {s_{xy}}{s_x^2}\right )^2 s_x^2 = M \frac {s_{xy}^2}{s_x^2},\\ \SST &= \sum _{k=1}^{M}(y_k-\bar {y})^2 = M s_y^2 \end {aligned} \end{equation}
Thus, \(\SSR /\SST = s_{xy}^2 / (s_x^2 s_y^2) = r_{xy}^2\).
-
• The identity is uni-variate. The proof rests on \(\hat {y}_k-\bar {y} = w_1(x_k-\bar {x})\), a single centered column. With more than one feature the prediction is no longer proportional to one column, and \(R^2\) is not the square of any single correlation coefficient.
-
• \(R^2\) is a square, so it discards the sign: a perfect rising line and a perfect falling line both score \(R^2=1\). The direction of the relation is carried by \(r_{xy}\), or equivalently by the sign of \(w_1\), which by (7.21) is the sign of \(s_{xy}\). Reporting \(R^2\) alone tells how much of the variance is explained, never in which direction.
7.3.2 MSE of Linear LS
The general (biased3) MSE is given by
\(\seteqnumber{0}{7.}{26}\)\begin{equation} \MSE = s_y^{2}\bigl (1-r_{xy}^{2}\bigr ) = s_e^{2} \end{equation}
-
Proof. Using (7.23):
\(\seteqnumber{0}{7.}{27}\)\begin{equation} \begin{aligned} \MSE &= \frac {1}{M}\SSE = \frac {1}{M}\left (\SST -\SSR \right )\\ &= \frac {\SST }{M}\left (1-\frac {\SSR }{\SST }\right )\\ &= s_y^{2}\bigl (1-R^{2}\bigr )\\ &= s_y^{2}\bigl (1-r_{xy}^{2}\bigr ) \end {aligned} \end{equation}
-
Example 7.2 (A complete uni-variate fit by hand): A dataset of \(M=5\) observations is given,
\(k\) 1 2 3 4 5 \(x_k\) 1 2 3 4 5 \(y_k\) 2 3 5 6 9 -
(a) Compute \(\bar {x}\), \(\bar {y}\), \(s_x^2\), \(s_y^2\) and the covariance \(s_{xy}\).
-
(b) Find \(w_1\) and \(w_0\) from (7.21).
-
(c) List the predictions and the errors, and compute \(\SSE \), \(\MSE \) and \(\RMSE \) directly.
-
(d) Compute \(r_{xy}\) and \(R^2\), check \(R^2=r_{xy}^2\), and check \(\MSE \) against \(s_y^2(1-r_{xy}^2)\).
-
(e) What is the \(\MSE \) of the best constant model (7.10), \(\hat {y}=\bar {y}\), and by what factor does the slope improve on it?
-
(f ) \(\SSE \) is minimized, but \(\RMSE \) is reported. Why?
-
Solution: Means, variances, covariance. Both means are integers, which keeps everything that follows exact.
\(\seteqnumber{0}{7.}{28}\)\begin{align*} \bar {x} &= \frac {1+2+3+4+5}{5} = 3, & \bar {y} &= \frac {2+3+5+6+9}{5} = \frac {25}{5} = 5\\ x_k-\bar {x} &= -2,\ -1,\ 0,\ 1,\ 2, & y_k-\bar {y} &= -3,\ -2,\ 0,\ 1,\ 4 \end{align*}
\(\seteqnumber{0}{7.}{28}\)\begin{align*} s_x^2 &= \frac {1}{M}\sum _k\left (x_k-\bar {x}\right )^2 = \frac {4+1+0+1+4}{5} = \frac {10}{5} = 2\\ s_y^2 &= \frac {1}{M}\sum _k\left (y_k-\bar {y}\right )^2 = \frac {9+4+0+1+16}{5} = \frac {30}{5} = 6\\ s_{xy} &= \frac {1}{M}\sum _k\left (x_k-\bar {x}\right )\left (y_k-\bar {y}\right ) = \frac {6+2+0+1+8}{5} = \frac {17}{5} = 3.4 \end{align*}
The coefficients.
\(\seteqnumber{0}{7.}{28}\)\begin{align*} w_1 &= \frac {s_{xy}}{s_x^2} = \frac {3.4}{2} = 1.7\\ w_0 &= \bar {y} - w_1\bar {x} = 5 - 1.7\cdot 3 = -0.1 \end{align*}
Predictions and errors.
\(k\) 1 2 3 4 5 \(\hat {y}_k\) 1.6 3.3 5.0 6.7 8.4 \(e_k\) 0.4 \(-0.3\) 0 \(-0.7\) 0.6 The errors sum to zero, \(0.4-0.3+0-0.7+0.6 = 0\), which is the arithmetic check on \(w_0\).
\(\seteqnumber{0}{7.}{28}\)\begin{align*} \SSE &= 0.16+0.09+0+0.49+0.36 = 1.10\\ \MSE &= \frac {\SSE }{M} = \frac {1.10}{5} = 0.22\\ \RMSE &= \sqrt {0.22} \approx 0.469 \end{align*}
Correlation and \(R^2\).
\(\seteqnumber{0}{7.}{28}\)\begin{align*} r_{xy} &= \frac {s_{xy}}{s_xs_y} = \frac {3.4}{\sqrt {2}\sqrt {6}} = \frac {3.4}{\sqrt {12}} \approx 0.9815\\ R^2 &= 1 - \frac {\SSE }{\SST } = 1 - \frac {1.10}{30} \approx 0.9633\\ r_{xy}^2 &= \frac {s_{xy}^2}{s_x^2s_y^2} = \frac {3.4^2}{2\cdot 6} = \frac {11.56}{12} = 0.9633 = R^2 , \end{align*} as the identity \(R^2=r_{xy}^2\) requires. The cross-check on the error closes too,
\(\seteqnumber{0}{7.}{28}\)\begin{equation*} s_y^2\left (1-r_{xy}^2\right ) = 6\left (1-0.9633\right ) = 0.22 = \MSE , \end{equation*}
matching the value obtained from the errors one at a time.
Figure 7.3: The fit of Example 7.2 and the baseline it has to beat. The solid segments are the errors of the line, the dotted ones those of the best constant model \(\hat {y}=\bar {y}=5\). Both sets sum to zero; the factor of \(27\) between \(\MSE =0.22\) and \(s_y^2=6\) is the difference in their lengths, and \(R^2=0.963\) is that comparison expressed as a fraction.The constant baseline. The best model with a single coefficient is \(\hat {y}=\bar {y}\), and its error is the variance of the target itself,
\(\seteqnumber{0}{7.}{28}\)\begin{equation*} \MSE _{\text {const}} = s_y^2 = 6 \qquad \text {against}\qquad \MSE = 0.22 , \end{equation*}
a factor of about \(27\). \(R^2\) measures precisely this: the constant model scores \(R^2=0\) by construction, and the slope claims \(96.3\%\) of the available variance.
Loss against metric. \(\SSE \) is the loss: it is what the normal equations (7.18) minimize. But it is in units of \(y^2\) and it grows with \(M\), so two datasets of different size cannot be compared through it. \(\RMSE = \sqrt {\MSE }\) divides out the sample size and takes the square root back, landing in the units of \(y\) itself: here \(0.469\), a typical miss of about half a unit against targets running from 2 to 9. Minimizing \(\SSE \), \(\MSE \) or \(\RMSE \) gives the same \(\bw \) by (7.7); the choice among them is a choice about reporting, not about fitting.
-
7.4 Normalized (z-score) Linear Regression (*)
The goal of this section is to provide an intuitive interpretation of the correlation coefficient.
Rescaling To make the slope directly interpretable, we rescale both variables to zero mean and unit variance:
\(\seteqnumber{0}{7.}{28}\)\begin{equation} z_i = \frac {x_i-\bar x}{s_x}, \qquad t_i = \frac {y_i-\bar y}{s_y}, \qquad i=1,\ldots ,M \end{equation}
The properties of \(z_i\) and \(t_i\) are
\(\seteqnumber{0}{7.}{29}\)\begin{align*} \bar z &= \frac {1}{M}\sum z_i=0,\\ \bar t &=\frac {1}{M}\sum t_i=0,\\ s_z &= s_t = 1. \end{align*}
-
Proof. Using biased variance definitions 4:
\(\seteqnumber{0}{7.}{29}\)\begin{equation} s_x^2 = \frac {1}{M}\sum _{k=1}^M (x_k - \bar {x})^2, \qquad s_y^2 = \frac {1}{M}\sum _{k=1}^M (y_k - \bar {y})^2. \end{equation}
For the means
\(\seteqnumber{0}{7.}{30}\)\begin{equation} \begin{aligned} \sum _{k=1}^M z_k &= \frac {1}{s_x}\sum _{k=1}^M (x_k - \bar {x}) \\ &= \frac {1}{s_x}\left (\sum _{k=1}^M x_k - M\bar {x}\right ) \\ &= \frac {1}{s_x}(M\bar {x} - M\bar {x}) = 0 \end {aligned} \end{equation}
hence \(\bar {z} = 0\) and similarly \(\bar {t} = 0\). For the variances:
\(\seteqnumber{0}{7.}{31}\)\begin{equation} s_z^2 = \frac {1}{M}\sum _{k=1}^M z_k^2 = \frac {1}{M}\sum _{k=1}^M \frac {(x_k - \bar {x})^2}{s_x^2} = \frac {1}{s_x^2} \left [ \frac {1}{M} \sum _{k=1}^M (x_k - \bar {x})^2 \right ] = \frac {s_x^2}{s_x^2} = 1. \end{equation}
The proof for \(s_t^2=1\) is analogous.
Normal Equations The linear model in normalized space is
\(\seteqnumber{0}{7.}{32}\)\begin{equation} \hat t_i = r_{xy}\,z_i. \end{equation}
where
\(\seteqnumber{0}{7.}{33}\)\begin{equation} r_{xy}=\frac {s_{xy}}{s_x\,s_y} \end{equation}
is the correlation coefficient.
-
Proof. Applying (7.22) to
\(\seteqnumber{0}{7.}{34}\)\begin{equation} \hat {t}_i = w_0^* + w_1^*z_i \end{equation}
yields
\(\seteqnumber{0}{7.}{35}\)\begin{align} w_0^* &=0,\label {eq-w_0-normalized}\\ w_1^* &= \frac {s_{zt}}{s_z^2} =\frac {1}{M}\sum _{i=1}^{M} z_i t_i =\frac {1}{M}\sum _{i=1}^{M} z_i t_i =\frac {1}{M}\sum _{i=1}^M \left (\frac {x_i-\bar x}{s_x}\right )\left (\frac {y_i-\bar y}{s_y}\right ) =r_{xy} \label {eq-w_1-normalized} \end{align}
Regression error as a function of \(r_{xy}\)
With the optimal normalized model \(\hat t = r_{xy} z\) the residuals are
\(\seteqnumber{0}{7.}{37}\)\begin{equation} e_i = t_i - \hat {t}_i = t_i - r_{xy} z_i \end{equation}
with
\(\seteqnumber{0}{7.}{38}\)\begin{equation} \MSE = 1 - r_{xy}^{2} = s_e^2. \end{equation}
-
Proof.
\(\seteqnumber{0}{7.}{39}\)\begin{equation} \begin{aligned} s_e^2 &= \frac {1}{M}\sum _{i=1}^M e_i^2 = \frac {1}{M}\sum _{i=1}^M \left (t_i - r_{xy} z_i\right )^2\\ &= \frac {1}{M}\sum _{i=1}^M t_i^2 -2r_{xy}\frac {1}{M}\sum _{i=1}^M t_iz_i + r_{xy}^2\frac {1}{M}\sum _{i=1}^Mz_i^2\\ &=s_t^2 -2r_{xy}\left (r_{xy}\right ) + r_{xy}^2s_z^2\\ &=1 - r_{xy}^2 \end {aligned} \end{equation}
Geometrical interpretation of \(r_{xy}\)
-
• Units of x- and y-axes are standard deviation from the origin since \(t_i\) and \(z_i\) are centered and standardized.
-
• In the normalized space, \(r_{xy}\) is the regression slope. A one-standard-deviation increase in \(x\) (\(z\) changes by 1) changes \(y\) by \(r_{xy}\) standard deviations on average (\(\hat {t}\) changes by \(r_{xy}\)).
-
• Let’s define two vectors, \(\bz \) and \(\bt \), both in \(\mathbb R^{M}\). The angle between them is \(\theta \) and \(r_{xy}=\cos \theta \) (Fig. 7.4).
-
Proof. The corresponding dot product is
\(\seteqnumber{0}{7.}{40}\)\begin{equation} \bz \cdot \bt = \norm {\bz }\norm {\bt }\cos (\theta ) \end{equation}
Using (7.37),
\(\seteqnumber{0}{7.}{41}\)\begin{equation} \bz \cdot \bt = \sum _{i=1}^M z_it_i = M r_{xy}. \end{equation}
Since \(\norm {\bz } = \sqrt {\sum z_i^2} = \sqrt {M s_z^2} = \sqrt {M}\), we have
\(\seteqnumber{0}{7.}{42}\)\begin{equation} \norm {\bz }\norm {\bt } = \sqrt {M}\cdot \sqrt {M} = M \end{equation}
-
Consequences:
-
• Range: \(-1 \le r_{xy} \le +1\).
-
• Special cases:
-
– \(r_{xy}=+1\) indicates perfect positive linear association, \(\theta = 0^\circ \).
-
– \(r_{xy}=-1\) indicates perfect negative linear association, \(\theta = 180^\circ \).
-
– \(r_{xy}=0\) indicates no linear association, \(\theta = 90^\circ \).
-
-
Example 7.3 (z-score regression and the cosine): Normalize the data of Example 7.2 to \(z\)-scores and fit \(\hat {t} = w_0^* + w_1^*z\).
-
(a) Tabulate \(z_k\) and \(t_k\).
-
(b) Verify \(w_0^*=0\) (7.36) and \(w_1^*=r_{xy}\) (7.37) numerically.
-
(c) Verify \(\MSE = 1-r_{xy}^2\), and explain why this is the same fit as before, not a better one.
-
(d) Using \(r_{xy}=\cos \theta \) (Fig. 7.4), find the angle \(\theta \) between the two centered vectors.
-
Solution: The \(z\)-scores. With \(s_x=\sqrt {2}\approx 1.4142\) and \(s_y=\sqrt {6}\approx 2.4495\),
\(k\) 1 2 3 4 5 \(z_k\) \(-1.4142\) \(-0.7071\) 0 0.7071 1.4142 \(t_k\) \(-1.2247\) \(-0.8165\) 0 0.4082 1.6330 Both rows have mean \(0\) and, by construction, variance \(1\).
The coefficients. Since \(\bar {z}=\bar {t}=0\), the intercept follows immediately,
\(\seteqnumber{0}{7.}{43}\)\begin{equation*} w_0^* = \bar {t} - w_1^*\bar {z} = 0 , \end{equation*}
which is (7.36): centering removes the intercept, always. For the slope, \(s_z^2=1\), so \(w_1^*\) is the covariance of the \(z\)-scores alone,
\(\seteqnumber{0}{7.}{43}\)\begin{align*} \sum _k z_kt_k &= 1.7321+0.5774+0+0.2887+2.3094 = 4.9075\\ w_1^* &= \frac {1}{M}\sum _k z_kt_k = \frac {4.9075}{5} \approx 0.9815 = r_{xy} . \end{align*}
The error. With \(s_t^2=1\),
\(\seteqnumber{0}{7.}{43}\)\begin{equation*} \MSE = 1 - r_{xy}^2 = 1-0.9633 = 0.0367 . \end{equation*}
This is not an improvement on the \(0.22\) of the original fit; it is the same error measured on a rescaled axis. Dividing \(y\) by \(s_y=\sqrt {6}\) divides every error by \(\sqrt {6}\) and so divides the \(\MSE \) by \(6\), and indeed \(0.22/6 = 0.0367\). Normalization is a change of units. It cannot change what the model captures, and \(r_{xy}\), being dimensionless, is invariant under it. What it does buy is a scale on which \(\MSE \) runs from \(0\) to \(1\) and reads directly as \(1-R^2\).
The angle. Collecting the centered, normalized values into vectors \(\bz \) and \(\bt \), the correlation is their cosine,
\(\seteqnumber{0}{7.}{43}\)\begin{equation*} r_{xy} = \cos \theta = 0.9815 \quad \Longrightarrow \quad \theta = \arccos (0.9815) \approx 11.0^\circ . \end{equation*}
The two vectors are nearly parallel, and the \(3.7\%\) of unexplained variance is the squared sine, \(\sin ^2\theta = 1-\cos ^2\theta = 0.0367\), which is the \(\MSE \) computed above.
-
7.5 Spearman Rank Correlation
The Pearson correlation coefficient \(r_{xy}\) measures linear association. When the relationship between \(x\) and \(y\) is monotonic but non-linear, or when the data contain outliers, the Spearman rank correlation provides a more robust measure.
Let \(R(x_i)\) denote the rank of \(x_i\) among \(\{x_1,\dots ,x_M\}\) (with average ranks for ties), and similarly \(R(y_i)\). The Spearman correlation is the Pearson correlation computed on the ranks:
\(\seteqnumber{0}{7.}{43}\)\begin{equation} r_s = r_{R(x)\,R(y)} \end{equation}
-
• Range: \(-1 \le r_s \le +1\).
-
• \(|r_s| = 1\): perfect monotonic (not necessarily linear) relationship.
-
• Robust to outliers and non-linear monotonic relationships.
-
• For linear data, \(r_s \approx r_{xy}\).
-
Example 7.4: Consider \(M=5\) observations with an exponential relationship \(y \approx e^x\). The table compares a clean dataset (perfectly monotonic) with a noisy one (rank swap at \(i=2,3\)):
Clean (\(y_i = e^{x_i}\)) Noisy \(i\) 1 2 3 4 5 1 2 3 4 5 \(x_i\) 1 2 3 4 5 1 2 3 4 5 \(y_i\) 2.7 7.4 20.1 54.6 148.4 3.5 8.1 5.3 52.0 135.7 \(R(x_i)\) 1 2 3 4 5 1 2 3 4 5 \(R(y_i)\) 1 2 3 4 5 1 3 2 4 5 -
• Clean: \(r_{xy} \approx 0.89\), \(r_s = 1\). Pearson underestimates the association (the exponential curve is not a straight line), while Spearman correctly identifies a perfect monotonic relationship.
-
• Noisy: \(r_{xy} \approx 0.86\), \(r_s = 0.90\). Noise causes \(y_2 > y_3\), swapping ranks 2 and 3. Both correlations decrease, but Spearman specifically reflects the broken monotonicity.
-
7.6 Correlation is not Causation
A non-zero \(r_{xy}\) (or \(r_s\)) quantifies statistical association between \(x\) and \(y\), but it says nothing about whether \(x\) causes \(y\). Several distinct mechanisms can produce the same correlation:
-
• Direct causation: \(x\) genuinely drives \(y\).
-
• Reverse causation: \(y\) drives \(x\), not the other way around.
-
• Confounding (common cause): a third variable \(z\) influences both \(x\) and \(y\), inducing an association between them even though neither causes the other.
-
• Selection bias: the way the dataset was sampled induces a correlation that does not exist in the underlying population.
-
• Coincidence: in small samples, or when many variable pairs are tested, large \(|r_{xy}|\) values arise purely by chance (spurious correlation).
-
Example 7.5: In a coastal town, daily ice-cream sales (\(x\)) and drowning incidents (\(y\)) are strongly positively correlated. Ice-cream does not cause drowning; both are driven by a confounder, summer temperature, which independently increases ice-cream consumption and the number of swimmers at the beach.
-
Example 7.6 (Naming the mechanism): Each of the following correlations is real and reproducible. Name the mechanism behind it, and propose one check that would tell it apart from a causal link.
-
(a) Hospital patients treated in an intensive care unit die at a higher rate than those treated on a general ward.
-
(b) The number of firefighters sent to a fire correlates strongly with the damage the fire causes.
-
(c) Among the hired job applicants of a company, the written-test score correlates negatively with the interview score, although across all applicants the two are unrelated.
-
-
(a) Reverse causation. Being close to death causes admission to intensive care, not the other way around. Check: compare patients of equal severity on admission.
-
(b) Confounding. The size of the fire drives both the number of firefighters and the damage. Check: compare fires of similar size; within them, more firefighters should mean less damage.
-
(c) Selection bias. The company hires anyone who does well on either test, so within the hired group a weak score on one test appears only alongside a strong score on the other. Check: compute the correlation on all applicants, rejected ones included; it vanishes.
None of the three is a coincidence: collecting the data again brings each correlation back, because the structure that produces it is still there.
-
-
Establishing causation requires a controlled experiment or a dedicated causal-inference framework. Correlation magnitude alone, no matter how close to \(\pm 1\), is not evidence of a causal link.
Further Reading
-
• Spurious Correlations by Tyler Vigen is a great illustration of the principle that correlation is not causation.
7.7 Exercises
7.1 (Reading the correlation coefficient) Fig. 7.5 shows the results of four two-dimensional experiments. Rank them from the highest to the lowest absolute correlation coefficient \(\abs {r_{xy}}\), and justify the ranking.
7.2 (Spearman against Pearson) Six measurements are taken of a quantity believed to grow geometrically,
| Clean | Noisy | |||||||||||
| \(k\) | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 |
| \(x_k\) | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 |
| \(y_k\) | 1 | 2 | 4 | 8 | 16 | 32 | 1 | 2 | 8 | 4 | 16 | 32 |
For each dataset:
-
(a) Compute the Pearson coefficient \(r_{xy}\).
-
(b) Compute the Spearman coefficient \(r_s\) (Sec. 7.5).
-
(c) Explain the gap between the two in the clean case, and what changes in the noisy one.
Useful sums for both datasets: \(\sum _k(x_k-\bar {x})^2 = 17.5\) and \(\sum _k(y_k-\bar {y})^2 = 703.5\).
7.3 (simulation: how often does noise look correlated) Find out, by simulation, how large a correlation coefficient has to be before it means anything.
Generate \(M\) pairs \((x_k,y_k)\) of independent standard normal values (randn), so that the true correlation is zero, and compute \(\hat {r}_{xy}\). Repeat \(k\) times, for \(M = 10\), \(30\) and \(100\). Fig. 7.6 shows the result of such a simulation with \(k=10^5\).
-
(a) Reproduce Fig. 7.6 with your own simulation and \(k=10^4\). What do the three boxes have in common, and what changes with \(M\)?
-
(b) Compare the std values \(s\) in the figure with \(1/\sqrt {M-1}\).
-
(c) What is shown in red? Explain the red lines and the red numbers, and what the red number at \(M=10\) means for a reader who finds \(\abs {r_{xy}} > 0.6\), a value most readers would call a strong correlation, in a dataset of ten points.
-
(d) State the practical rule this suggests for reporting a correlation coefficient.
7.4 (simulation: regression error against sample size) This assignment focuses on the effect of the sample size on linear least-squares. Generate data from a known linear model, \(y_k = w_1x_k + w_0 + \epsilon _k\) with \(\epsilon _k\) of variance \(\sigma ^2\), fit it by LS, and study the resulting \(\MSE \).
-
• Perform linear regression using the LS method and analyze the \(\MSE \) across different sample sizes \(M\).
-
• Repeat each \(\MSE \) evaluation at least 30 times.
-
• Summarize the results in a boxplot (e.g., as in Fig. 7.7).
-
• Does it seem reasonable that the \(\MSE \) grows with an increase in \(M\) (more data)?
Hint for the last part: count how many of the \(M\) errors are free to vary once two coefficients have been fitted to the same data.