줄리아에서 l1 트렌드 필터링 사용하는 법
개요
L1TrendFiltering.jl
는 원래 매트랩으로 구현된 $\ell_{1}$ 트렌드 필터링 코드를 줄리아로 번역한 패키지다1. $\ell_{1}$ 은 주어진 시계열 데이터 $y = \left\{ y_{t} \right\}_{t = 1}^{n}$ 와 $\lambda \ge 0$ 에 대해 다음과 같은 최적화 문제의 해인 $x = \left\{ x_{t} \right\}_{t=1}^{n}$ 를 찾는다2.
$$
\argmin_{x} {\frac{ 1 }{ 2 }} \left\| y - x \right\|_{2}^{2} + \lambda \left\| D x \right\|_{1}
$$
여기서 행렬 $D \in \mathbb{R}^{(n-2) \times n}$ 는 다음과 같이 2차 차분을 반영하기 위한 퇴플리츠 행렬이다. $$ D = \begin{bmatrix} 1 & -2 & 1 & & & \\ & 1 & -2 & 1 & & \\ & & \ddots & \ddots & \ddots & \\ & & & 1 & -2 & 1 \end{bmatrix} $$
코드
SNP 500 데이터를 시계열 $y$ 로 가져와서 $\ell_{1}$ 트렌드 필터링을 적용한 결과는 위와 같다.
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")
환경
- OS: Windows
- julia: v1.11.1
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/ ↩︎