logo

ジュリアでl1トレンドフィルタリングを使用する方法 📂ジュリア

ジュリアでl1トレンドフィルタリングを使用する方法

概要

L1TrendFiltering.jlは元々マトラボで実装された1\ell_{1}トレンドフィルタリングコードをジュリアに翻訳したパッケージだ11\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/ ↩︎