4
$\begingroup$

Two formulations of the elastic net regression function

Consider sklearn's implementation of elastic net regularisation (Wikipedia link). From the docs, it works by minimising the following objective function:

1 / (2 * n_samples) * ||y - Xw||^2_2 [OLS part]
+ alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2 [penalty part]

Comparing this to the objective function from the Wikipedia article:

$\hat{\beta} \equiv \underset{\beta}{\operatorname{argmin}} (\| y-X \beta \|^2 + \lambda_2 \|\beta\|^2 + \lambda_1 \|\beta\|_1)$

They are very similar: alpha * l1_ratio equates to $\lambda_1$, 0.5 * alpha * (1 - l1_ratio) equates to $\lambda_2$. But there's a difference in the 'OLS part' of the objective function - in the sklearn implementation, we divide by 2 * n_samples.

Differences between the cross-validated models

Now, this does not necessarily matter, because the regularisation hyperparameters are free - so we can get the same results as the Wikipedia formula by multiplying the objective function by 2 * n_samples. But I'd argue this leads to misleading results when cross-validating.

Suppose we have a dataset composed of 1000 observations. We want to do a Ridge regression (so we ignore the L1 penalty term by setting l1_ratio or $\lambda_1$ to 0) of one column y on the rest of the columns X. First, in order to find the best hyperparameter $\tilde \lambda$ to use, we do a 5-fold cross-validation using this dataset (so each fold has 800 training observations and 200 test observations).

For this we use the sklearn method, and find that $\tilde \lambda = 1$. Out of interest, we try again using the Wikipedia method, and find that (as Sycorax suggested) the best hyperparameter ($\lambda^{*}$) under this method is just a rescaled version of $\tilde \lambda$. In fact, $\lambda^{*} = 2n \tilde \lambda = 1600$. In each cross-validation training set, the models under the different methods are the same when trained with the respective optimal hyperparameters.

Now, let's see what happens when we try to train these models (with the associated hyperparameters) on the whole dataset ($n = 1000$). We can write down the respective objective functions:

sklearn:

$\hat{\beta} \equiv \underset{\beta}{\operatorname{argmin}} (\| y-X \beta \|^2/2n + \tilde \lambda \|\beta\|^2)$

$\hat{\beta} \equiv \underset{\beta}{\operatorname{argmin}} (\| y-X \beta \|^2/2000 + \|\beta\|^2)$

Wikipedia:

$\hat{\beta} \equiv \underset{\beta}{\operatorname{argmin}} (\| y-X \beta \|^2 + \lambda^{*} \|\beta\|^2)$

$\hat{\beta} \equiv \underset{\beta}{\operatorname{argmin}} (\| y-X \beta \|^2 + 1600 \|\beta\|^2)$

Clearly the values of $\hat{\beta}$ that we obtain from these models will be different - in particular, those from the sklearn model will be more strongly regularised.

The sklearn implementation is less suited for cross-validation problems

Therefore the sklearn and Wikipedia methods result in different models, when selecting the optimal hyperparameter based on cross-validation on a subsample of the dataset. Which one makes more sense?

I'd argue that it's the Wikipedia version. Ceteris paribus the more data you have, the lower variance your predictions, and so the less you want to regularise. The Wikipedia approach - in which the OLS part of the objective function is allowed to scale with the number of samples, rather than being an average - means that this happens automatically as the size of your dataset increases, without having to change the regularisation parameter. This means that it is better suited to cases where you select the optimal parameter on a subset of data, as with cross-validation.

$\endgroup$
3
  • 1
    $\begingroup$ Because the discrepancy boils down to "[the coefficients] from the sklearn model will be more strongly regularised", it seems worth a few experiments to see when/whether that's better. $\endgroup$ Commented May 19, 2022 at 15:02
  • $\begingroup$ @Richard Hardy - no, sorry. I didn't even realise I'd done that! $\endgroup$
    – tobmo
    Commented May 19, 2022 at 17:21
  • $\begingroup$ @BenReiniger - agreed. My intuition is you'd much rather your regularisation strength diminishes as you add more data. I'm going to play around with some different implementations and see if I can prove this. $\endgroup$
    – tobmo
    Commented May 19, 2022 at 17:22

1 Answer 1

3
$\begingroup$

It's correct that when the sample size is fixed, there is not a difference between the two statements of the optimization problem.

Your demonstration in the revised question makes it clear that the choice of objective function implies that $\lambda$ selected from one sample size is not "portable" to a different sample size.

The question then becomes "What can we do about this?" On the one hand, cross-validation is the preferred way to estimate the optimal $\lambda$. On the other hand, the two alternative statements of the optimization task will yield two alternative choices of "optimal" $\lambda$. You could try to re-estimate $\lambda$ on the whole data set, but this begs the question because using the whole data set implies that out-of-sample data is not available to assess the quality of fit.

$\endgroup$
11
  • $\begingroup$ Thanks for the clear explanation of the equivalence of these methods. I'm not denying that they are identical, save that their parameters are on different scales. I'm arguing that - specifically when we try to identify the correct parameter through cross-validation - the performance of our model will be dependent on which method we use. $\endgroup$
    – tobmo
    Commented May 18, 2022 at 19:53
  • $\begingroup$ The hyperparameters are on different scales. The parameters $\beta$ are identical. $\endgroup$
    – Sycorax
    Commented May 18, 2022 at 19:55
  • 2
    $\begingroup$ The question is a little hard to follow, but it seems to me its focus is clear: when you estimate the regularization coefficients ("parameters") $\lambda_i$ based on a subset of the data (either in a training set or during cross-validation), then two sets of estimates made on subsets of different sizes will be directly comparable only with the sklearn (mean squared error) formulation. But I don't see why there's any concern: we're not usually interested in these coefficients; we only use them to help develop a model--in which the coefficients don't appear at all. $\endgroup$
    – whuber
    Commented May 18, 2022 at 20:04
  • 1
    $\begingroup$ The model eventually uses a subset of the variables along with their regularized slope estimates. The regularization parameters play no further role in determining what response the model might eventually predict for any set of explanatory values. $\endgroup$
    – whuber
    Commented May 18, 2022 at 21:18
  • 2
    $\begingroup$ I agree. The issue is whether, upon training models on the whole dataset, you are willing to re-estimate the hyperparameters in the process or not. $\endgroup$
    – whuber
    Commented May 19, 2022 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.