Skip to main content

12  Optimization with Matrixes for Data Fitting

This section focuses on optimization problems motivated by data fitting, including least squares derivations and principal-component style objectives in matrix form.

12.1 Matrix derivation of least square minimizer

12.1.1 Minimizing the residual sum of squares

Given matrix \(X\) that is \(n\) by \(k\) where \(n\) is the number of observations and \(k\) is the number of variables to be included on the “right-hand side” of the regression, and given vector \(y\) that is \(n\) by \(1\) representing the outcome variable, we want to find the vector \(\beta\) that is \(k\) by \(1\) that minimizes the residual sum of squares (RSS). Residual sum of squares is a scalar value, so this is a scalar-valued function with a vector input.

The residual sum of squares can be expressed as, using summation notation, and not using vector notation:

\[ RSS(\beta) = \sum_{i=1}^{n} (y_i - \sum_{j=1}^k x_{ij} \beta_j )^2 \]

where \(y_i\) is the \(i\)-th observed value of the outcome variable, \(x_{ij}\) is the value of the \(j\)-th predictor variable for the \(i\)-th observation, and \(\beta_j\) is the coefficient for the \(j\)-th predictor variable.

Using vector notation, we can express the residual sum of squares as:

\[ RSS(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \]

Suppose \(n=2\) and \(k=2\), then we can write out the matrix formula out explicitly as:

\[ RSS(\boldsymbol{\beta}) = \begin{bmatrix} y_1 - (x_{11}\beta_1 + x_{12}\beta_2) & y_2 - (x_{21}\beta_1 + x_{22}\beta_2) \end{bmatrix} \begin{bmatrix} y_1 - (x_{11}\beta_1 + x_{12}\beta_2) \\ y_2 - (x_{21}\beta_1 + x_{22}\beta_2) \end{bmatrix} \]

12.1.2 Minimizing the residual sum of squares, partial derivative approach

For solve for the vector \(\beta\) that minimizes the residual sum of squares, we can take the derivative of the RSS function with respect to \(\beta\), set it equal to zero, and solve for \(\beta\).

We can do this with the summation based formula, and take derivative with respect to each \(\beta_j\) one by one. We have:

\[ \frac{\partial RSS(\boldsymbol{\beta})}{\partial \beta_j} = -2 \sum_{i=1}^{n} x_{ij} (y_i - \sum_{m=1}^{k} x_{im} \beta_m) = 0 \]

The equations above represents a system of linear equations with \(k\) equations and \(k\) unknowns, which can be solved using standard methods for solving systems of linear equations, which looks cumbersome, and still ends up requiring matrix algebra to solve linear system.

12.1.3 Minimizing the residual sum of squares, using the product rule

We know directly use the matrix differential product rule and take the derivative of the RSS function with respect to \(\beta\).

We start with the RSS function:

\[ RSS(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \]

First, we distribute the terms using the distributive property of matrix multiplication:

\[ RSS(\boldsymbol{\beta}) = \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]

Second, we apply the matrix differential product rule to take the derivative of each term with respect to \(\boldsymbol{\beta}\). For the first term, \(\mathbf{y}^T\mathbf{y}\), it is a constant with respect to \(\boldsymbol{\beta}\), so its derivative is zero. For the second term, \(-2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y}\), we treat \(\mathbf{X}^T\mathbf{y}\) as a constant vector and apply the product rule:

\[ \frac{\partial (-2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y})}{\partial \boldsymbol{\beta}} = -2\mathbf{X}^T\mathbf{y} \]

Third, for the third term, \(\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta}\), we treat \(\mathbf{X}^T\mathbf{X}\) as a constant matrix and apply the product rule:

\[ \frac{\partial (\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]

12.1.4 Minimizing the residual sum of squares, more steps

A less efficient way to find the derivative is to not use the product rule and work out things in more granular steps. Using the matrix formula for RSS, we can expand the expression as follows using the transpose and distributive properties of matrix multiplication:

\[ RSS(\boldsymbol{\beta}) = \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]

Now we can consider the \(d(RSS(\boldsymbol{\beta}))\), the differential of the RSS function with respect to \(\boldsymbol{\beta}\). Applying the distributive and inverse rules, we have:

\[\begin{align} RSS(\boldsymbol{\beta}+d\boldsymbol{\beta})-RSS(\boldsymbol{\beta}) &= (\mathbf{y} - \mathbf{X}(\boldsymbol{\beta} + d\boldsymbol{\beta}))^T (\mathbf{y} - \mathbf{X}(\boldsymbol{\beta} + d\boldsymbol{\beta})) - (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \\ &= -2 d\boldsymbol{\beta}^T \mathbf{X}^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) + d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta} \\ &= -2 d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{y} + 2 d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} + d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta} \end{align}\]

The last term \(d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta}\) is second order in the increment and does not contribute to the differential. We are left with the first two terms, which we can express as:

\[ d(RSS(\boldsymbol{\beta})) = \left(-2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta}\right)^T d\boldsymbol{\beta} \]

This means that the gradient of the RSS function with respect to \(\boldsymbol{\beta}\) is:

\[ \nabla RSS(\boldsymbol{\beta}) = -2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} \]

If \(\mathbf{X}\) has full column rank, then \(\mathbf{X}^T\mathbf{X}\) is invertible. Setting the gradient to zero and multiplying by its inverse gives:

\[\begin{align} -2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} &= 0\\ \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} &= \mathbf{X}^T \mathbf{y}\\ \boldsymbol{\beta} &= (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}\\ \end{align}\]

If \(\mathbf{X}\) is rank deficient, the normal equations still characterize least-squares minimizers, but the coefficient vector need not be unique; a generalized inverse can be used to select a solution.

12.2 Optimization and Principal Component Analysis

12.2.1 The centered variance covariance matrix

Given a matrix of data, \(\widehat{X}\), what happens we multiply the transpose of the matrix with itself? Let:

\[ X = \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{22} \\ x_{31} & x_{32} \end{bmatrix} \]

Then we have:

\[ X^T X = \begin{bmatrix} x_{11} & x_{21} & x_{31} \\ x_{12} & x_{22} & x_{32} \end{bmatrix} \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{22} \\ x_{31} & x_{32} \end{bmatrix} = \begin{bmatrix} x_{11}^2 + x_{21}^2 + x_{31}^2 & x_{11}x_{12} + x_{21}x_{22} + x_{31}x_{32} \\ x_{12}x_{11} + x_{22}x_{21} + x_{32}x_{31} & x_{12}^2 + x_{22}^2 + x_{32}^2 \end{bmatrix} \]

Each column of \(X\) can be viewed as a variable. The diagonal entries of \(X^TX\) are sums of squares, and the off-diagonal entries are cross-products. For a centered \(n\)-observation data matrix, \(X^TX/(n-1)\) is the sample covariance matrix; without centering, the cross-products also reflect the column means.

12.2.2 Do we always have to demean the data before PCA?

In standard PCA, the answer is essentially yes: we usually center each variable first. The reason is that PCA is designed to find directions of greatest variation around the mean, not directions of where the data happen to sit in the ambient space. If we do not subtract the mean, a large overall shift in the data can dominate the first principal component even when there is little spread around that shifted center.

Suppose the raw data matrix is \(X\) (\(m\) observations by \(p\) variables), and let \(\bar{x}\) be the \(p \times 1\) vector of column means. The centered data matrix is

\[ \tilde{X} = X - \mathbf{1}\bar{x}^T, \]

where \(\mathbf{1}\) is the \(m \times 1\) vector of ones. Then the sample covariance matrix among the \(p\) variables is

\[ \Sigma = \frac{1}{m-1} \tilde{X}^T\tilde{X}. \]

This is the matrix whose eigenvectors define the principal directions. If we skip centering, the object we optimize is instead based on \(X^T X\), which includes the mean level of the variables. In that case, the first principal component may simply point in the direction of the overall mean rather than the direction of maximum variation around the mean.

So the key idea is:

  • PCA on centered data captures variation around the center of the cloud of points.
  • PCA on raw data can be dominated by the location of the cloud rather than its shape.

This is why demeaning is not a cosmetic step in classical PCA; it is essential for the usual covariance-based interpretation. That said, there are special settings where centering is not needed or is not the right choice. For example, if all variables are already centered, or if the application is specifically about absolute level rather than deviations from the mean, then the centering step may be unnecessary. But for the standard PCA formulation used in statistics and data analysis, demeaning is the normal and essential step.

12.2.3 The principal component analysis idea

Each individual has some vector of attributes, \(x_i \in R^p\), and we have \(m\) individuals. We stack these as rows of a data matrix \(X\) that is \(m\) by \(p\) (observations by variables):

\[ X = \begin{bmatrix} — & x_1^T & — \\ — & x_2^T & — \\ & \vdots & \\ — & x_m^T & — \end{bmatrix} \]

We can think of each individual as a point in \(R^p\), in another word, the individual is defined by a \(p\)-dimensional vector of characteristics. But potentially this is a lot of information if \(p\) is large. How do we summarize this person? Perhaps we can find a lower dimensional representation of the person.

Specifically, we can think about this as looking for a basis vector. When we project an individual’s vector of data onto this basis, we can capture partly what this individual’s attributes are through the “shadow” they cast. Because we are projecting to a specific basis vector, so the direction of this “shadow” is the direction that the basis vector is pointing in and known, what is to be determined is just the length of the shadow along the direction of the basis vector.

For example, given the vector \(x_i = [x_1, x_2]^T\), and a basis vector \(b = [b_1, b_2]^T\), the projection of \(x_i\) onto \(b\) is given by \(p^{x_i}_b = \gamma^{x_i}_b b\), where \(\gamma^{x_i}_b\) is a scalar that determines how long the “shadow” is. The key fact is that the residual vector, \(x_i - p^{x_i}_b\), is perpendicular to the direction of the basis vector \(b\). That is,

\[ (x_i - \gamma^{x_i}_b b) \cdot b = 0. \]

Expanding the dot product gives

\[ x_i \cdot b - \gamma^{x_i}_b (b \cdot b) = 0, \]

so

\[ \gamma^{x_i}_b \|b\|^2 = x_i \cdot b, \]

and therefore, as long as \(b \neq 0\),

\[ \gamma^{x_i}_b = \frac{x_i \cdot b}{\|b\|^2}. \]

This is exactly the scalar that scales the unit direction \(b\) to the orthogonal projection of \(x_i\) onto the line spanned by \(b\). For each row of matrix \(X\), we can cast onto the same basis and compute their respective \(\gamma^{x_j}_b\) shadow lengths.

Principal component analysis (PCA) searches over unit directions on the sphere in \(\mathbb{R}^p\) and finds the direction onto which the data vectors have the greatest aggregate squared projection. A direction orthogonal to all data vectors would yield zero projections and provide no information. Finding the direction with the largest aggregate projection is an optimization problem.

12.2.4 The principal component analysis optimization problem

We need to normalize the basis vector, because the shadow lengths proportions of the basis. We normalize the basis vector to be a unit vector, i.e., \(\|b_a\| = 1\).

Considering all \(i\), we have:

\[ \sum_{i=1}^{m} (x_i \cdot b_a)^2 = \sum_{i=1}^{m} b_a^T x_i x_i^T b_a = b_a^T \left( \sum_{i=1}^{m} x_i x_i^T \right) b_a \]

This is the squared sum

So we set up a constrained optimization problem, where we maximize the above expression subject to the constraint that \(b_a\) is a unit vector, i.e., \(\|b_a\| = 1\). This is equivalent to maximizing the expression subject to the constraint that \(b_a^T b_a = 1\).

We can solve this using the method of Lagrange multipliers. The Lagrangian function is given by:

\[ \mathcal{L}(b_a, \lambda) = b_a^T \left( \sum_{i=1}^{m} x_i x_i^T \right) b_a - \lambda (b_a^T b_a - 1) \]

But we can get rid of the summation term by noting that, with \(X\) stacked by rows,

\[ \sum_{i=1}^{m} x_i x_i^T = X^T X \]

So the fully matrix based lagrangian function is:

\[ \mathcal{L}(b_a, \lambda) = b_a^T X^T X b_a - \lambda (b_a^T b_a - 1) \]

where \(X^T X\) is the (unnormalized) Gram / sum-of-squares matrix among the \(p\) attributes; after demeaning the columns of \(X\), it is proportional to the sample covariance matrix.

Taking the derivative with respect to \(b_a\) and setting it equal to zero, we have:

\[ \frac{\partial \mathcal{L}(b_a, \lambda)}{\partial b_a} = 2X^T X b_a - 2\lambda b_a = 0 \]

To see this carefully, write the Lagrangian as

\[ \mathcal{L}(b_a, \lambda) = b_a^T A b_a - \lambda (b_a^T b_a - 1) \]

And we have:

\[ \frac{\partial \mathcal{L}}{\partial b_a} = 2X^T X b_a - 2\lambda b_a. \]

Setting this equal to zero gives the first-order condition, which is that the optimal constrained basis vector, \(b_a^{\ast}\) (along with \(\lambda^{\ast}\)) must satisfy:

\[ X^T X b_a^{\ast} = A b_a^{\ast} = \lambda^{\ast} b_a^{\ast} \]

This is the eigenvalue-eigenvector equation, where \(b_a^{\ast}\) is an eigenvector of the matrix \(X^T X=A\), and \(\lambda^{\ast}\) is the corresponding eigenvalue. But there is a problem, as we demonstrated earlier, there are potentially multiple eigenvector-eigenvalue pairs, all of which would satisfy the optimality condition above, so how do we pick the right one?

We proceed to recall the objective of the optimization problem as well as the constraint that the \(b_a\) is a unit-norm basis vector. For a unit-norm eigenvector \(b_a\) with eigenvalue \(\lambda\), working with the just derived optimality condition, we (1) multiply the Equation above by \(b_a^T\) on the left and on the right at the same time, (2) move the position of \(\lambda\) because it is a scalar value, (3) realize that \(b_a^T b_a = 1\) by unit-norm constraint, and arrive the result that:

\[ (b_a^{\ast})^T X^T X b_a^{\ast} = (b_a^{\ast})^T \lambda^{\ast} b_a^{\ast} = \lambda^{\ast} (b_a^{\ast})^T b_a^{\ast} = \lambda^{\ast} 1 = \lambda^{\ast}. \]

The derivation above tells us that among the candidate \((b_a^{\ast}, \lambda^{\ast})\) that satisfy the optimality condition, the pair where the \(\lambda^{\ast}\) is the largest (the pair with the largest eigenvalue) will maximize the objective function. So we select among the candidates the one with the largest eigenvalue.

This makes intuitive sense, our objective here is to find the basis vector over which when we project all “individual” data vectors, the most information is captured. If the eigenvalue is zero, is there is no information captured, larger eigenvalue means that more information is captured through the “reflections” of the multiple-dimensional-individual-specific information onto this single directional basis vector.

12.2.5 From the first to the second and third principal components

The first principal component solves

\[ \max_{\|b\|=1} b^T A b, \]

where \(A = X^T X\) or, after centering, \(A = \tilde{X}^T\tilde{X}\). The solution is the eigenvector \(v_1\) associated with the largest eigenvalue \(\lambda_1\).

The second principal component is obtained by solving the same problem again, but now with the new restriction that the direction must be orthogonal to the first one:

\[ \max_{\|b\|=1,\; b^T v_1 = 0} b^T A b. \]

This is equivalent to maximizing the quadratic form over the subspace orthogonal to \(v_1\). Because the matrix \(A\) is symmetric, its eigenvectors are orthogonal, and the best direction in that orthogonal subspace is the next eigenvector \(v_2\), associated with the second largest eigenvalue \(\lambda_2\).

Similarly, the third principal component solves

\[ \max_{\|b\|=1,\; b^T v_1 = 0,\; b^T v_2 = 0} b^T A b. \]

This is the same optimization problem restricted to the subspace orthogonal to both \(v_1\) and \(v_2\). The optimal direction is the eigenvector \(v_3\) corresponding to the third largest eigenvalue \(\lambda_3\).

In general, the \(k\)th principal component is the unit vector \(v_k\) solving

\[ \max_{\|b\|=1,\; b^T v_j = 0 \text{ for } j=1,\ldots,k-1} b^T A b, \]

and the solution is the eigenvector associated with the \(k\)th largest eigenvalue of \(A\).

This is the sequence of principal components: each one captures the largest remaining variance, subject to being orthogonal to the earlier components.

Furthermore, we have a very easy way to summarize the total variance we are capturing after each and all iterations, it is simply the sum of the sequence of eigenvalues:

\[ \lambda_1 + \lambda_2 + \cdots + \lambda_r. \]

From each these basis-vector that we find, we project individual-specific-vector-of-data onto each success data-vectors, with the earlier projection capturing more and subsequent ones capturing less of the information embedded in the original data, and with incremental dimension capturing orthogonal information. The relative importance of each projection depends on the rate of value reduction in the sequence of \(\lambda_k\) values.

12.2.6 PCA, stars in space and reflection along radius of earth

To gain intuition for what we are doing with each successively and additionally constrained optimization problem: Consider space, each star has a coordinate in 3D, consider the center of earth as origin, and consider the surface of the earth to mark the direction and unit-length of all basis-vector in 3D normalized by the radius of earth. Principal component analysis is doing the following:

  1. Consider a sample dataset of stars. \(X\) is the matrix of star positions with rows as stars (observations) and columns as spatial coordinates (variables). For any point on the surface of the earth, draw a line from the center to that point, and extend this line in both directions infinitely.
  2. Look at the “reflection” of each star on this infinite line in space, which is measured by finding the closest point along this line to the star, the point (measured from the origin of earth) is now the projection of the star onto or point-on-earth-surface-specific basis vector. We look at the relative length of this projection vs the basis vector for all stars, that creates a new array/vector of individual-star-specific data points, for all the stars in the sample. Note that the value for each star in this vector can be negative or positive, depending on whether its closest point to the line is in the direction of the basis vector, or in the opposite direction.
  3. We look through all points on earth, and measure the squared sum (aggregating each time over all stars’ reflections) of these relative length of projection from star to the basis vector, and we find the basis-vector that maximizes the squared sum relative length of projection. It turns out the basis vector that solves this problem is the eigenvector of the \(X^T X\) Gram matrix of coordinates with the highest eigenvalue.
  4. After having found the first eigenvector that maximizes the squared sum of the relative lengths of the projections, we then look for the second eigenvector. This second eigenvector must be orthogonal to the first one and maximizes the squared sum of the relative lengths of the projections in the remaining subspace. This is equivalent to looking at a circle around the earth’s origin where all points in the circle are orthogonal to the first basis vector that we found (an arrow right through the center of the circle). Note that we are constrained to stay on the surface of earth, so there is only one circle of points where the values are orthogonal to the first basis vector we found, where the projection of all the points along the circle to this first basis vector is the null (zero) vector.