```
import pandas as pd
import numpy as np
import sqlite3
from plotnine import *
from mizani.formatters import percent_format, date_format
from mizani.breaks import date_breaks
from itertools import product
from sklearn.model_selection import (
train_test_split, GridSearchCV,
TimeSeriesSplit, cross_val_score
)from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import (
ElasticNet, Lasso, Ridge )
```

# Factor Selection via Machine Learning

You are reading the work-in-progress edition of **Tidy Finance with Python**. Code chunks and text might change over the next couple of months. We are always looking for feedback via contact@tidy-finance.org. Meanwhile, you can find the complete R version here.

The aim of this chapter is twofold. From a data science perspective, we introduce `scikit-learn`

, a collection of packages for modeling and machine learning (ML). `scikit-learn`

comes with a handy workflow for all sorts of typical prediction tasks. From a finance perspective, we address the notion of *factor zoo* (Cochrane 2011) using ML methods. We introduce Lasso and Ridge regression as a special case of penalized regression models. Then, we explain the concept of cross-validation for model *tuning* with Elastic Net regularization as a popular example. We implement and showcase the entire cycle from model specification, training, and forecast evaluation within the `scikit-learn`

universe. While the tools can generally be applied to an abundance of interesting asset pricing problems, we apply penalized regressions for identifying macroeconomic variables and asset pricing factors that help explain a cross-section of industry portfolios.

In previous chapters, we illustrate that stock characteristics such as size provide valuable pricing information in addition to the market beta. Such findings question the usefulness of the Capital Asset Pricing Model. In fact, during the last decades, financial economists discovered a plethora of additional factors which may be correlated with the marginal utility of consumption (and would thus deserve a prominent role in pricing applications). The search for factors that explain the cross-section of expected stock returns has produced hundreds of potential candidates, as noted more recently by Harvey, Liu, and Zhu (2016), Mclean and Pontiff (2016), and Hou, Xue, and Zhang (2020). Therefore, given the multitude of proposed risk factors, the challenge these days rather is: *do we believe in the relevance of 300+ risk factors?* During recent years, promising methods from the field of ML got applied to common finance applications. We refer to Mullainathan and Spiess (2017) for a treatment of ML from the perspective of an econometrician, Nagel (2021) for an excellent review of ML practices in asset pricing, Easley et al. (2020) for ML applications in (high-frequency) market microstructure, and Dixon, Halperin, and Bilokon (2020) for a detailed treatment of all methodological aspects.

## Brief Theoretical Background

This is a book about *doing* empirical work in a tidy manner, and we refer to any of the many excellent textbook treatments of ML methods and especially penalized regressions for some deeper discussion. Excellent material is provided, for instance, by Hastie, Tibshirani, and Friedman (2009), Gareth et al. (2013), and De Prado (2018). Instead, we briefly summarize the idea of Lasso and Ridge regressions as well as the more general Elastic Net. Then, we turn to the fascinating question on *how* to implement, tune, and use such models with the `scikit-learn`

package.

To set the stage, we start with the definition of a linear model: suppose we have data \((y_t, x_t), t = 1,\ldots, T\), where \(x_t\) is a \((K \times 1)\) vector of regressors and \(y_t\) is the response for observation \(t\). The linear model takes the form \(y_t = \beta' x_t + \varepsilon_t\) with some error term \(\varepsilon_t\) and has been studied in abundance. The well-known ordinary-least square (OLS) estimator for the \((K \times 1)\) vector \(\beta\) minimizes the sum of squared residuals and is then \[\hat{\beta}^\text{ols} = \left(\sum\limits_{t=1}^T x_t'x_t\right)^{-1} \sum\limits_{t=1}^T x_t'y_t.\]

While we are often interested in the estimated coefficient vector \(\hat\beta^\text{ols}\), ML is about the predictive performance most of the time. For a new observation \(\tilde{x}_t\), the linear model generates predictions such that \[\hat y_t = E\left(y|x_t = \tilde x_t\right) = \hat\beta^\text{ols}{}' \tilde x_t.\] Is this the best we can do? Not really: instead of minimizing the sum of squared residuals, penalized linear models can improve predictive performance by choosing other estimators \(\hat{\beta}\) with lower variance than the estimator \(\hat\beta^\text{ols}\). At the same time, it seems appealing to restrict the set of regressors to a few meaningful ones if possible. In other words, if \(K\) is large (such as for the number of proposed factors in the asset pricing literature), it may be a desirable feature to *select* reasonable factors and set \(\hat\beta^{\text{ols}}_k = 0\) for some redundant factors.

It should be clear that the promised benefits of penalized regressions, i.e., reducing the mean squared error (MSE), come at a cost. In most cases, reducing the variance of the estimator introduces a bias such that \(E\left(\hat\beta\right) \neq \beta\). What is the effect of such a bias-variance trade-off? To understand the implications, assume the following data-generating process for \(y\): \[y = f(x) + \varepsilon, \quad \varepsilon \sim (0, \sigma_\varepsilon^2)\] We want to recover \(f(x)\), which denotes some unknown functional which maps the relationship between \(x\) and \(y\). While the properties of \(\hat\beta^\text{ols}\) as an unbiased estimator may be desirable under some circumstances, they are certainly not if we consider predictive accuracy. Alternative predictors \(\hat{f}(x)\) could be more desirable: For instance, the MSE depends on our model choice as follows: \[\begin{aligned} MSE &=E((y-\hat{f}(x))^2)=E((f(x)+\epsilon-\hat{f}(x))^2)\\ &= \underbrace{E((f(x)-\hat{f}(x))^2)}_{\text{total quadratic error}}+\underbrace{E(\epsilon^2)}_{\text{irreducible error}} \\ &= E\left(\hat{f}(x)^2\right)+E\left(f(x)^2\right)-2E\left(f(x)\hat{f}(x)\right)+\sigma_\varepsilon^2\\ &=E\left(\hat{f}(x)^2\right)+f(x)^2-2f(x)E\left(\hat{f}(x)\right)+\sigma_\varepsilon^2\\ &=\underbrace{\text{Var}\left(\hat{f}(x)\right)}_{\text{variance of model}}+ \underbrace{E\left((f(x)-\hat{f}(x))\right)^2}_{\text{squared bias}} +\sigma_\varepsilon^2. \end{aligned}\] While no model can reduce \(\sigma_\varepsilon^2\), a biased estimator with small variance may have a lower MSE than an unbiased estimator.

### Ridge regression

One biased estimator is known as Ridge regression. Hoerl and Kennard (1970) propose to minimize the sum of squared errors *while simultaneously imposing a penalty on the \(L_2\) norm of the parameters* \(\hat\beta\). Formally, this means that for a penalty factor \(\lambda\geq 0\) the minimization problem takes the form \(\min_\beta \left(y - X\beta\right)'\left(y - X\beta\right)\text{ s.t. } \beta'\beta \leq c\). Here \(c\geq 0\) is a constant that depends on the choice of \(\lambda\). The larger \(\lambda\), the smaller \(c\) (technically speaking, there is a one-to-one relationship between \(\lambda\), which corresponds to the Lagrangian of the minimization problem above and \(c\)). Here, \(X = \left(x_1 \ldots x_T\right)'\) and \(y = \left(y_1, \ldots, y_T\right)'\). A closed-form solution for the resulting regression coefficient vector \(\beta^\text{ridge}\) exists: \[\hat{\beta}^\text{ridge} = \left(X'X + \lambda I\right)^{-1}X'y.\] A couple of observations are worth noting: \(\hat\beta^\text{ridge} = \hat\beta^\text{ols}\) for \(\lambda = 0\) and \(\hat\beta^\text{ridge} \rightarrow 0\) for \(\lambda\rightarrow \infty\). Also for \(\lambda > 0\), \(\left(X'X + \lambda I\right)\) is non-singular even if \(X'X\) is which means that \(\hat\beta^\text{ridge}\) exists even if \(\hat\beta\) is not defined. However, note also that the Ridge estimator requires careful choice of the hyperparameter \(\lambda\) which controls the *amount of regularization*: a larger value of \(\lambda\) implies *shrinkage* of the regression coefficient toward 0, a smaller value of \(\lambda\) reduces the bias of the resulting estimator.

Note, that \(X\) usually contains an intercept column with ones. As a general rule, the associated intercept coefficient is not penalized. In practice, this often implies that \(y\) is simply demeaned before computing \(\hat\beta^\text{ridge}\).

What about the statistical properties of the Ridge estimator? First, the bad news is that \(\hat\beta^\text{ridge}\) is a biased estimator of \(\beta\). However, the good news is that (under homoscedastic error terms) the variance of the Ridge estimator is guaranteed to be *smaller* than the variance of the ordinary least square estimator. We encourage you to verify these two statements in the exercises. As a result, we face a trade-off: The Ridge regression sacrifices some unbiasedness to achieve a smaller variance than the OLS estimator.

### Lasso

An alternative to Ridge regression is the Lasso (*l*east *a*bsolute *s*hrinkage and *s*election *o*perator). Similar to Ridge regression, the Lasso (Tibshirani 1996) is a penalized and biased estimator. The main difference to Ridge regression is that Lasso does not only *shrink* coefficients but effectively selects variables by setting coefficients for *irrelevant* variables to zero. Lasso implements a \(L_1\) penalization on the parameters such that: \[\hat\beta^\text{Lasso} = \arg\min_\beta \left(Y - X\beta\right)'\left(Y - X\beta\right)\text{ s.t. } \sum\limits_{k=1}^K|\beta_k| < c(\lambda).\] There is no closed form solution for \(\hat\beta^\text{Lasso}\) in the above maximization problem but efficient algorithms exist (e.g., the R package `glmnet`

). Like for Ridge regression, the hyperparameter \(\lambda\) has to be specified beforehand.

### Elastic Net

The Elastic Net (Zou and Hastie 2005) combines \(L_1\) with \(L_2\) penalization and encourages a grouping effect, where strongly correlated predictors tend to be in or out of the model together. This more general framework considers the following optimization problem: \[\hat\beta^\text{EN} = \arg\min_\beta \left(Y - X\beta\right)'\left(Y - X\beta\right) + \lambda(1-\rho)\sum\limits_{k=1}^K|\beta_k| +\frac{1}{2}\lambda\rho\sum\limits_{k=1}^K\beta_k^2\] Now, we have to chose two hyperparameters: the *shrinkage* factor \(\lambda\) and the *weighting parameter* \(\rho\). The Elastic Net resembles Lasso for \(\rho = 0\) and Ridge regression for \(\rho = 1\). While the R package `glmnet`

provides efficient algorithms to compute the coefficients of penalized regressions, it is a good exercise to implement Ridge and Lasso estimation on your own before you use the `scikit-learn`

back-end.

## Data Preparation

To get started, we load the required packages and data. The main focus is on the workflow behind the `scikit-learn`

(Pedregosa et al. 2011) package collection.

In this analysis, we use four different data sources that we load from our `SQLite`

-database introduced in Accessing and Managing Financial Data. We start with two different sets of factor portfolio returns which have been suggested as representing practical risk factor exposure and thus should be relevant when it comes to asset pricing applications.

- The standard workhorse: monthly Fama-French 3 factor returns (market, small-minus-big, and high-minus-low book-to-market valuation sorts) defined in Fama and French (1992) and Fama and French (1993)
- Monthly q-factor returns from Hou, Xue, and Zhang (2014). The factors contain the size factor, the investment factor, the return-on-equity factor, and the expected growth factor

Next, we include macroeconomic predictors which may predict the general stock market economy. Macroeconomic variables effectively serve as conditioning information such that their inclusion hints at the relevance of conditional models instead of unconditional asset pricing. We refer the interested reader to Cochrane (2009) on the role of conditioning information.

- Our set of macroeconomic predictors comes from Welch and Goyal (2008). The data has been updated by the authors until 2021 and contains monthly variables that have been suggested as good predictors for the equity premium. Some of the variables are the dividend price ratio, earnings price ratio, stock variance, net equity expansion, treasury bill rate, and inflation

Finally, we need a set of *test assets*. The aim is to understand which of the plenty factors and macroeconomic variable combinations prove helpful in explaining our test assets’ cross-section of returns. In line with many existing papers, we use monthly portfolio returns from 10 different industries according to the definition from Kenneth French’s homepage as test assets.

```
= sqlite3.connect(
tidy_finance ="data/tidy_finance_python.sqlite"
database
)
= (
factors_ff3_monthly
pd.read_sql_query(="SELECT * FROM factors_ff3_monthly",
sql=tidy_finance,
con={"month"}
parse_dates
)
.dropna()"factor_ff_")
.add_prefix(
)
= (
factors_q_monthly
pd.read_sql_query(="SELECT * FROM factors_q_monthly",
sql=tidy_finance,
con={"month"}
parse_dates
)
.dropna()"factor_q_")
.add_prefix(
)
= (
macro_predictors
pd.read_sql_query(="SELECT * FROM macro_predictors",
sql=tidy_finance,
con={"month"}
parse_dates
)
.dropna()"macro_")
.add_prefix(="macro_rp_div")
.drop(columns
)
= (
industries_ff_monthly
pd.read_sql_query(="SELECT * FROM industries_ff_monthly",
sql=tidy_finance,
con={"month"}
parse_dates
)
.dropna()="month",
.melt(id_vars="industry",
var_name="ret")
value_name )
```

We combine all the monthly observations into one data frame.

```
= (industries_ff_monthly
data
.merge(factors_ff3_monthly, ="left",
how="month",
left_on="factor_ff_month")
right_on
.merge(factors_q_monthly, ="left",
how="month",
left_on="factor_q_month")
right_on
.merge(macro_predictors, ="left",
how="month",
left_on="macro_month")
right_on=lambda x: x["ret"] - x["factor_ff_rf"])
.assign(ret_excess=["ret", "factor_ff_month",
.drop(columns"factor_q_month", "macro_month"])
.dropna() )
```

Our data contains 22 columns of regressors with the 13 macro variables and 8 factor returns for each month. Figure 1 provides summary statistics for the 10 monthly industry excess returns in percent.

```
= (ggplot(data,
data_plot ="industry", y="ret_excess")) +
aes(x+
geom_boxplot() +
coord_flip() ="", y="",
labs(x="Excess return distributions by industry in percent") +
title=percent_format())
scale_y_continuous(labels
) data_plot.draw()
```

## The Machine Learning Workflow

To illustrate penalized linear regressions, we employ the `scikit-learn`

collection of packages for modeling and ML. Using the ideas of Ridge and Lasso regressions, the following example guides you through (i) pre-processing the data (data split and variable mutation), (ii) building models, (iii) fitting models, and (iv) tuning models to create the “best” possible predictions.

### Pre-process data

We want to explain excess returns with all available predictors. The regression equation thus takes the form \[r_{t} = \alpha_0 + \left(\tilde f_t \otimes \tilde z_t\right)B + \varepsilon_t \] where \(r_t\) is the vector of industry excess returns at time \(t\) and \(\tilde f_t\) and \(\tilde z_t\) are the (standardized) vectors of factor portfolio returns and macroeconomic variables.

We hence perform the following pre-processing steps:

- We exclude the column
*month*from the analysis - We include all interaction terms between factors and macroeconomic predictors
- We demean and scale each regressor such that the standard deviation is one

Scaling is often necessary in machine learning applications, especially when combining variables of different magnitudes or units, or when using algorithms sensitive to feature scales (e.g., gradient descent-based algorithms). We use `ColumnTransformer()`

to scale all regressors using `StandardScaler()`

. The `remainder="drop"`

ensures that only the specified columns are retained in the output, and others are dropped. The option `verbose_feature_names_out=False`

ensures that the output feature names remain unchanged.

```
= data.filter(like = "macro").columns
macro_variables = data.filter(like = "factor").columns
factor_variables
= list(
column_combinations
product(macro_variables, factor_variables)
)
= []
new_column_values for macro_column, factor_column in column_combinations:
new_column_values.append(* data[factor_column]
data[macro_column]
)
= [" x ".join(t) for t in column_combinations]
column_names = pd.DataFrame(
new_columns dict(zip(column_names, new_column_values))
)
= pd.concat([data, new_columns], axis=1)
data
= ColumnTransformer(
preprocessor =[
transformers"scale", StandardScaler(),
(for col in data.columns
[col if col not in ["ret_excess", "month", "industry"]])
],="drop",
remainder=False
verbose_feature_names_out )
```

### Build a model

Next, we can build an actual model based on our pre-processed data. In line with the definition above, we estimate regression coefficients of a Lasso regression such that we get

\[\begin{aligned}\hat\beta_\lambda^\text{Lasso} = \arg\min_\beta \left(Y - X\beta\right)'\left(Y - X\beta\right) + \lambda\sum\limits_{k=1}^K|\beta_k|.\end{aligned}\]

We want to emphasize that the workflow for *any* model is very similar, irrespective of the specific model. As you will see further below, it is straightforward to fit Ridge regression coefficients and - later - Neural networks or Random forests with similar code. For now, we start with the linear regression model with a given value for the penalty factor \(\lambda\) (denoted as `alpha=0.007`

in the code below). In the setup below, `l1_ratio`

denotes the value of \(1-\rho\), hence setting `l1_ratio=1`

implies the Lasso.

```
= ElasticNet(
lm_model =0.007,
alpha=1,
l1_ratio=5000,
max_iter=False
fit_intercept
)
= Pipeline([
lm_pipeline "preprocessor", preprocessor),
("regressor", lm_model)
( ])
```

That’s it - we are done! The object `lm_model_pipeline`

contains the definition of our model with all required information, in particular the pre-processing steps and the regression model.

### Fit a model

With the pipeline from above, we are ready to fit it to the data. Typically, we use training data to fit the model. The training data is pre-processed according to our recipe steps, and the Lasso regression coefficients are computed. For illustrative purposes, we focus on the manufacturing industry for now.

```
= data.query("industry == 'manuf'")
data_manufacturing
= lm_pipeline.fit(
lm_fit
data_manufacturing, "ret_excess")
data_manufacturing.get( )
```

First, we focus on the predicted values \(\hat{y}_t = x_t\hat\beta^\text{Lasso}.\) Figure 2 illustrates the projections for the *entire* time series of the manufacturing industry portfolio returns.

```
= (pd.DataFrame({
predicted_values "Fitted value": lm_fit.predict(
data_manufacturing
),"Realization": data_manufacturing.get("ret_excess")
})= data_manufacturing["month"])
.assign(month ="month",
.melt(id_vars="Variable",
var_name="return")
value_name
)
= (
predicted_values_plot
ggplot(predicted_values, ="month", y="return",
aes(x="Variable", linetype="Variable")) +
color+
geom_line()
labs(="", y="", color="", linetype="",
x=("Monthly realized and fitted manufacturing "
title"industry risk premia")
+
)
scale_x_datetime(=date_breaks("5 years"),
breaks=date_breaks("1 year"),
minor_breaks=date_format("%Y"),
labels=(0, 0)
expand+
) =percent_format()))
scale_y_continuous(labels predicted_values_plot.draw()
```

What do the estimated coefficients look like? To analyze these values, it is worth computing the coefficients \(\hat\beta^\text{Lasso}\) directly. The code below estimates the coefficients for the Lasso and Ridge regression for the processed training data sample.

```
= preprocessor.fit_transform(data_manufacturing)
x = data_manufacturing["ret_excess"]
y
= np.logspace(-5, 5, 100)
alphas
= []
coefficients_lasso for a in alphas:
= Lasso(alpha=a, fit_intercept=False)
lasso
coefficients_lasso.append(lasso.fit(x, y).coef_)
= (pd.DataFrame(coefficients_lasso)
coefficients_lasso = alphas, model = "Lasso")
.assign(alpha =["alpha", "model"])
.melt(id_vars
)
= []
coefficients_ridge for a in alphas:
= Ridge(alpha=a, fit_intercept=False)
ridge
coefficients_ridge.append(ridge.fit(x, y).coef_)
= (pd.DataFrame(coefficients_ridge)
coefficients_ridge = alphas, model = "Ridge")
.assign(alpha =["alpha", "model"])
.melt(id_vars )
```

The data frames `lasso_coefficients`

and `ridge_coefficients`

contain an entire sequence of estimated coefficients for multiple values of the penalty factor \(\lambda\). Figure 3 illustrates the trajectories of the regression coefficients as a function of the penalty factor. Both Lasso and Ridge coefficients converge to zero as the penalty factor increases.

```
= (
coefficients_plot
ggplot(pd.concat([coefficients_lasso, coefficients_ridge]), = "alpha", y = "value", color = "variable")) +
aes(x +
geom_line() +
scale_x_log10() "model") +
facet_wrap(
labs(= "Penalty factor (lambda)", y = "",
x = ("Estimated coefficient paths for different "
title "penalty factors")
+
) ="none"))
theme(legend_position coefficients_plot.draw()
```

### Tune a model

To compute \(\hat\beta_\lambda^\text{Lasso}\) , we simply imposed a value for the penalty hyperparameter \(\lambda\). Model tuning is the process of optimally selecting such hyperparameters through *cross-validation*.

The goal for choosing \(\lambda\) (or any other hyperparameter, e.g., \(\rho\) for the Elastic Net) is to find a way to produce predictors \(\hat{Y}\) for an outcome \(Y\) that minimizes the mean squared prediction error \(\text{MSPE} = E\left( \frac{1}{T}\sum_{t=1}^T (\hat{y}_t - y_t)^2 \right)\). Unfortunately, the MSPE is not directly observable. We can only compute an estimate because our data is random and because we do not observe the entire population.

Obviously, if we train an algorithm on the same data that we use to compute the error, our estimate \(\hat{\text{MSPE}}\) would indicate way better predictive accuracy than what we can expect in real out-of-sample data. The result is called overfitting.

Cross-validation is a technique that allows us to alleviate this problem. We approximate the true MSPE as the average of many MSPE obtained by creating predictions for \(K\) new random samples of the data, none of them used to train the algorithm \(\frac{1}{K} \sum_{k=1}^K \frac{1}{T}\sum_{t=1}^T \left(\hat{y}_t^k - y_t^k\right)^2\). In practice, this is done by carving out a piece of our data and pretending it is an independent sample. We again divide the data into a training set and a test set. The MSPE on the test set is our measure for actual predictive ability, while we use the training set to fit models with the aim to find the *optimal* hyperparameter values. To do so, we further divide our training sample into (several) subsets, fit our model for a grid of potential hyperparameter values (e.g., \(\lambda\)), and evaluate the predictive accuracy on an *independent* sample. This works as follows:

- Specify a grid of hyperparameters
- Obtain predictors \(\hat{y}_i(\lambda)\) to denote the predictors for the used parameters \(\lambda\)
- Compute \[ \text{MSPE}(\lambda) = \frac{1}{K} \sum_{k=1}^K \frac{1}{T}\sum_{t=1}^T \left(\hat{y}_t^k(\lambda) - y_t^k\right)^2 \] With K-fold cross-validation, we do this computation \(K\) times. Simply pick a validation set with \(M=T/K\) observations at random and think of these as random samples \(y_1^k, \dots, y_{\tilde{T}}^k\), with \(k=1\)

How should you pick \(K\)? Large values of \(K\) are preferable because the training data better imitates the original data. However, larger values of \(K\) will have much higher computation time. `scikit-learn`

provides all required tools to conduct \(K\)-fold cross-validation. We just have to update our model specification. In our case, we specify the penalty factor \(\lambda\) as well as the mixing factor \(\rho\) as *free* parameters.

For our sample, we consider a time-series cross-validation sample. This means that we tune our models with 20 random samples of length five years with a validation period of four years. For a grid of possible hyperparameters, we then fit the model for each fold and evaluate \(\hat{\text{MSPE}}\) in the corresponding validation set. Finally, we select the model specification with the lowest MSPE in the validation set. First, we define the cross-validation folds based on our training data only.

Then, we evaluate the performance for a grid of different penalty values. `scikit-learn`

provides functionalities to construct a suitable grid of hyperparameters with `GridSearchCV()`

. The code chunk below creates a \(10 \times 3\) hyperparameters grid. Then, the method `fit()`

evaluates all the models for each fold.

```
= 5
initial_years = 48
assessment_months = len(data_manufacturing) // assessment_months
n_splits = 12
length_of_year = np.logspace(-6, 2, 100)
alphas
= TimeSeriesSplit(
data_folds =n_splits,
n_splits=assessment_months,
test_size=initial_years * length_of_year
max_train_size
)
= {
params "regressor__alpha": alphas,
"regressor__l1_ratio": (0.0, 0.5, 1)
}
= GridSearchCV(
finder
lm_pipeline,=params,
param_grid="neg_root_mean_squared_error",
scoring= data_folds
cv
)
= finder.fit(
finder
data_manufacturing,"ret_excess")
data_manufacturing.get( )
```

After the tuning process, we collect the evaluation metrics (the root mean-squared error in our example) to identify the *optimal* model. Figure 4 illustrates the average validation set’s root mean-squared error for each value of \(\lambda\) and \(\rho\).

```
= (pd.DataFrame(finder.cv_results_)
validation
.assign(= lambda x: -x["mean_test_score"],
mspe = lambda x: pd.to_numeric(
param_regressor__alpha "param_regressor__alpha"], errors="coerce"
x[
)
)
)
= (ggplot(validation,
validation_plot = "param_regressor__alpha",
aes(x = "mspe",
y = "param_regressor__l1_ratio",
color = "param_regressor__l1_ratio")) +
group +
geom_point() = "Root MSPE",
labs(y="Penalty factor (lambda)",
x = "Root MSPE for different penalty factors",
title = "Proportion of Lasso Penalty") +
color +
geom_line() = "none") +
guides(linetype
scale_x_log10()
) validation_plot.draw()
```

Figure 4 shows that the MSPE drops faster for Lasso and Elastic net compared to Ridge regressions as penality factor increases. However, for higher penalty factors, the MSPE for Ridge regressions dips below the others, which both slightly increase again above a certain threshold. Recall that the larger the regularization, the more restricted the model becomes.

### Full workflow

Our starting point was the question: Which factors determine industry returns? While Avramov et al. (2022) provide a Bayesian analysis related to the research question above, we choose a simplified approach: To illustrate the entire workflow, we now run the penalized regressions for all ten industries. We want to identify relevant variables by fitting Lasso models for each industry returns time series. More specifically, we perform cross-validation for each industry to identify the optimal penalty factor \(\lambda\).

First, we define the Lasso model with one tuning parameter.

```
= Lasso(fit_intercept=False, max_iter=5000)
lm_model
= {
params "regressor__alpha": alphas
}
= Pipeline([
lm_pipeline "preprocessor", preprocessor),
("regressor", lm_model)
( ])
```

The following task can be easily parallelized to reduce computing time, but we use a simple loop for ease of exposition.

```
= data["industry"].drop_duplicates()
all_industries
= []
results for industry in all_industries:
print(industry)
= GridSearchCV(
finder
lm_pipeline,=params,
param_grid="neg_mean_squared_error",
scoring= data_folds
cv
)
= finder.fit(
finder "industry==@industry"),
data.query("industry==@industry").get("ret_excess")
data.query(
)
results.append(
pd.DataFrame(!= 0
finder.best_estimator_.named_steps.regressor.coef_
)
)
= (
selected_factors
pd.DataFrame(-1].get_feature_names_out(),
lm_pipeline[:= ["variable"]
columns
)= lambda x: (
.assign(variable "variable"].str.replace("factor_|ff_|q_|macro_",""))
x[
)**dict(zip(all_industries, results)))
.assign(="variable", var_name ="industry")
.melt(id_vars"value==True")
.query( )
```

What has just happened? In principle, exactly the same as before but instead of computing the Lasso coefficients for one industry, we did it for ten sequentially. Now, we just have to do some housekeeping and keep only variables that Lasso does *not* set to zero. We illustrate the results in a heat map in Figure 5.

```
= (
selected_factors_plot
ggplot(selected_factors, ="variable", y="industry")) +
aes(x+
geom_tile() =element_text(rotation=70, hjust=1)) +
theme(axis_text_x="", y="",
labs(x="Selected variables for different industries") +
title+
coord_flip() =reversed)
scale_x_discrete(limits
) selected_factors_plot.draw()
```

The heat map in Figure 5 conveys two main insights: first, we see that many factors, macroeconomic variables, and interaction terms are not relevant for explaining the cross-section of returns across the industry portfolios. In fact, only `factor_ff_mkt_excess`

and its interaction with `macro_bm`

a role for several industries. Second, there seems to be quite some heterogeneity across different industries. While barely any variable is selected by Lasso for Utilities, many factors are selected for, e.g., Durable and Manufacturing, but the selected factors do not necessarily coincide. In other words, there seems to be a clear picture that we do not need many factors, but Lasso does not provide a factor that consistently provides pricing abilities across industries.

## Exercises

- Write a function that requires three inputs, namely,
`y`

(a \(T\) vector),`X`

(a \((T \times K)\) matrix), and`lambda`

and then returns the Ridge estimator (a \(K\) vector) for a given penalization parameter \(\lambda\). Recall that the intercept should not be penalized. Therefore, your function should indicate whether \(X\) contains a vector of ones as the first column, which should be exempt from the \(L_2\) penalty. - Compute the \(L_2\) norm (\(\beta'\beta\)) for the regression coefficients based on the predictive regression from the previous exercise for a range of \(\lambda\)’s and illustrate the effect of penalization in a suitable figure.
- Now, write a function that requires three inputs, namely,
`y`

(a \(T\) vector),`X`

(a \((T \times K)\) matrix), and ’lambda` and then returns the Lasso estimator (a \(K\) vector) for a given penalization parameter \(\lambda\). Recall that the intercept should not be penalized. Therefore, your function should indicate whether \(X\) contains a vector of ones as the first column, which should be exempt from the \(L_1\) penalty. - After you understand what Ridge and Lasso regressions are doing, familiarize yourself with the
`glmnet()`

package’s documentation. It is a thoroughly tested and well-established package that provides efficient code to compute the penalized regression coefficients for Ridge and Lasso and for combinations, commonly called*Elastic Nets*.

## References

*The Journal of Finance (Forthcoming)*. http://dx.doi.org/10.2139/ssrn.3924337.

*Asset pricing (revised edition)*. Princeton University Press.

*The Journal of Finance*66 (4): 1047–1108. https://doi.org/10.1111/j.1540-6261.2011.01671.x.

*Advances in financial machine learning*. John Wiley & Sons. https://doi.org/10.5555/3217448.

*Machine learning in finance*. Springer.

*Review of Financial Studies*34 (7): 3316–63. https://doi.org/10.1093/rfs/hhaa078.

*The Journal of Finance*47 (2): 427–65. https://doi.org/2329112.

*Journal of Financial Economics*33 (1): 3–56. https://doi.org/10.1016/0304-405X(93)90023-5.

*An introduction to statistical learning: With applications in R*. Spinger.

*Review of Financial Studies*29 (1): 5–68. https://doi.org/10.1093/rfs/hhv059.

*The elements of statistical learning: Data mining, inference and prediction*. 2nd ed. Springer. https://hastie.su.domains/ElemStatLearn/.

*Technometrics*12 (1): 69–82. https://doi.org/1267352.

*Review of Financial Studies*28 (3): 650–705. https://doi.org/10.1093/rfs/hhu068.

*Review of Financial Studies*33 (5): 2019–2133. https://doi.org/10.1093/rfs/hhy131.

*The Journal of Finance*71 (1): 5–32. https://doi.org/10.1111/jofi.12365.

*Journal of Economic Perspectives*31 (2): 87–106. https://doi.org/10.1257/jep.31.2.87.

*Machine learning in asset pricing*. Princeton University Press.

*Journal of Machine Learning Research*12: 2825–30.

*Journal of the Royal Statistical Society. Series B (Methodological)*58 (1): 267–88. http://www.jstor.org/stable/2346178.

*Review of Financial Studies*21 (4): 1455–1508. https://doi.org/10.1093/rfs/hhm014.

*Journal of the Royal Statistical Society. Series B (Statistical Methodology)*67 (2): 301–20. https://www.jstor.org/stable/3647580.