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)

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) 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) Figure 2: CVXR estimated $$L_1$$ trends for $$\lambda = 50$$ (red) and $$\lambda = 100$$ (blue).

Session Info

sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-apple-darwin18.5.0 (64-bit)
## Running under: macOS Mojave 10.14.5
##
## Matrix products: default
## BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.6_1/lib/libopenblasp-r0.3.6.dylib
##
## locale:
##  en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
##  stats     graphics  grDevices datasets  utils     methods   base
##
## other attached packages:
##  l1tf_0.0.0.9000 ggplot2_3.1.1   CVXR_0.99-6
##
## loaded via a namespace (and not attached):
##   gmp_0.5-13.5      Rcpp_1.0.1        highr_0.8
##   compiler_3.6.0    pillar_1.4.1      plyr_1.8.4
##   R.methodsS3_1.7.1 R.utils_2.8.0     tools_3.6.0
##  digest_0.6.19     bit_1.1-14        evaluate_0.14
##  tibble_2.1.2      gtable_0.3.0      lattice_0.20-38
##  pkgconfig_2.0.2   rlang_0.3.4       Matrix_1.2-17
##  yaml_2.2.0        blogdown_0.12.1   xfun_0.7
##  withr_2.1.2       dplyr_0.8.1       Rmpfr_0.7-2
##  ECOSolveR_0.5.2   stringr_1.4.0     knitr_1.23
##  tidyselect_0.2.5  bit64_0.9-7       grid_3.6.0
##  glue_1.3.1        R6_2.4.0          rmarkdown_1.13
##  bookdown_0.11     purrr_0.3.2       magrittr_1.5
##  scales_1.0.0      htmltools_0.3.6   scs_1.2-3
##  assertthat_0.2.1  colorspace_1.4-1  labeling_0.3
##  stringi_1.4.3     lazyeval_0.2.2    munsell_0.5.0
##  crayon_1.3.4      R.oo_1.22.0

R Markdown