Tutorials › Regression Analysis (Undergraduate) › Lambda Conventions Across Software Implementations

Ridge and Lasso Regression · Tutorial 688 of 991

Lambda Conventions Across Software Implementations

Learn to translate lambda values between common penalized-objective conventions and identify when that translation does—and does not—preserve a fit.

Advanced 8 min read

What You'll Learn

  • Write normalized and unnormalized ridge and lasso objectives using a consistent penalty definition
  • Derive the lambda conversion needed to preserve the same penalized fit
  • Account for factors such as one-half in the loss or penalty
  • Explain how exact data replication affects equivalent lambda values
  • Check whether matching lambda values are meaningful across implementations

Why Lambda Values Can Be Hard to Compare

A penalized regression fit balances two quantities: how closely predictions match the observed responses, and how strongly the coefficient vector is penalized. The tuning parameter \(\lambda\) controls that balance. But software packages do not all write the data-fit part of the objective in the same scale. One may use a mean loss, while another uses a sum. As a result, the same numerical value of \(\lambda\) can represent different amounts of penalization.

The key is to compare the whole objective, not just the number printed as lambda. If the loss is multiplied by a constant, the penalty must be multiplied by the same constant to preserve the minimizer. This tutorial develops that relationship for ridge and lasso, works through conversions, and explains why sample size can change the numerical value needed for an equivalent fit.

Two Common Scales for the Objective

Let \(y_i\) be the response and let \(\mathbf{x}_i\) be the predictor vector for observation \(i\). Write the fitted value as \(\beta_0+\mathbf{x}_i^\mathsf{T}\boldsymbol{\beta}\), where the intercept \(\beta_0\) is not penalized. Define the residual sum of squares by

$$ \mathrm{RSS}(\beta_0,\boldsymbol{\beta}) = \sum_{i=1}^{n} \left(y_i-\beta_0-\mathbf{x}_i^\mathsf{T}\boldsymbol{\beta}\right)^2. $$

For clarity, suppose two implementations use the same observations, predictors, response scale, intercept treatment, and coefficient scaling. They differ only in whether the loss is normalized by sample size. A common normalized form is

$$ \frac{1}{2n}\mathrm{RSS}+\lambda_{\mathrm{norm}}P(\boldsymbol{\beta}), $$

and a common unnormalized form is

$$ \frac{1}{2}\mathrm{RSS}+\lambda_{\mathrm{sum}}P(\boldsymbol{\beta}). $$

Here \(P(\boldsymbol{\beta})\) is the penalty, and the subscripts distinguish the two lambda conventions. For lasso, a common choice is \(P(\boldsymbol{\beta})=\sum_j|\beta_j|\). For ridge, a common choice is \(P(\boldsymbol{\beta})=\frac{1}{2}\sum_j\beta_j^2\). The factor of one-half in the ridge penalty is a convention; some objectives omit it. What matters for a conversion is using the same penalty definition on both sides.

Definition: Two penalized objectives give equivalent fits when their objective functions differ only by multiplication by a positive constant that does not depend on the coefficients. Such multiplication preserves the minimizer. For the two objectives above, using the same penalty \(P\), equivalence requires \(\lambda_{\mathrm{sum}}=n\lambda_{\mathrm{norm}}\).

Deriving the Conversion

The normalized objective can be multiplied by \(n\) without changing which coefficient values minimize it, because \(n\) is positive. Multiplying every term by \(n\) gives

$$ n\left\{\frac{1}{2n}\mathrm{RSS} +\lambda_{\mathrm{norm}}P(\boldsymbol{\beta})\right\} = \frac{1}{2}\mathrm{RSS} +n\lambda_{\mathrm{norm}}P(\boldsymbol{\beta}). $$

This is the unnormalized objective when \(\lambda_{\mathrm{sum}}=n\lambda_{\mathrm{norm}}\). The two objectives therefore have exactly the same minimizers under the stated conditions. This is an algebraic equivalence, not an approximation.

The conversion also illustrates why the word “lambda” alone does not specify a universal numerical scale. More generally, suppose two objectives are \(c_A\mathrm{RSS}+\lambda_A P\) and \(c_B\mathrm{RSS}+\lambda_B P\), with the same penalty. Dividing each objective by its loss coefficient shows that they have the same relative penalty strength when

$$ \frac{\lambda_A}{c_A}=\frac{\lambda_B}{c_B}. $$

Thus the sample-size conversion applies specifically to the displayed pair of loss scales. If an implementation uses \(\mathrm{RSS}\) instead of \(\mathrm{RSS}/2\), or uses a penalty with a different constant factor, the conversion changes accordingly.

Worked Example: Converting a Normalized Lambda to an Unnormalized Lambda

Suppose a hypothetical analysis has \(n=80\) observations and uses the normalized objective \(\mathrm{RSS}/(2n)+\lambda_{\mathrm{norm}}P\), with \(\lambda_{\mathrm{norm}}=0.15\). What lambda gives the equivalent fit under the objective \(\mathrm{RSS}/2+\lambda_{\mathrm{sum}}P\)?

Check the conditions. Assume the same 80 observations, response and predictor scales, unpenalized intercept, and penalty definition are used in both objectives. The only change is replacing the normalized loss with the unnormalized loss.

Apply the conversion.

$$ \lambda_{\mathrm{sum}} =n\lambda_{\mathrm{norm}} =80(0.15) =12. $$

Multiplying the first objective by 80 confirms the result:

$$ 80\left\{\frac{\mathrm{RSS}}{160}+0.15P\right\} =\frac{\mathrm{RSS}}{2}+12P. $$

Therefore, lambda \(0.15\) in the normalized convention and lambda \(12\) in the unnormalized convention produce the same minimizers under these assumptions. The numerical difference does not mean that one fit is more strongly penalized; the loss and penalty are expressed on different scales.

Check the Loss and Penalty Factors

A reliable conversion starts by writing down both objectives, including every constant. For example, \(\mathrm{RSS}/n+\lambda P\) and \(\mathrm{RSS}/(2n)+\lambda P\) do not use the same relative scale for the penalty at the same numerical lambda. The loss coefficient in the first objective is twice as large, so its lambda must also be twice as large to match the second objective.

This rule is about relative scaling, not a special property of lasso or ridge. It works whenever the same penalty function is used in both objectives. However, if one implementation defines the ridge penalty as \(\sum_j\beta_j^2\) and another as \(\frac{1}{2}\sum_j\beta_j^2\), their penalty functions themselves differ by a factor of two. That difference must be included along with any change in loss scale.

Worked Example: Matching Two Lasso Loss Conventions

Consider a hypothetical lasso fit with \(n=20\). Implementation A minimizes \(\mathrm{RSS}/(2n)+\lambda_A\sum_j|\beta_j|\), while Implementation B minimizes \(\mathrm{RSS}/n+\lambda_B\sum_j|\beta_j|\). If \(\lambda_A=0.25\), find the value of \(\lambda_B\) that gives the same fit.

Compare the loss coefficients. In A, the coefficient of RSS is \(1/(2n)\); in B, it is \(1/n\), twice as large. The penalty must also be twice as large in B. Equivalently, multiply A's entire objective by 2:

$$ 2\left\{\frac{\mathrm{RSS}}{2n} +0.25\sum_j|\beta_j|\right\} = \frac{\mathrm{RSS}}{n} +0.50\sum_j|\beta_j|. $$

Thus \(\lambda_B=0.50\). The value is not \(n(0.25)=5\), because Implementation B did not switch from a loss divided by \(2n\) to one divided by 2. Its loss is divided by \(n\); the ratio between the two loss coefficients is only 2.

Verify with a one-coefficient calculation. Suppose the predictor is centered and the hypothetical data have \(\sum_i x_i^2=40\) and \(\sum_i x_i y_i=30\). For the normalized objective \(\mathrm{RSS}/(2n)+\lambda_A|\beta|\), the derivative on a positive-coefficient solution is

$$ -\frac{\sum_i x_i y_i}{n} +\frac{\sum_i x_i^2}{n}\beta+\lambda_A=0. $$

Substituting \(n=20\) and \(\lambda_A=0.25\) gives \(-1.5+2\beta+0.25=0\), so \(\beta=0.625\). Under Implementation B, the derivative is \(-2\sum_i x_i y_i/n+2\sum_i x_i^2\beta/n+\lambda_B=0\). With \(\lambda_B=0.50\), it becomes \(-3+4\beta+0.50=0\), again giving \(\beta=0.625\). The matching coefficient confirms the scaling argument for this positive solution.

Why Sample Size Changes the Numerical Lambda

The factor of \(n\) has a direct interpretation. A sum loss grows with the number of observations, while a mean loss divides that sum by \(n\). If the penalty is left unchanged as the loss is rescaled, their relative influence changes. Consequently, holding a numerical lambda fixed under an unnormalized sum-loss convention does not generally have the same meaning as holding it fixed under a normalized mean-loss convention when sample size changes.

A useful exact illustration is to replicate every observation the same number of times. The replicated data have a larger sample size and a proportionally larger RSS at every fixed coefficient vector. Their mean squared-error contribution is unchanged, while their sum-loss contribution is multiplied by the replication count. Thus, under the normalized convention, the same lambda yields the same objective and fit; under the unnormalized convention, lambda must increase by the replication count to preserve the same relative penalty.

Worked Example: Exact Replication in a Ridge Fit

Suppose a hypothetical centered one-predictor data set has \(n=10\), \(\sum_i x_i^2=20\), and \(\sum_i x_i y_i=10\). Use the ridge penalty \(P(\beta)=\beta^2/2\) and \(\lambda_{\mathrm{norm}}=0.4\). Compare the normalized fit with the equivalent unnormalized fit, then replicate every observation three times.

Fit the normalized objective. The objective is \(\mathrm{RSS}/(2n)+\lambda_{\mathrm{norm}}\beta^2/2\). Differentiating with respect to \(\beta\) and setting the derivative to zero gives

$$ -\frac{\sum_i x_i y_i}{n} +\frac{\sum_i x_i^2}{n}\beta +\lambda_{\mathrm{norm}}\beta=0, \qquad \hat{\beta} = \frac{\sum_i x_i y_i/n} {\sum_i x_i^2/n+\lambda_{\mathrm{norm}}}. $$

Substitution gives \(\hat{\beta}=(10/10)/(20/10+0.4)=1/2.4=0.4167\), to four significant figures.

Convert to the unnormalized convention. Here \(\lambda_{\mathrm{sum}}=n\lambda_{\mathrm{norm}}=10(0.4)=4\). The unnormalized objective is \(\mathrm{RSS}/2+4\beta^2/2\), whose derivative gives

$$ \hat{\beta} = \frac{\sum_i x_i y_i}{\sum_i x_i^2+\lambda_{\mathrm{sum}}} = \frac{10}{20+4} = 0.4167. $$

Replicate each row three times. The new sample size is \(30\), \(\sum_i x_i^2=60\), and \(\sum_i x_i y_i=30\). The normalized fit remains \((30/30)/(60/30+0.4)=1/2.4=0.4167\). For the unnormalized objective, the equivalent lambda is \(30(0.4)=12\), giving \(\hat{\beta}=30/(60+12)=0.4167\). Keeping the old unnormalized lambda \(4\) instead would give \(30/(60+4)=0.4688\), a different fit. Exact replication justifies this comparison; for a genuinely new sample, the data summaries and resulting fit need not be the same.

What Must Match Before Comparing Lambda

The algebraic conversion isolates loss normalization, but software implementations can differ in other ways as well. A conversion based only on \(n\) is valid only when the rest of the objective is aligned. Before treating two lambda values as equivalent, check:

  • Loss definition: Is the loss RSS, RSS divided by \(n\), or RSS divided by \(2n\)?
  • Penalty definition: Is there a factor such as one-half in the ridge penalty, and are the same coefficients included?
  • Intercept treatment: Is the intercept excluded from the penalty in both fits?
  • Predictor and response scales: Were the predictors and response transformed in the same way before fitting?
  • Data and weights: Are the same observations used, with the same observation weights and missing-data handling?

If one of these differs, the relationship \(\lambda_{\mathrm{sum}}=n\lambda_{\mathrm{norm}}\) may not apply by itself. For example, standardizing predictors changes the units in which coefficients—and therefore their penalties—are expressed. Matching the loss scale cannot undo a difference in predictor scaling.

Lambda values are therefore best compared only after the objective has been written in full. When objectives are genuinely equivalent, the conversion can be calculated exactly. When important details are unknown, matching the printed numbers is not evidence of matching fits; compare the definitions and, where appropriate, the resulting coefficients or predictions.

Common Mistake

Wrong reasoning: “Both packages report lambda \(=0.2\), so they apply the same penalty.” This ignores the loss scale. If one package uses \(\mathrm{RSS}/(2n)+0.2P\) and the other uses \(\mathrm{RSS}/2+0.2P\), the objectives generally do not have the same minimizer. With the stated conventions, the equivalent unnormalized lambda is \(0.2n\), not \(0.2\).

Correction: Write both objective functions, identify the loss coefficient and penalty definition in each, and solve for the lambda values that make the objectives proportional. State the sample size and other conditions used in the conversion. A lambda value has meaning through its place in a complete objective, not on its own.

Key takeaway: For \(\mathrm{RSS}/(2n)+\lambda_{\mathrm{norm}}P\) and \(\mathrm{RSS}/2+\lambda_{\mathrm{sum}}P\), with all other choices held fixed, equivalent fits require \(\lambda_{\mathrm{sum}}=n\lambda_{\mathrm{norm}}\). Always check both the loss and penalty conventions before comparing software lambda values.

Check Your Understanding

Use the objective definitions in each question, and state any assumptions needed for a conversion.

  1. For \(n=50\), what unnormalized lambda corresponds to \(\lambda_{\mathrm{norm}}=0.08\) when the objectives are \(\mathrm{RSS}/(2n)+\lambda_{\mathrm{norm}}P\) and \(\mathrm{RSS}/2+\lambda_{\mathrm{sum}}P\)?
  2. An implementation changes its loss from \(\mathrm{RSS}/(2n)\) to \(\mathrm{RSS}/n\), leaving the penalty definition unchanged. By what factor must lambda change to preserve the fit?
  3. Why can two implementations with the same numerical lambda produce different fits even if they both use lasso?
  4. If every observation is replicated twice, what happens to the lambda needed for an equivalent fit under the unnormalized loss convention?
  5. Name two implementation choices, other than loss normalization, that should be checked before comparing lambda values.