logo

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

Juliaで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} $$

コード

alt text

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

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