logo

How to Use l1 Trend Filtering in Julia 📂Julia

How to Use l1 Trend Filtering in Julia

Overview

L1TrendFiltering.jl is a Julia package that translates the $\ell_{1}$ trend filtering code originally implemented in MATLAB1. The $\ell_{1}$ finds the solution $x = \left\{ x_{t} \right\}_{t=1}^{n}$ to the following optimization problem for the given time series data $y = \left\{ y_{t} \right\}_{t = 1}^{n}$ and $\lambda \ge 0$2. $$ \argmin_{x} {\frac{ 1 }{ 2 }} \left\| y - x \right\|_{2}^{2} + \lambda \left\| D x \right\|_{1} $$

Here, the matrix $D \in \mathbb{R}^{(n-2) \times n}$ is a Toeplitz matrix designed to reflect the second-order difference. $$ D = \begin{bmatrix} 1 & -2 & 1 & & & \\ & 1 & -2 & 1 & & \\ & & \ddots & \ddots & \ddots & \\ & & & 1 & -2 & 1 \end{bmatrix} $$

Code

alt text

The result of applying $\ell_{1}$ trend filtering to the time series $y$ after importing the SNP 500 data is shown above.

using L1TrendFiltering
using Plots, CSV, DataFrames

y = snp500
result = l1tf(y, 50, verbose = true)
x = result.x

plot(ylabel = "log(price)")
plot!(y, label = "S&P500")
plot!(x, label = "l1tf")

Environment

  • OS: Windows
  • Julia: v1.11.1

  1. https://github.com/dsryu0822/L1TrendFiltering.jl ↩︎

  2. Kim, S. J., Koh, K., Boyd, S., & Gorinevsky, D. (2009). \ell_1 trend filtering. SIAM review, 51(2), 339-360. https://stanford.edu/~boyd/l1_tf/ ↩︎