L1 Trend Filtering

Introduction

Kim et al. (2009) propose the \(l_1\) trend filtering method for trend estimation. The method solves an optimization problem of the form

\[ \begin{array}{ll} \underset{\beta}{\mbox{minimize}} & \frac{1}{2}\sum_{i=1}^m (y_i - \beta_i)^2 + \lambda ||D\beta||_1 \end{array} \] where the variable to be estimated is \(\beta\) and we are given the problem data \(y\) and \(\lambda\). The matrix \(D\) is the second-order difference matrix,

\[ D = \left[ \begin{matrix} 1 & -2 & 1 & & & & \\ & 1 & -2 & 1 & & & \\ & & \ddots & \ddots & \ddots & & \\ & & & 1 & -2 & 1 & \\ & & & & 1 & -2 & 1\\ \end{matrix} \right]. \]

The implementation is in both C and Matlab. Hadley Wickham provides an R interface to the C code. This is on GitHub and can be installed via:

library(devtools)
install_github("hadley/l1tf")

Example

We will use the example in l1tf to illustrate. The package provides the function l1tf which computes the trend estimate for a specified \(\lambda\).

sp_data <- data.frame(x = sp500$date,
                      y = sp500$log,
                      l1_50 = l1tf(sp500$log, lambda = 50),
                      l1_100 = l1tf(sp500$log, lambda = 100))

The CVXR version

CVXR provides all the atoms and functions necessary to formulat the problem in a few lines. For example, the \(D\) matrix above is provided by the function diff(..., differences = 2). Notice how the formulation tracks the mathematical construct above.

## lambda = 50
y <- sp500$log
lambda_1 <- 50 
beta <- Variable(length(y))
objective_1 <- Minimize(0.5 * p_norm(y - beta) +
                        lambda_1 * p_norm(diff(x = beta, differences = 2), 1))
p1 <- Problem(objective_1)
betaHat_50 <- solve(p1)$getValue(beta)

## lambda = 100
lambda_2 <- 100
objective_2 <- Minimize(0.5 * p_norm(y - beta) +
                        lambda_2 * p_norm(diff(x = beta, differences = 2), 1))
p2 <- Problem(objective_2)
betaHat_100 <- solve(p2)$getValue(beta)

NOTE Of course, CVXR is much slower since it is not optimized just for one problem.

Comparison Plots

A plot of the estimates for two values of \(\lambda\) is shown below using both approaches. First the l1tf plot.

ggplot(data = sp_data) +
    geom_line(mapping = aes(x = x, y = y), color = 'grey50') +
    labs(x = "Date", y = "SP500 log-price") +
    geom_line(mapping = aes(x = x, y = l1_50), color = 'red', size = 1) +
    geom_line(mapping = aes(x = x, y = l1_100), color = 'blue', size = 1)
$L_1$ trends for $\lambda = 50$ (red) and $\lambda = 100$ (blue).

Figure 1: \(L_1\) trends for \(\lambda = 50\) (red) and \(\lambda = 100\) (blue).

Next the corresponding CVXR plots.

cvxr_data <- data.frame(x = sp500$date,
                        y = sp500$log,
                        l1_50 = betaHat_50,
                        l1_100 = betaHat_100)
ggplot(data = cvxr_data) +
    geom_line(mapping = aes(x = x, y = y), color = 'grey50') +
    labs(x = "Date", y = "SP500 log-price") +
    geom_line(mapping = aes(x = x, y = l1_50), color = 'red', size = 1) +
    geom_line(mapping = aes(x = x, y = l1_100), color = 'blue', size = 1)
`CVXR` estimated $L_1$ trends for $\lambda = 50$ (red) and $\lambda = 100$ (blue).

Figure 2: CVXR estimated \(L_1\) trends for \(\lambda = 50\) (red) and \(\lambda = 100\) (blue).

Notes

The CVXR solution is not quite exactly that of l1tf: on the left it shows a larger difference for the two \(\lambda\) values; in the middle, it is less flatter than l1tf; and on the right, it does not have as many knots as l1tf.

Session Info

sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-apple-darwin21.6.0 (64-bit)
## Running under: macOS Ventura 13.0
## 
## Matrix products: default
## BLAS:   /usr/local/Cellar/openblas/0.3.21/lib/libopenblasp-r0.3.21.dylib
## LAPACK: /usr/local/Cellar/r/4.2.1_4/lib/R/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
## [1] l1tf_0.0.0.9000 ggplot2_3.3.6   CVXR_1.0-11    
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.0 xfun_0.34        bslib_0.4.0      slam_0.1-50     
##  [5] lattice_0.20-45  Rmosek_10.0.25   colorspace_2.0-3 vctrs_0.5.0     
##  [9] generics_0.1.3   htmltools_0.5.3  yaml_2.3.6       gmp_0.6-6       
## [13] utf8_1.2.2       rlang_1.0.6      jquerylib_0.1.4  pillar_1.8.1    
## [17] glue_1.6.2       Rmpfr_0.8-9      withr_2.5.0      DBI_1.1.3       
## [21] Rcplex_0.3-5     bit64_4.0.5      lifecycle_1.0.3  stringr_1.4.1   
## [25] munsell_0.5.0    blogdown_1.13    gtable_0.3.1     gurobi_9.5-2    
## [29] codetools_0.2-18 evaluate_0.17    labeling_0.4.2   knitr_1.40      
## [33] fastmap_1.1.0    fansi_1.0.3      cccp_0.2-9       highr_0.9       
## [37] Rcpp_1.0.9       scales_1.2.1     cachem_1.0.6     jsonlite_1.8.3  
## [41] farver_2.1.1     bit_4.0.4        digest_0.6.30    stringi_1.7.8   
## [45] bookdown_0.29    dplyr_1.0.10     Rglpk_0.6-4      grid_4.2.1      
## [49] ECOSolveR_0.5.4  cli_3.4.1        tools_4.2.1      magrittr_2.0.3  
## [53] sass_0.4.2       tibble_3.1.8     pkgconfig_2.0.3  rcbc_0.1.0.9001 
## [57] Matrix_1.5-1     assertthat_0.2.1 rmarkdown_2.17   R6_2.5.1        
## [61] compiler_4.2.1

Source

R Markdown

References

Kim, Seung-Jean, Kwangmoo Koh, Stephen Boyd, and Dimitry Gorinevsky. 2009. \(l_1\) Trend Filtering.” SIAM Review 51 (2): 339–60. https://doi.org/doi:10.1137/070690274.