logo

줄리아에서 l1 트렌드 필터링 사용하는 법 📂줄리아

줄리아에서 l1 트렌드 필터링 사용하는 법

개요

L1TrendFiltering.jl는 원래 매트랩으로 구현된 1\ell_{1} 트렌드 필터링 코드를 줄리아로 번역한 패키지다1. 1\ell_{1} 은 주어진 시계열 데이터 y={yt}t=1ny = \left\{ y_{t} \right\}_{t = 1}^{n}λ0\lambda \ge 0 에 대해 다음과 같은 최적화 문제의 해인 x={xt}t=1nx = \left\{ x_{t} \right\}_{t=1}^{n} 를 찾는다2. arg minx12yx22+λDx1 \argmin_{x} {\frac{ 1 }{ 2 }} \left\| y - x \right\|_{2}^{2} + \lambda \left\| D x \right\|_{1}

여기서 행렬 DR(n2)×nD \in \mathbb{R}^{(n-2) \times n} 는 다음과 같이 2차 차분을 반영하기 위한 퇴플리츠 행렬이다. D=[121121121] D = \begin{bmatrix} 1 & -2 & 1 & & & \\ & 1 & -2 & 1 & & \\ & & \ddots & \ddots & \ddots & \\ & & & 1 & -2 & 1 \end{bmatrix}

코드

alt text

SNP 500 데이터를 시계열 yy 로 가져와서 1\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

  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/ ↩︎