Tutorials › Regression Analysis (Undergraduate) › Ridge and Lasso When Predictors Outnumber Cases

Ridge and Lasso Regression · Tutorial 686 of 991

Ridge and Lasso When Predictors Outnumber Cases

See how ridge and lasso behave when there are more predictors than observations, and why a computable or sparse fit does not by itself make individual coefficients interpretable.

Advanced 11 min read

What You'll Learn

  • Explain why ordinary least-squares coefficients may not be unique when predictors outnumber cases.
  • Derive the dual formula that makes a ridge fit computable using an observation-sized matrix.
  • Work through a ridge fit with more predictors than cases.
  • Explain when lasso has a sparse solution and how its coefficient solution can still be nonunique.
  • Distinguish a well-defined fitted value from an identifiable individual coefficient.
  • Recognize why penalized coefficients do not automatically support ordinary least-squares inference.

More Predictors Than Cases

A regression data set can have more candidate predictors than observed cases. This occurs, for example, when a small data set contains measurements on many features. In that setting, the number of coefficients to estimate can exceed the number of independent pieces of information in the observed outcomes. Ordinary least squares may then have multiple coefficient vectors that fit equally well, or may fail to define a unique coefficient vector at all.

Ridge and lasso address this computational difficulty by adding a penalty to the residual sum of squares. Ridge shrinks coefficients and, for a positive penalty, gives a unique coefficient vector even when predictors are linearly dependent. Lasso also penalizes coefficients, but its absolute-value penalty can produce exact zeros. Neither property implies that the fitted coefficients reveal which predictors are truly responsible for an outcome.

Definition: Let \(n\) be the number of cases and \(p\) the number of predictors. Write \(X\) for the \(n \times p\) predictor matrix, \(y\) for the \(n\)-entry response vector, and \(\beta\) for the \(p\)-entry coefficient vector. The predictors outnumber the cases when \(p>n\). The rank of \(X\), written \(\operatorname{rank}(X)\), is the number of linearly independent columns of \(X\); it cannot exceed \(\min(n,p)\).

With an intercept, a common convention is to center the response and each predictor before fitting the penalized slopes, then estimate the intercept separately and leave it unpenalized. Standardizing predictors is also common so that the penalty treats them on comparable scales. The examples below instead specify uncentered data and explicitly omit an intercept, so their arithmetic does not rely on a centering claim. The matrix results apply to whatever predictor and response values enter the stated objective.

The fits discussed here are solutions to optimization problems for a given design matrix and a given positive penalty value. Their computation does not require normally distributed errors. When a model is used to describe a stochastic response, assumptions about the response and errors matter for that broader interpretation; a penalized fit alone does not establish them.

Why Ridge Can Still Be Computed

For a specified \(\lambda>0\), define the ridge coefficient vector as the minimizer of residual sum of squares plus a squared-coefficient penalty. The intercept, if included, is not penalized. Here the objective is written without a factor of one-half, so \(\lambda\) has the scale shown in the formula.

Definition: The ridge estimate for a fit without an intercept is \(\hat{\beta}_{\text{ridge}}=(X^\mathsf{T}X+\lambda I_p)^{-1}X^\mathsf{T}y\), where \(I_p\) is the \(p \times p\) identity matrix and \(\lambda>0\). With an intercept and centered predictors and response, this formula estimates the slopes; the intercept is then the response mean in the centered-data setup.

Even if \(p>n\), the matrix \(X^\mathsf{T}X\) is only guaranteed to be positive semidefinite, not invertible. Ridge adds \(\lambda I_p\). For every nonzero \(v\), \(v^\mathsf{T}(X^\mathsf{T}X+\lambda I_p)v=\|Xv\|^2+\lambda\|v\|^2>0\). Thus the matrix is positive definite and invertible. The resulting ridge coefficient vector is unique.

There is also a useful formula involving an \(n \times n\) matrix rather than a \(p \times p\) matrix. When \(p\) is much larger than \(n\), this can make the computation more manageable.

$$ \hat{\beta}_{\text{ridge}} = X^\mathsf{T}(XX^\mathsf{T}+\lambda I_n)^{-1}y $$

To verify the identity, note that \((X^\mathsf{T}X+\lambda I_p)X^\mathsf{T}=X^\mathsf{T}(XX^\mathsf{T}+\lambda I_n)\). Both matrices to be inverted are positive definite for \(\lambda>0\). Multiplying by their inverses gives the stated equality. The dual formula does not change the fit or the penalty; it expresses the same coefficient vector through an observation-sized system.

Worked Example: Computing a Ridge Fit With Three Predictors and Two Cases

Suppose the hypothetical, uncentered data are \(X=\begin{pmatrix}1&0&1\\0&1&0\end{pmatrix}\) and \(y=\begin{pmatrix}2\\1\end{pmatrix}\). Fit ridge regression with \(\lambda=1\), using no intercept.

There are \(n=2\) cases and \(p=3\) predictors. The first and third predictor columns are identical, so \(X^\mathsf{T}X\) is singular. Using the dual formula, \(XX^\mathsf{T}=\begin{pmatrix}2&0\\0&1\end{pmatrix}\), and therefore \(XX^\mathsf{T}+I_2=\begin{pmatrix}3&0\\0&2\end{pmatrix}\).

$$ (XX^\mathsf{T}+I_2)^{-1}y = \begin{pmatrix}1/3&0\\0&1/2\end{pmatrix} \begin{pmatrix}2\\1\end{pmatrix} = \begin{pmatrix}2/3\\1/2\end{pmatrix} $$

Multiplying by \(X^\mathsf{T}\) gives \(\hat{\beta}_{\text{ridge}}=(2/3,\,1/2,\,2/3)^\mathsf{T}\). The fitted values are \(X\hat{\beta}_{\text{ridge}}=(4/3,\,1/2)^\mathsf{T}\). The two identical columns receive equal ridge coefficients in this example; the unique fit is computable despite the singular unpenalized cross-product matrix.

How Lasso Can Give a Sparse Fit

Lasso replaces the squared-coefficient penalty with a penalty on the sum of absolute coefficient values. We use the objective with a factor of one-half on the residual sum of squares below. Its particular numerical \(\lambda\) scale differs from the ridge convention above, so a value of \(\lambda\) should always be interpreted relative to the stated objective.

Definition: For \(\lambda>0\), a lasso solution is any coefficient vector minimizing \(\frac{1}{2}\|y-X\beta\|^2+\lambda\sum_{j=1}^{p}|\beta_j|\). A coefficient is active if it is nonzero; the set of active predictors is the support of the coefficient vector.

The absolute-value penalty has corners at zero, making exact zero coefficients possible. When \(p>n\), there is an important sparse-solution result: at least one lasso minimizer can be chosen with no more than \(\operatorname{rank}(X)\) nonzero coefficients. Since \(\operatorname{rank}(X)\leq n\), this guarantees the existence of a solution with at most \(n\) active predictors. It does not say that every minimizer must be sparse, or that the active predictors are uniquely determined.

Here is a justification. Take a lasso minimizer and let \(z=X\hat{\beta}\) be its fitted-value vector. Among all coefficient vectors giving this same \(z\), the minimizer must have the smallest possible sum of absolute values; otherwise replacing it with a lower-penalty representation of \(z\) would lower the full lasso objective. Finding a minimum-\(\ell_1\) representation of \(z\) can be written as minimizing \(\sum_j(u_j^++u_j^-)\), subject to \(X(u^+-u^-)=z\) and \(u_j^+,u_j^-\geq0\). A basic feasible optimum for these equality constraints uses at most \(\operatorname{rank}(X)\) positive variables. Also, both \(u_j^+\) and \(u_j^-\) cannot be positive at an optimum, since reducing both by the same small amount leaves the representation unchanged and lowers the objective. Thus the corresponding coefficient vector has at most \(\operatorname{rank}(X)\) nonzero entries. It gives the same fit and penalty as the original minimizer, so it is also a lasso solution.

Worked Example: A Sparse Lasso Solution With More Predictors Than Cases

Use the same hypothetical \(X\) and \(y\) as above, with no intercept, but fit lasso using \(\frac{1}{2}\|y-X\beta\|^2+0.5\sum_j|\beta_j|\). Write \(a=\beta_1+\beta_3\) and \(c=\beta_2\). The fitted values are \((a,c)^\mathsf{T}\), because the first and third columns are duplicates.

For any fixed \(a\), the smallest possible value of \(|\beta_1|+|\beta_3|\) is \(|a|\), achieved, for example, by setting one of the two coefficients to zero when \(a\ne0\). The optimization therefore reduces to minimizing \(\frac{1}{2}(2-a)^2+\frac{1}{2}(1-c)^2+0.5|a|+0.5|c|\).

For \(a>0\), differentiating its part of the objective gives \(a-2+0.5=0\), hence \(a=1.5\); this is consistent with \(a>0\). For \(c>0\), the corresponding equation is \(c-1+0.5=0\), giving \(c=0.5\), also consistent with \(c>0\). One solution is therefore \(\hat{\beta}=(1.5,\,0.5,\,0)^\mathsf{T}\), with two active predictors despite \(p=3>n=2\). Its fitted values are \((1.5,\,0.5)^\mathsf{T}\).

The first and third coefficients need not be allocated this way: any nonnegative \(\beta_1,\beta_3\) that sum to \(1.5\), together with \(\beta_2=0.5\), has the same fit and the same penalty. The lasso objective has multiple coefficient solutions in this example, including sparse ones.

What a Computable Fit Does—and Does Not—Tell You

The ridge inverse guarantees a unique numerical coefficient vector for the stated design and penalty. Lasso can produce exact zeros and has a sparse solution even when predictors outnumber cases. These are useful properties for constructing predictions, but they do not create information absent from the data. In particular, the penalty does not make a predictor’s individual association identifiable when its column is redundant with other columns.

In the example, let \(v=(1,0,-1)^\mathsf{T}\). Since \(Xv=0\), adding any multiple of \(v\) to a coefficient vector leaves all training fitted values unchanged. For ordinary least squares, every vector \((t,1,2-t)^\mathsf{T}\) gives fitted values \((2,1)^\mathsf{T}\). The data distinguish the sum of the first and third coefficients, not their separate contributions. Ridge and lasso select coefficient allocations by their penalties, but that selection should not be mistaken for evidence that one predictor has a uniquely established effect.

Worked Example: Separating a Fitted Value From a Coefficient Claim

For the same two-case data, compare the exact ordinary least-squares fits with a ridge fit at \(\lambda=1\) and one lasso fit at \(\lambda=0.5\). The OLS vector \((t,1,2-t)^\mathsf{T}\), for any real \(t\), gives \(X\beta=(2,1)^\mathsf{T}\). Thus infinitely many coefficient vectors fit the observed outcomes perfectly.

The ridge vector \((2/3,1/2,2/3)^\mathsf{T}\) gives \((4/3,1/2)^\mathsf{T}\), while the sparse lasso vector \((1.5,0.5,0)^\mathsf{T}\) gives \((1.5,0.5)^\mathsf{T}\). These fitted values are determined by their respective optimization problems. But for lasso, \((0,0.5,1.5)^\mathsf{T}\) produces the same fitted values and the same objective as the displayed lasso vector.

Consequently, the data and lasso objective do not uniquely assign the first predictor’s contribution separately from the third’s. A sparse solution is a computational choice among equivalent solutions here, not proof that the third predictor is irrelevant. Any interpretation should distinguish the fitted values on the observed design from claims about individual predictors.

The limitation also matters for inference. Penalized coefficients are not ordinary least-squares estimates: shrinkage changes their values, and lasso selection makes the set of reported coefficients depend on the penalty and data. Standard OLS standard errors and tests do not automatically apply to these coefficients or account for that selection. A unique ridge solution is not, by itself, a confidence interval or a test of an individual coefficient.

Common Mistakes

  • Assuming \(p>n\) makes every penalized fit impossible. The unpenalized cross-product matrix can be singular, but ridge with \(\lambda>0\) has a positive-definite penalized matrix and a unique fit.
  • Claiming lasso always selects a unique set of predictors. Duplicate or dependent columns can yield multiple coefficient solutions. A sparse solution exists, but it need not be unique.
  • Equating a lasso zero with proof of no relationship. A zero is the result of a penalized optimization at a specified \(\lambda\); correlated or redundant predictors can share information.
  • Treating coefficients as separately identified because the algorithm returned numbers. When \(Xv=0\), coefficient vectors differing by \(v\) have identical fitted values. A penalty selects among representations; the observed design may not distinguish their individual contributions.
  • Calling data centered when they are not. If an example uses an intercept or claims centered variables, check column and response means. Otherwise state that the fit is uncentered and specify whether an intercept is included.
Key takeaway: With more predictors than cases, ridge remains uniquely computable for a positive penalty, while lasso can have a solution with at most \(\operatorname{rank}(X)\) active predictors. Neither uniqueness nor sparsity alone establishes that individual coefficients are scientifically identifiable or suitable for ordinary least-squares inference.

Check Your Understanding

Answer each question using the stated design and penalty setup.

  1. Why can \(X^\mathsf{T}X\) be singular when \(p>n\), and why is \(X^\mathsf{T}X+\lambda I_p\) invertible for \(\lambda>0\)?
  2. In the ridge dual formula, what are the dimensions of \(XX^\mathsf{T}+\lambda I_n\), and why might that be useful when \(p\) is much larger than \(n\)?
  3. What does the lasso sparse-solution result guarantee about the number of nonzero coefficients, and what does it not guarantee about uniqueness?
  4. If two predictor columns are identical, which feature of their coefficients can the observed fitted values distinguish: each coefficient separately or their sum?
  5. Why is a nonzero ridge coefficient or a zero lasso coefficient not, on its own, an ordinary least-squares inferential conclusion?