39.2. Regression analysis with linear models#
When performing a regression analysis with a linear model, i.e., doing linear regression, we have access to a set of data \(\mathcal{D}\) for the dependent variable, i.e.,
For each datum there is an independent variable \(x_i\), and our model for each datum \(y_i\) is
We can collect the basis function evaluated at each independent variable \(x_i\) in a matrix \(\mathbf{X}\) of dimension \(N_d \times N_p\)
This matrix will be referred to as a design matrix.
Example 39.3 (The design matrix for polynomial models)
The design matrix for a linear model with polynomial basis functions becomes
where we are considering a polynomial of degree \(p-1\) which implies a model with \(p\) features (including the intercept). It is also known within linear algebra as a Vandermonde matrix.
Next, we introduce a column vector for the parameters
and we arrive at the matrix equation
The last term \(\boldsymbol{\epsilon}\) is a column vector of so-called residuals. This term expresses the part of the dependent variable, for which we have data, that we cannot describe using a linear model. Formally, we can therefore write \(\epsilon_i = y_i - M_i\) and define the vector as
It is important to realize that our model \(M\) provides an approximate description of the data. Indeed, all models are wrong and in a realistic setting we have no guarantee that the data is generated by a linear process. Of course, based on physics insight, or other assumptions, there might exists very good reasons for using a linear model to explain the data.
The normal equation#
A regression analysis often aims at finding the model parameters \(\pars\) of a model \(M\) such that the vector of errors \(\boldsymbol{\epsilon}\) is minimized in the sense of its Euclidean norm (or 2-norm). You might ask the very relevant question why this particular goal is desirable. We will return to this consideration in Bayesian Linear Regression (BLR). Nevertheless, in order to find the βoptimalβ set of parameters \(\pars^*\) we seek to minimize
The solution to this optimization problem turns out to be a solution of the normal equation and is known as ordinary least-squares or ordinary linear regression.
Theorem 39.1 (Ordinary least squares (the normal equation))
The ordinary least-squares method corresponds to finding the optimal parameter vector \(\pars^*\) that minimizes the Euclidean norm of the residual vector \(\boldsymbol{\epsilon} = \data - \dmat \pars\), where \(\data\) is a column vector of observations and \(\dmat\) is the design matrix (39.6).
Finding this optimum turns out to correspond to solving the normal equation
Given that the normal matrix \(\dmat^T\dmat\) is invertible, the solution to the normal equation is given by
Proof. Due to its quadratic form, the Euclidean norm \(\left| \boldsymbol{\epsilon} \right|_2^2 = \left(\data-\dmat\pars\right)^T\left(\data-\dmat\pars\right) \equiv C(\pars)\) is bounded from below and we just need to find the single extremum. That is we need to solve the problem
In practical terms it means we will require
where \(y_i\) and \(f_j(x_i)\) are the elements of \(\data\) and \(\dmat\), respectively. Performing the derivative results in
which is one element of the full gradient vecor. This gradient vector can be succinctly expressed in matrix-vector form as
The minimum of \(C\), where \(\boldsymbol{\nabla}_{\pars} C(\pars) = 0\), then corresponds to
which is the normal equation. Finally, if the matrix \(\dmat^T\dmat\) is invertible then we have the solution
We note also that since our design matrix is defined as \(\dmat\in {\mathbb{R}}^{N_d\times N_p}\), the product \(\dmat^T\dmat \in {\mathbb{R}}^{N_p\times N_p}\). The product \(\left(\dmat^T\dmat\right)^{-1}\dmat^T\) is called the pseudo-inverse of the design matrix \(\dmat\). The pseudo-inverse is a generalization of the usual matrix inverse. The former can be defined for also for non-square matrices that are not necessarily full rank. In the case of full-rank and square matrices the pseudo-inverse is equal to the usual inverse.
Checkpoint question
Here we have been minimizing the sum of squared residuals, see Eq. (39.11). An optimization metric that is less dependent on the number of data is the mean-squared error. The cost function is then
If, and how, would this choice of cost function modify:
The optimum \(\pars^*\)?
The expression for the gradient vector \(\boldsymbol{\nabla}_{\pars} C_\mathrm{MSE} (\pars)\)?
Answers
The only difference is a factor \(1/N_d\) which does not affect the position of the minimum. The gradient, however, will inherit this factor from the cost fundtion.
The regression residuals \(\boldsymbol{\epsilon}^{*} = \data - \dmat \pars^{*}\) can be used to obtain an estimator \(s^2\) of the variance of the residuals
where \(N_p\) is the number of parameters in the model and \(N_d\) is the number of data.