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 1\ell_{1} trend filtering code originally implemented in MATLAB1. The 1\ell_{1} finds the solution x={xt}t=1nx = \left\{ x_{t} \right\}_{t=1}^{n} to the following optimization problem for the given time series data y={yt}t=1ny = \left\{ y_{t} \right\}_{t = 1}^{n} and λ0\lambda \ge 02. arg minx12yx22+λDx1 \argmin_{x} {\frac{ 1 }{ 2 }} \left\| y - x \right\|_{2}^{2} + \lambda \left\| D x \right\|_{1}

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

Code

alt text

The result of applying 1\ell_{1} trend filtering to the time series yy 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/ ↩︎