Tutorials › High-Dimensional Genomic Data Analysis › Dual Ridge Regression When P Exceeds N

Penalized Regression for p Much Larger Than n · Tutorial 517 of 795

Dual Ridge Regression When P Exceeds N

Learn why ridge regression can be solved with an \(n\)-by-\(n\) Gram matrix when \(p\) is much larger than \(n\), and how this dual solution relates to coefficient estimates and predictions.

Advanced 12 min read

What You'll Learn

  • State the centered ridge-regression objective and the role of a positive penalty.
  • Derive the sample-space solution using the \(n\)-by-\(n\) Gram matrix.
  • Show how dual coefficients recover feature-space ridge coefficients.
  • Calculate fitted values and a new prediction using the dual representation.
  • Compare matrix sizes, computation costs, and memory requirements when \(p\) greatly exceeds \(n\).
  • Recognize numerical and interpretation pitfalls in dual ridge regression.

Why Solve Ridge Regression in Sample Space?

In high-dimensional genomic data, the number of measured features can be far larger than the number of samples. For example, an expression matrix might have thousands of gene columns but only a few dozen sample rows. Ridge regression remains well-defined in this setting, but its usual feature-space formula involves a \(p\)-by-\(p\) matrix. The dual formulation instead solves a system involving an \(n\)-by-\(n\) matrix, which can be much smaller when \(p\) greatly exceeds \(n\).

The earlier material on the penalized least-squares objective and ridge shrinkage establishes the optimization setup and its relationship to the design matrix. Here the focus is the algebra that moves the computation from feature space to sample space, and what that change does—and does not—save.

Setup and Assumptions

Let \(X\) be an \(n\)-by-\(p\) design matrix, with one row per sample and one column per predictor, and let \(y\) be the \(n\)-vector of responses. For the centered formulation below, each column of \(X\) and the response vector \(y\) have mean zero. If a model includes an intercept, center the predictors and response, estimate the slopes from the centered data, and recover the intercept as \(\bar y-\bar x^\mathsf{T}\hat\beta\). The intercept is not penalized.

Assume that the ridge penalty \(\lambda\) is strictly positive. No full-rank condition on \(X\) is needed. The derivation is an optimization identity for a fixed design matrix; it does not require normally distributed errors. The ridge objective used here is the unaveraged sum of squares plus penalty. If an objective instead uses the mean squared error, its penalty parameter must be scaled accordingly to represent the same fit.

Definition: The centered ridge estimator minimizes \( \|y-X\beta\|_2^2+\lambda\|\beta\|_2^2 \) over the \(p\)-vector \(\beta\), where \(\lambda>0\). The \(n\)-by-\(n\) matrix \(K=XX^\mathsf{T}\), whose entries are inner products between rows of \(X\), is the sample-space Gram matrix. The dual ridge vector is \( \alpha=(K+\lambda I_n)^{-1}y \), and the coefficient vector is \( \hat\beta=X^\mathsf{T}\alpha \).

The penalty makes the minimizer unique: \(X^\mathsf{T}X\) is positive semidefinite, so for every nonzero \(v\), \(v^\mathsf{T}(X^\mathsf{T}X+\lambda I_p)v=\|Xv\|_2^2+\lambda\|v\|_2^2>0\). Thus \(X^\mathsf{T}X+\lambda I_p\) is invertible even when \(p>n\) or predictors are collinear. Likewise, \(K+\lambda I_n\) is invertible.

Deriving the Dual Solution

Differentiating the objective with respect to \(\beta\) and setting the gradient to zero gives the ridge normal equations. Their solution is the feature-space formula:

$$ (X^\mathsf{T}X+\lambda I_p)\hat\beta=X^\mathsf{T}y, \qquad \hat\beta=(X^\mathsf{T}X+\lambda I_p)^{-1}X^\mathsf{T}y. $$

To obtain the sample-space form, first solve the \(n\)-dimensional system \((XX^\mathsf{T}+\lambda I_n)\alpha=y\), then set \(\hat\beta=X^\mathsf{T}\alpha\). This is valid because substituting that coefficient vector into the feature-space normal equations yields:

$$ \begin{aligned} (X^\mathsf{T}X+\lambda I_p)X^\mathsf{T}\alpha &=X^\mathsf{T}(XX^\mathsf{T}+\lambda I_n)\alpha\\ &=X^\mathsf{T}y. \end{aligned} $$

The feature-space normal equations have a unique solution, so \(X^\mathsf{T}\alpha\) must be that solution. This proves the identity

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

The fitted response vector has an especially direct sample-space expression. With \(K=XX^\mathsf{T}\), it is \(\hat y=X\hat\beta=K(K+\lambda I_n)^{-1}y\). Since \(K\) commutes with \(K+\lambda I_n\), the residual vector is \(y-\hat y=\lambda(K+\lambda I_n)^{-1}y\). In computation, solve the linear system for \(\alpha\); do not explicitly calculate a matrix inverse.

Worked Example: A Centered Matrix with More Features Than Samples

Suppose the hypothetical centered data are \(n=2\), \(p=3\), \(X=\begin{pmatrix}1&0&1\\-1&0&-1\end{pmatrix}\), and \(y=(1,-1)^\mathsf{T}\), with \(\lambda=1\). Each column of \(X\) and the response have mean zero. Here \(p>n\), and the columns are not required to be linearly independent.

The Gram matrix and its penalized version are

$$ K=XX^\mathsf{T} =\begin{pmatrix}2&-2\\-2&2\end{pmatrix}, \qquad K+I_2=\begin{pmatrix}3&-2\\-2&3\end{pmatrix}. $$

The determinant of \(K+I_2\) is \(9-4=5\), so its inverse is \( \frac15\begin{pmatrix}3&2\\2&3\end{pmatrix}\). Therefore,

$$ \alpha=(K+I_2)^{-1}y =\frac15\begin{pmatrix}3&2\\2&3\end{pmatrix} \begin{pmatrix}1\\-1\end{pmatrix} =\begin{pmatrix}0.2\\-0.2\end{pmatrix}. $$

Recover the coefficients by multiplying by \(X^\mathsf{T}\):

$$ \hat\beta=X^\mathsf{T}\alpha =\begin{pmatrix}1&-1\\0&0\\1&-1\end{pmatrix} \begin{pmatrix}0.2\\-0.2\end{pmatrix} =\begin{pmatrix}0.4\\0\\0.4\end{pmatrix}. $$

The fitted values are \(X\hat\beta=(0.8,-0.8)^\mathsf{T}\). This agrees with the sample-space expression: \(K\alpha=(0.8,-0.8)^\mathsf{T}\). The coefficients are unique despite the zero predictor column and the fact that \(p>n\), because \(\lambda>0\).

Dual Coordinates and Predictions

The vector \(\alpha\) has one entry per training sample, rather than one entry per predictor. It is a set of weights that expresses the fitted coefficient vector as a linear combination of the training predictor columns: \(\hat\beta=\sum_{i=1}^n\alpha_i x_i\), where \(x_i^\mathsf{T}\) is row \(i\) of \(X\). This representation explains why prediction can also be carried out without first storing the \(p\) coefficients.

For a new centered predictor vector \(x_{\mathrm{new}}\) of length \(p\), its predicted response is \(x_{\mathrm{new}}^\mathsf{T}\hat\beta\). Substitution of the dual expression gives \(x_{\mathrm{new}}^\mathsf{T}X^\mathsf{T}\alpha=(Xx_{\mathrm{new}})^\mathsf{T}\alpha\). The vector \(Xx_{\mathrm{new}}\) contains the inner products between the new sample and each training sample. If an intercept was fitted, add the recovered intercept to this centered prediction.

Worked Example: Predicting from the Dual Vector

Use the centered training data and \(\alpha=(0.2,-0.2)^\mathsf{T}\) from the preceding example. Suppose a hypothetical new centered sample has \(x_{\mathrm{new}}=(1,1,0)^\mathsf{T}\). Its inner products with the two training rows are

$$ Xx_{\mathrm{new}} =\begin{pmatrix}1&0&1\\-1&0&-1\end{pmatrix} \begin{pmatrix}1\\1\\0\end{pmatrix} =\begin{pmatrix}1\\-1\end{pmatrix}. $$

The dual prediction is therefore

$$ \hat y_{\mathrm{new}} =(Xx_{\mathrm{new}})^\mathsf{T}\alpha =(1,-1)\begin{pmatrix}0.2\\-0.2\end{pmatrix} =0.2+0.2=0.4. $$

Directly using the coefficients gives the same result: \(x_{\mathrm{new}}^\mathsf{T}\hat\beta=(1,1,0)(0.4,0,0.4)^\mathsf{T}=0.4\). The agreement checks both the dual prediction identity and the arithmetic.

When the Sample-Space Solve Is Cheaper

The feature-space formula requires solving a system with \(p\) rows and columns; the dual formula requires solving one with \(n\) rows and columns. For dense matrices, a direct factorization has cubic cost in the system dimension. Forming the Gram matrix also has a cost: \(XX^\mathsf{T}\) takes on the order of \(n^2p\) arithmetic operations, while forming \(X^\mathsf{T}X\) takes on the order of \(p^2n\). The dual approach is typically attractive when \(p\) greatly exceeds \(n\), but forming the matrix and producing predictions still require work. Both approaches must access the design matrix, and obtaining all \(p\) coefficients requires an additional multiplication by \(X^\mathsf{T}\).

Worked Example: Comparing Matrix Sizes at Genomic Scale

Consider a hypothetical expression analysis with \(n=40\) samples and \(p=12{,}000\) predictors, and use the unaveraged ridge objective with \(\lambda>0\). The feature-space system is \(12{,}000\)-by-\(12{,}000\); it contains \(12{,}000^2=144{,}000{,}000\) entries. The sample-space system is \(40\)-by-\(40\), with \(40^2=1{,}600\) entries.

If stored as 8-byte numbers, these matrices alone require about \(1.152\) GB and \(12.8\) kB, respectively. The ratio of the system dimensions is \(12{,}000/40=300\); for dense cubic factorization, the corresponding dimension-cubed ratio is \(300^3=27{,}000{,}000\). Forming \(XX^\mathsf{T}\) takes on the order of \(40^2(12{,}000)=19.2\) million multiply-accumulate operations, whereas forming \(X^\mathsf{T}X\) takes on the order of \(12{,}000^2(40)=5.76\) billion.

These counts explain the potential advantage of the dual solve in this setting. They are not a promise of a 27-million-fold faster end-to-end analysis: software, hardware, memory access, repeated tuning fits, and the cost of returning \(p\) coefficients all matter. The smaller linear system is the central computational benefit.

Checks, Alternatives, and Limitations

The feature-space and sample-space formulas are algebraically identical for the same \(X\), \(y\), and \(\lambda\). Choose the system with the smaller dimension as a first computational guide, then consider whether the Gram matrix can be formed and stored efficiently. The feature-space route may be preferable when \(p\) is not large relative to \(n\), or when coefficients are the primary object and the feature-space solve is manageable. The dual route is often preferable when \(p\gg n\), especially when predictions can be computed from sample inner products.

Neither representation removes the modeling choices behind ridge regression. The value of \(\lambda\) still governs shrinkage, predictor scaling still affects how a common penalty acts on coefficients, and a training fit alone does not establish predictive performance. Keep the penalty convention consistent: changing from a sum of squares to an average changes the numerical scale of \(\lambda\). If features or the response were centered for fitting, apply the same training-derived centering to new observations.

Useful diagnostics include checking that the Gram matrix is symmetric up to numerical precision, verifying that the computed solution satisfies \((K+\lambda I_n)\alpha\approx y\), and comparing \(X^\mathsf{T}\alpha\) with a feature-space solution on a small test problem. Large discrepancies usually indicate inconsistent centering, mismatched penalty scaling, a transposed design matrix, or a faulty linear solve. A very small positive \(\lambda\) can also make the solve numerically sensitive when the data contain nearly redundant directions; use a stable factorization and inspect the residual rather than explicitly forming an inverse.

Common pitfall: “There are more predictors than samples, so the feature-space ridge coefficients cannot be unique.” This is true for unpenalized least squares in many such settings, but not for ridge with \(\lambda>0\): \(X^\mathsf{T}X+\lambda I_p\) is positive definite, so the ridge coefficient vector is unique. The dual system is a smaller way to calculate that same vector, not a different estimator.
1
Prepare the data.
Center predictors and response if an unpenalized intercept is to be fitted; apply the same centering convention to future samples.
2
Form the sample Gram matrix.
Compute \(K=XX^\mathsf{T}\), an \(n\)-by-\(n\) matrix.
3
Solve the dual system.
For \(\lambda>0\), solve \((K+\lambda I_n)\alpha=y\) using a linear solver.
4
Use the fitted representation.
Compute \(\hat\beta=X^\mathsf{T}\alpha\) when coefficients are needed, or predict with \((Xx_{\mathrm{new}})^\mathsf{T}\alpha\).

Check Your Understanding

Answer each question, then check the solutions below.

  1. Why is \(K+\lambda I_n\) invertible when \(\lambda>0\), even if the rows of \(X\) are linearly dependent?
  2. For \(n=30\) and \(p=3{,}000\), which system is smaller, and what are the dimensions of the two ridge systems?
  3. What centering should be used when ridge slopes are fitted with an unpenalized intercept?
  4. For a new centered sample, state the dual formula for its fitted response.

Solutions. (1) For any nonzero \(v\), \(v^\mathsf{T}(K+\lambda I_n)v=\|X^\mathsf{T}v\|_2^2+\lambda\|v\|_2^2>0\), so the matrix is positive definite and invertible. (2) The dual system is smaller: it is \(30\)-by-\(30\), whereas the feature-space system is \(3{,}000\)-by-\(3{,}000\). (3) Center the response and every predictor using the training data, fit the slopes to those centered values, and recover the intercept as \(\bar y-\bar x^\mathsf{T}\hat\beta\). (4) The dual prediction is \((Xx_{\mathrm{new}})^\mathsf{T}\alpha\), where \(\alpha=(XX^\mathsf{T}+\lambda I_n)^{-1}y\); add the intercept if predictions are on the original response scale.

Clinical Biostats