library(tidyverse)
library(nanoparquet)
library(tidymodels)
sample_start <- "1973-01-01"
sample_end <- "2023-12-31"
test_start <- "2000-01-01"
months_per_year <- 12
osap_url <- paste0(
"https://huggingface.co/datasets/OpenSourceAP/CrossSection/resolve/",
"Data-Release-2026.10/Portfolios/Full%20Sets%20OP/PredictorLSretWide.parquet"
)
download.file(osap_url, "data/osap_long_short_returns.parquet", mode = "wb")
anomalies <- read_parquet("data/osap_long_short_returns.parquet") |>
filter(date >= sample_start, date <= sample_end) |>
select(date, where(\(x) !anyNA(x))) |>
mutate(across(-date, \(x) x / 100), y = 1)
anomaly_names <- setdiff(names(anomalies), c("date", "y"))
sample_split <- make_splits(
filter(anomalies, date < test_start),
assessment = filter(anomalies, date >= test_start)
)
estimation_months <- training(sample_split)
test_months <- testing(sample_split)
length(anomaly_names)
x_estimation <- as.matrix(estimation_months[anomaly_names])
x_test <- as.matrix(test_months[anomaly_names])
y <- estimation_months$y
sharpe_ratio_vec <- function(truth, estimate, ...) {
mean(estimate) / sd(estimate) * sqrt(months_per_year)
}
portfolio_sharpe <- function(weights) {
c(
estimation = sharpe_ratio_vec(estimate = drop(x_estimation %*% weights)),
test = sharpe_ratio_vec(estimate = drop(x_test %*% weights))
)
}
portfolio_sharpe(rep(1, length(anomaly_names)))Exercise 7: Shrinking the SDF
These exercises go with lecture 10 and lecture 11 and reproduce their results: ridge regression and the lasso, and how to choose their penalty. You first implement the two estimators yourself. Then you estimate them with the machine learning workflow of the lectures, tidymodels in R and scikit-learn in Python, check them against your own functions, and let the workflow choose the penalty by cross-validation.
The application is the running example of the machine learning lectures: the factor zoo. Hundreds of published anomalies each come with a long-short portfolio. Which combination of them prices the cross-section of stock returns, and how much of that combination survives out of sample? The lecture plan shows the week of this exercise.
Ridge and lasso on the factor zoo
Exercises:
- Download the monthly long-short returns of the anomaly portfolios from Open Source Asset Pricing (Chen and Zimmermann) and store them in the
datafolder. Keep the anomalies with a return in every month from January 1973 to December 2023. The years until 1999 are your estimation sample, the years from 2000 on your test sample. How many anomalies are left? Compute the annualized Sharpe ratio of the portfolio that holds all anomalies with equal weights (1/N), in both samples. - The stochastic discount factor (SDF) \(M_{t+1} = 1 - \sum_{i=1}^{N} b_i F_{i,t+1}\), where \(F_{i,t+1}\) is the return of anomaly \(i\)’s long-short portfolio in month \(t+1\), prices all \(N\) anomalies if the weights \(b_i\) are proportional to \(\Sigma^{-1}\mu\), the weights of the mean-variance efficient portfolio of the anomalies (lecture 11). Britten-Jones (1999) shows that you get these weights from a regression: regress the number one on the vector \(F_t\) of the anomaly returns in month \(t\), without an intercept, \(1 = b'F_t + u_t\). Run this regression in the estimation sample and compute the Sharpe ratio of the resulting portfolio in both samples. What do you find?
- Write a function that requires three inputs,
y(a vector of \(T\) ones),x(the \(T \times N\) matrix \(F\) of the anomaly returns), andlambda, and which returns the ridge estimator \(\hat b = (F'F + T\lambda I)^{-1}F'y\). This scaling of the penalty is the one of the packages, so you can compare your results with them later. Compute the \(L_2\) norm (\(b'b\)) of the estimated weights and the Sharpe ratios in both samples for the penalties \(\lambda = 10^{-7}, 10^{-6.75}, \dots, 10^{-1}\) of lecture 10, and illustrate the effect of the penalization in suitable figures. Which \(\lambda\) would you choose, and why may you not use the test sample to choose it? - Estimate the same ridge regression with the workflow of lecture 10:
linear_reg(mixture = 0)with the engineglmnetin R,ElasticNet(l1_ratio=0)in Python, both without an intercept, and in R without glmnet’s standardization of the returns. Check that the workflow gives the same weights as your function. - Choose \(\lambda\) by cross-validation within the estimation sample, as in lecture 11: split it into five consecutive blocks of months, estimate the weights on four blocks and compute the Sharpe ratio on the fifth, so that each block is the validation block once, and average the five Sharpe ratios. Let the workflow do this: the Sharpe ratio becomes a metric (a yardstick metric in R, a scorer from
make_scorer()in Python), the blocks become folds (group_vfold_cv(),GroupKFold()), andtune_grid()orGridSearchCV()loops over the penalties and folds. Which \(\lambda\) gets the highest average, and what is the Sharpe ratio of this ridge portfolio in the test sample? - Now, write a function with the same three inputs which returns the lasso estimator, the \(b\) that minimizes \(\frac{1}{2T}(y - Fb)'(y - Fb) + \lambda \sum_{i=1}^{N} |b_i|\), the objective of lecture 10 divided by \(2T\). Use coordinate descent: go through the weights one by one and update each, keeping the others fixed, until nothing changes anymore. Check your function against the lasso of the workflow (
mixture = 1in R,l1_ratio=1in Python). - Compute the lasso with the workflow for the penalties \(\lambda = 10^{-6}, 10^{-5.75}, \dots, 10^{-2.5}\) of lecture 10. How many anomalies does the lasso select for each \(\lambda\), and how does the Sharpe ratio in the test sample change? Choose \(\lambda\) by cross-validation as for ridge. How many anomalies does a good SDF need? Kozak, Nagel, and Santosh (2020) discuss whether the SDF is sparse.
Solutions:
We read the long-short returns directly from the October 2026 data release of Open Source Asset Pricing; once tidyfinance supports this source, download_data() will do the same. The returns are in percent. select(where(...)) (R) and the list of complete columns (Python) keep the anomalies without missing months. y, a one in every month, is the dependent variable of all regressions on this page. As in lecture 10, make_splits() (R) and .filter() (Python) split the months by date into the estimation and the test sample, and the matrices x_estimation and x_test hold the anomaly returns for our own functions.
sharpe_ratio_vec() (R) and sharpe_ratio() (Python) return the annualized Sharpe ratio of a vector of portfolio returns. Cross-validation below uses them as a metric, and a metric of the workflow always takes two arguments: the observed outcome (truth in R, y in Python) and the prediction (estimate, y_pred). The predictions of our regressions are the portfolio returns, so the Sharpe ratio needs only the second argument and ignores the first.
The output blocks after the R code show what R prints. The Python code prints the same numbers in its own format; only the tiny differences between our functions and the workflow differ between the languages, and the text gives them for both.
#> [1] 158
#> estimation test
#> 2.792511 1.434807from datetime import date
import numpy as np
import polars as pl
from plotnine import *
from sklearn.base import clone
from sklearn.linear_model import ElasticNet
from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV, GroupKFold
sample_start = date(1973, 1, 1)
sample_end = date(2023, 12, 31)
test_start = date(2000, 1, 1)
months_per_year = 12
osap_url = (
"https://huggingface.co/datasets/OpenSourceAP/CrossSection/resolve/"
"Data-Release-2026.10/Portfolios/Full%20Sets%20OP/PredictorLSretWide.parquet"
)
pl.read_parquet(osap_url).write_parquet("data/osap_long_short_returns.parquet")
anomalies = pl.read_parquet("data/osap_long_short_returns.parquet").filter(
pl.col("date").is_between(sample_start, sample_end)
)
anomaly_names = [
c for c in anomalies.columns if c != "date" and anomalies[c].null_count() == 0
]
anomalies = anomalies.select("date", pl.col(anomaly_names) / 100, y=pl.lit(1.0))
estimation_months = anomalies.filter(pl.col("date") < test_start)
test_months = anomalies.filter(pl.col("date") >= test_start)
print(len(anomaly_names))
x_estimation = estimation_months.select(anomaly_names).to_numpy()
x_test = test_months.select(anomaly_names).to_numpy()
y_estimation = estimation_months["y"].to_numpy()
y_test = test_months["y"].to_numpy()
def sharpe_ratio(y, y_pred):
return y_pred.mean() / y_pred.std(ddof=1) * np.sqrt(months_per_year)
def portfolio_sharpe(weights):
return {
"estimation": sharpe_ratio(y_estimation, x_estimation @ weights),
"test": sharpe_ratio(y_test, x_test @ weights),
}
portfolio_sharpe(np.ones(len(anomaly_names)))158 anomalies have complete returns. The equal-weighted portfolio earns a Sharpe ratio of 2.79 until 1999, but only 1.43 afterwards: many anomalies weaken once they are published and traded on.
The Britten-Jones regression has no intercept and a vector of ones as the dependent variable. Its coefficients are the weights of the mean-variance efficient portfolio, up to a scaling factor that does not affect the Sharpe ratio.
To see why, stack the anomaly returns of the \(T\) estimation months in the \(T \times N\) matrix \(F\), whose row \(t\) is \(F_t'\), and let \(\mathbf 1\) be a vector of \(T\) ones. The sample mean of the returns is \(\hat\mu = F'\mathbf 1 / T\), and their sample covariance matrix with divisor \(T\) is \(\hat\Sigma = F'F / T - \hat\mu\hat\mu'\). So \(F'F = T(\hat\Sigma + \hat\mu\hat\mu')\) and \(F'\mathbf 1 = T\hat\mu\), and OLS without an intercept gives
\[ \hat b = (F'F)^{-1}F'\mathbf 1 = (\hat\Sigma + \hat\mu\hat\mu')^{-1}\hat\mu. \]
The Sherman–Morrison formula inverts a matrix plus an outer product: \((A + uu')^{-1} = A^{-1} - A^{-1}uu'A^{-1} / (1 + u'A^{-1}u)\). With \(A = \hat\Sigma\) and \(u = \hat\mu\), and the number \(q = \hat\mu'\hat\Sigma^{-1}\hat\mu\),
\[ \hat b = \hat\Sigma^{-1}\hat\mu - \frac{\hat\Sigma^{-1}\hat\mu\, q}{1 + q} = \frac{\hat\Sigma^{-1}\hat\mu}{1 + q}. \]
\(\hat\Sigma^{-1}\hat\mu\) are the weights of the tangency portfolio, and \(q\), the squared monthly Sharpe ratio of the tangency portfolio, is positive. So \(\hat b\) is the tangency portfolio scaled down by the factor \(1 + q\), with the same Sharpe ratio. The derivation needs \(\hat\Sigma\) to be invertible, which requires more months than anomalies. In our estimation sample, \(q = 20.66\): the two weight vectors have a correlation of 1, and \(\hat b\) times 21.66 equals \(\hat\Sigma^{-1}\hat\mu\) up to rounding errors of the computer.
b_ols <- solve(crossprod(x_estimation), crossprod(x_estimation, y))
portfolio_sharpe(b_ols)#> estimation test
#> 15.721492 2.511463b_ols = np.linalg.solve(x_estimation.T @ x_estimation, x_estimation.T @ y_estimation)
portfolio_sharpe(b_ols)In the estimation sample, the plug-in portfolio reaches a Sharpe ratio of about 15.7, an impossible number. In the test sample, it falls to 2.51: with 158 weights estimated from 324 months, the portfolio mostly fits noise.
The ridge estimator adds \(T\lambda\) to the diagonal of \(F'F\). We evaluate it on the grid of lecture 10, from almost no penalty to strong shrinkage.
ridge <- function(y, x, lambda) {
solve(crossprod(x) + nrow(x) * lambda * diag(ncol(x)), crossprod(x, y))
}
ridge_lambdas <- 10^seq(-7, -1, by = 0.25)
ridge_path <- tibble(lambda = ridge_lambdas) |>
mutate(
weights = map(lambda, \(lambda) ridge(y, x_estimation, lambda)),
l2_norm = map_dbl(weights, \(b) sum(b^2)),
sharpe = map(weights, portfolio_sharpe)
) |>
unnest_wider(sharpe)
ridge_path |>
ggplot(aes(x = lambda, y = l2_norm)) +
geom_line() +
scale_x_log10() +
scale_y_log10() +
labs(x = "Penalty λ", y = "L2 norm of the weights")
ridge_path |>
pivot_longer(
c(estimation, test),
names_to = "sample",
values_to = "sharpe_ratio"
) |>
ggplot(aes(x = lambda, y = sharpe_ratio, color = sample)) +
geom_line() +
scale_x_log10() +
labs(x = "Penalty λ", y = "Sharpe ratio (annualized)", color = NULL)def ridge(y, x, lambda_):
return np.linalg.solve(x.T @ x + x.shape[0] * lambda_ * np.eye(x.shape[1]), x.T @ y)
ridge_lambdas = 10 ** np.arange(-7, -0.99, 0.25)
ridge_path = pl.DataFrame(
[
{"lambda": lambda_, "l2_norm": float(np.sum(b**2)), **portfolio_sharpe(b)}
for lambda_ in ridge_lambdas
for b in [ridge(y_estimation, x_estimation, lambda_)]
]
)
(
ggplot(ridge_path, aes(x="lambda", y="l2_norm"))
+ geom_line()
+ scale_x_log10()
+ scale_y_log10()
+ labs(x="Penalty λ", y="L2 norm of the weights")
)(
ggplot(
ridge_path.unpivot(
index="lambda",
on=["estimation", "test"],
variable_name="sample",
value_name="sharpe_ratio",
),
aes(x="lambda", y="sharpe_ratio", color="sample"),
)
+ geom_line()
+ scale_x_log10()
+ labs(x="Penalty λ", y="Sharpe ratio (annualized)", color="")
)

More shrinkage lowers the Sharpe ratio in the estimation sample, from 15.7 to 3.9. In the test sample, more shrinkage helps up to a point: the Sharpe ratio rises from 2.51 to 2.92 at \(\lambda = 10^{-3.5}\) and falls to 1.47 at \(\lambda = 0.1\). Picking the \(\lambda\) with the best test Sharpe ratio would use the test sample to build the portfolio, so its Sharpe ratio would no longer be out of sample. Instead, we choose \(\lambda\) by cross-validation within the estimation sample, with the workflow of lectures 10 and 11.
The workflow first specifies the model. In R, workflow() bundles the variables with the model: the regression of y on all anomalies without an intercept (0 +) of lecture 10, with the ridge penalty of glmnet (mixture = 0). penalty = tune() leaves \(\lambda\) open, and path_values lets glmnet fit all penalties of the grid in one run. In Python, ElasticNet() with l1_ratio=0 is the ridge regression; its penalty alpha is our \(\lambda\). Both packages minimize the ridge objective divided by \(2T\), \(\frac{1}{2T}(y - Fb)'(y - Fb) + \frac{\lambda}{2} b'b\), whose solution is our closed form. Both find the weights iteratively, so a tight convergence threshold makes them agree with our function.
plug_in_workflow <- workflow() |>
add_variables(outcomes = y, predictors = all_of(anomaly_names)) |>
add_model(linear_reg(), formula = y ~ 0 + .)
ridge_model <- linear_reg(penalty = tune(), mixture = 0) |>
set_engine(
"glmnet",
intercept = FALSE,
standardize = FALSE,
path_values = ridge_lambdas,
control = list(thresh = 1e-12)
)
ridge_workflow <- update_model(plug_in_workflow, ridge_model)
check_lambda <- 1e-4
b_workflow <- ridge_workflow |>
finalize_workflow(tibble(penalty = check_lambda)) |>
fit(estimation_months) |>
tidy() |>
filter(term != "(Intercept)") |>
pull(estimate)
b_ridge <- ridge(y, x_estimation, check_lambda)
max(abs(b_workflow - b_ridge)) / max(abs(b_ridge))#> [1] 9.449511e-05ridge_model = ElasticNet(l1_ratio=0, fit_intercept=False, tol=1e-8, max_iter=1_000_000)
check_lambda = 1e-4
b_workflow = (
clone(ridge_model)
.set_params(alpha=check_lambda)
.fit(x_estimation, y_estimation)
.coef_
)
b_ridge = ridge(y_estimation, x_estimation, check_lambda)
np.max(np.abs(b_workflow - b_ridge)) / np.max(np.abs(b_ridge))The weights of the workflow differ from those of our function by less than 0.01% of the largest weight in R and by less than 0.0001% in Python. What remains comes from the convergence thresholds of the iterative solvers: the objective is very flat in some directions, so a tiny difference in the objective allows a small difference in the weights. For the portfolio, this does not matter: the Sharpe ratios of the two portfolios agree to four decimals.
For cross-validation, the Sharpe ratio becomes a metric of the workflow, as in lecture 10: the fitted values \(\hat b'F_t\) are the returns of the portfolio \(\hat b\), so the Sharpe ratio of the predictions is the Sharpe ratio of the portfolio. The folds are five consecutive blocks of months, so each fold keeps the time series together. tune_grid() and GridSearchCV() loop over penalties and folds: they fit the weights on the other four blocks, compute the Sharpe ratio in the validation block, and average over the folds. GridSearchCV() then refits the best penalty on the whole estimation sample by itself; in R, finalize_workflow() and fit() do this step, and sharpe_in() computes the Sharpe ratio of the fitted workflow in the estimation or the test months.
sharpe_ratio <- function(data, truth, estimate, ...) {
numeric_metric_summarizer(
"sharpe_ratio",
sharpe_ratio_vec,
data,
!!enquo(truth),
!!enquo(estimate)
)
}
sharpe_ratio <- new_numeric_metric(sharpe_ratio, direction = "maximize")
n_folds <- 5
block_folds <- estimation_months |>
mutate(block = ceiling(dense_rank(date) / (n_distinct(date) / n_folds))) |>
group_vfold_cv(group = block, v = n_folds)
ridge_tuning <- tune_grid(
ridge_workflow,
block_folds,
grid = tibble(penalty = ridge_lambdas),
metrics = metric_set(sharpe_ratio)
)
show_best(ridge_tuning, metric = "sharpe_ratio", n = 3)
sharpe_in <- function(fit, months) {
augment(fit, months) |> sharpe_ratio(y, .pred) |> pull(.estimate)
}
ridge_fit <- ridge_workflow |>
finalize_workflow(select_best(ridge_tuning, metric = "sharpe_ratio")) |>
fit(estimation_months)
tibble(
estimation = sharpe_in(ridge_fit, estimation_months),
test = sharpe_in(ridge_fit, test_months)
)#> # A tibble: 3 × 7
#> penalty .metric .estimator mean n std_err .config
#> <dbl> <chr> <chr> <dbl> <int> <dbl> <chr>
#> 1 0.0001 sharpe_ratio standard 8.48 5 0.429 pre0_mod13_post0
#> 2 0.000178 sharpe_ratio standard 8.40 5 0.451 pre0_mod14_post0
#> 3 0.0000562 sharpe_ratio standard 8.39 5 0.417 pre0_mod12_post0
#> # A tibble: 1 × 2
#> estimation test
#> <dbl> <dbl>
#> 1 12.9 2.89sharpe_scorer = make_scorer(sharpe_ratio)
n_folds = 5
months = estimation_months["date"].rank("dense")
blocks = np.ceil(months / (months.max() / n_folds)).to_numpy()
ridge_search = GridSearchCV(
ridge_model,
{"alpha": ridge_lambdas},
scoring=sharpe_scorer,
cv=GroupKFold(n_splits=n_folds),
)
ridge_search.fit(x_estimation, y_estimation, groups=blocks)
{
"lambda": ridge_search.best_params_["alpha"],
"cv": ridge_search.best_score_,
"estimation": sharpe_scorer(ridge_search, x_estimation, y_estimation),
"test": sharpe_scorer(ridge_search, x_test, y_test),
}Cross-validation picks \(\lambda = 10^{-4}\) in both languages, with an average Sharpe ratio of 8.48 in the five validation blocks. The ridge portfolio reaches a Sharpe ratio of 2.89 in the test sample, clearly above the 2.51 of the plug-in portfolio, while its Sharpe ratio in the estimation sample drops from 15.7 to 12.9. These are the numbers of lecture 11. The validation blocks lie in the years until 1999, when the anomalies earned much more than after 2000, so their average Sharpe ratio says little about the test sample; it only ranks the penalties.
Coordinate descent updates one weight at a time. Given the other weights, the optimal \(b_i\) has a closed form: compute the correlation of \(F_i\), the returns of anomaly \(i\), with the partial residual (the residual without \(F_i\)’s contribution), shrink it towards zero by \(\lambda\) (soft thresholding), and divide by \(F_i'F_i / T\). Weights whose correlation is smaller than \(\lambda\) become exactly zero; this is how the lasso selects. The workflow’s lasso is the ridge workflow with mixture = 1 (R) or l1_ratio=1 (Python) and the grid of penalties for the lasso.
soft_threshold <- function(z, gamma) sign(z) * pmax(abs(z) - gamma, 0)
lasso <- function(y, x, lambda, tolerance = 1e-10, max_iterations = 10000) {
n <- nrow(x)
b <- rep(0, ncol(x))
x_squared <- colSums(x^2) / n
residual <- y
for (iteration in seq_len(max_iterations)) {
b_old <- b
for (i in seq_along(b)) {
residual <- residual + x[, i] * b[i]
b[i] <- soft_threshold(sum(x[, i] * residual) / n, lambda) / x_squared[i]
residual <- residual - x[, i] * b[i]
}
if (max(abs(b - b_old)) < tolerance) break
}
b
}
lasso_lambdas <- 10^seq(-6, -2.5, by = 0.25)
lasso_model <- linear_reg(penalty = tune(), mixture = 1) |>
set_engine(
"glmnet",
intercept = FALSE,
standardize = FALSE,
path_values = lasso_lambdas,
control = list(thresh = 1e-12)
)
lasso_workflow <- update_model(plug_in_workflow, lasso_model)
check_lambda <- 1e-3
b_workflow <- lasso_workflow |>
finalize_workflow(tibble(penalty = check_lambda)) |>
fit(estimation_months) |>
tidy() |>
filter(term != "(Intercept)") |>
pull(estimate)
b_lasso <- lasso(y, x_estimation, check_lambda)
tibble(
selected = sum(b_lasso != 0),
max_relative_difference = max(abs(b_lasso - b_workflow)) / max(abs(b_lasso))
)#> # A tibble: 1 × 2
#> selected max_relative_difference
#> <int> <dbl>
#> 1 31 0.0000140def soft_threshold(z, gamma):
return np.sign(z) * np.maximum(np.abs(z) - gamma, 0)
def lasso(y, x, lambda_, tolerance=1e-10, max_iterations=10000):
n = x.shape[0]
b = np.zeros(x.shape[1])
x_squared = (x**2).sum(axis=0) / n
residual = y.copy()
for _ in range(max_iterations):
b_old = b.copy()
for i in range(len(b)):
residual += x[:, i] * b[i]
b[i] = soft_threshold(x[:, i] @ residual / n, lambda_) / x_squared[i]
residual -= x[:, i] * b[i]
if np.max(np.abs(b - b_old)) < tolerance:
break
return b
lasso_lambdas = 10 ** np.arange(-6, -2.49, 0.25)
lasso_model = ElasticNet(l1_ratio=1, fit_intercept=False, tol=1e-8, max_iter=1_000_000)
check_lambda = 1e-3
b_workflow = (
clone(lasso_model)
.set_params(alpha=check_lambda)
.fit(x_estimation, y_estimation)
.coef_
)
b_lasso = lasso(y_estimation, x_estimation, check_lambda)
{
"selected": int((b_lasso != 0).sum()),
"max_relative_difference": float(
np.max(np.abs(b_lasso - b_workflow)) / np.max(np.abs(b_lasso))
),
}With \(\lambda = 10^{-3}\), the lasso keeps 31 of the 158 anomalies. Our coordinate descent and the workflow agree up to 0.002% of the largest weight in R and 0.00001% in Python: both packages minimize exactly this objective when the intercept, and in R glmnet’s standardization, are switched off.
For the path, each penalty of the grid gets its own fit on the estimation sample, as in lecture 10; cross-validation then works exactly as for ridge.
lasso_path <- tibble(lambda = lasso_lambdas) |>
mutate(
fit = map(lambda, \(lambda) {
lasso_workflow |>
finalize_workflow(tibble(penalty = lambda)) |>
fit(estimation_months)
}),
selected = map_int(fit, \(fit) sum(tidy(fit)$estimate != 0)),
estimation = map_dbl(fit, \(fit) sharpe_in(fit, estimation_months)),
test = map_dbl(fit, \(fit) sharpe_in(fit, test_months))
)
lasso_path |>
ggplot(aes(x = selected, y = test)) +
geom_line() +
geom_point() +
labs(
x = "Number of selected anomalies",
y = "Sharpe ratio in the test sample"
)
lasso_tuning <- tune_grid(
lasso_workflow,
block_folds,
grid = tibble(penalty = lasso_lambdas),
metrics = metric_set(sharpe_ratio)
)
best_lasso <- select_best(lasso_tuning, metric = "sharpe_ratio")
lasso_path |>
filter(lambda == best_lasso$penalty) |>
select(lambda, selected, estimation, test)#> # A tibble: 1 × 4
#> lambda selected estimation test
#> <dbl> <int> <dbl> <dbl>
#> 1 0.000316 74 11.5 2.96lasso_path = []
for lambda_ in lasso_lambdas:
lasso_fit = clone(lasso_model).set_params(alpha=lambda_)
lasso_fit.fit(x_estimation, y_estimation)
lasso_path.append(
{
"lambda": lambda_,
"selected": int(np.sum(lasso_fit.coef_ != 0)),
"estimation": sharpe_scorer(lasso_fit, x_estimation, y_estimation),
"test": sharpe_scorer(lasso_fit, x_test, y_test),
}
)
lasso_path = pl.DataFrame(lasso_path)
(
ggplot(lasso_path, aes(x="selected", y="test"))
+ geom_line()
+ geom_point()
+ labs(x="Number of selected anomalies", y="Sharpe ratio in the test sample")
)lasso_search = GridSearchCV(
lasso_model,
{"alpha": lasso_lambdas},
scoring=sharpe_scorer,
cv=GroupKFold(n_splits=n_folds),
)
lasso_search.fit(x_estimation, y_estimation, groups=blocks)
{
"lambda": lasso_search.best_params_["alpha"],
"selected": int(np.sum(lasso_search.best_estimator_.coef_ != 0)),
"estimation": sharpe_scorer(lasso_search, x_estimation, y_estimation),
"test": sharpe_scorer(lasso_search, x_test, y_test),
}
Cross-validation picks \(\lambda = 3.2 \times 10^{-4}\) in both languages, which keeps 74 of the 158 anomalies, with a Sharpe ratio of 2.96 in the test sample, about as good as ridge (2.89). With fewer anomalies, the Sharpe ratio in the test sample falls: 2.79 with 31 anomalies and 2.15 with 18. A good SDF of the zoo needs dozens of anomalies, so it is not sparse in the anomalies. Kozak, Nagel, and Santosh (2020) ask whether it is sparser in the principal components of the anomaly returns; lecture 11 tries this on the zoo.
Your own validation scheme
The solution above chooses the ridge penalty with one particular cross-validation. How much does the Sharpe ratio in the test sample depend on that choice? Choose your own validation scheme within the estimation sample: the number of folds, random or consecutive blocks of months, and the criterion, for instance the Sharpe ratio or the pricing error. Never touch the test sample.
Submit by Sunday 11 April: your scheme (number of folds, how you split the months, the criterion), the chosen penalty, and the Sharpe ratio in the test sample: submission form.