logo

残差二乗和の勾配 📂ベクトル分析

残差二乗和の勾配

概要

統計学および機械学習の多くの回帰問題では、以下のように残差平方和目的関数として使用し、特に$f$が線形結合の場合、行列形で簡潔に表現することができる。 $$ \begin{align*} RSS =& \sum_{k} \left( y_{k} - f \left( \mathbf{x}_{k} \right) \right)^{2} \\ =& \sum_{k} \left( y_{k} - \left( s_{0} + s_{1} x_{k1} + \cdots + s_{p} x_{kp} \right) \right)^{2} \\ =& \left( \mathbf{y} - X \mathbf{s} \right)^{T} \left( \mathbf{y} - X \mathbf{s} \right) \\ =& \left\| \mathbf{y} - X \mathbf{s} \right\|_{2}^{2} \end{align*} $$ これをさらに一般化して、行列$R \in \mathbb{R}^{n \times n}$に対して、以下の形を持つスカラー関数のグラディエントを導出する。

公式 1

$$ f \left( \mathbf{s} \right) := \left( \mathbf{y} - X \mathbf{s} \right)^{T} R \left( \mathbf{y} - X \mathbf{s} \right) $$ $\mathbf{s}$に依存しないベクトル$\mathbf{y} \in \mathbb{R}^{n}$と行列$X \in \mathbb{R}^{n \times p}$、$R \in \mathbb{R}^{n \times n}$に対して、以下が成り立つ。 $$ {{ \partial f \left( \mathbf{s} \right) } \over { \partial \mathbf{s} }} = - X^{T} \left( R + R^{T} \right) \left( \mathbf{y} - X \mathbf{s} \right) $$

導出

転置行列の性質: $r,s\in \mathbb{R}$と$A,B$がそれぞれの場合で行列操作が適切に定義されるサイズを持つとする。すると、以下が成立する。

  • (a) 線形性: $$\left( rA + sB\right)^{T}=r A^{T} + s B^{T}$$

ベクトルと行列のグラディエント: $$ \frac{ \partial \mathbf{w}^{T}\mathbf{x}}{ \partial \mathbf{w} } = \frac{ \partial \mathbf{x}^{T}\mathbf{w}}{ \partial \mathbf{w} } = \mathbf{x} $$ $$ \frac{ \partial }{ \partial \mathbf{w} }\left( \mathbf{w}^{T}\mathbf{R}\mathbf{w} \right)= \left( \mathbf{R} + \mathbf{R}^{T} \right) \mathbf{w} $$

$$ \begin{align*} {{ \partial } \over { \partial \mathbf{s} }} f \left( \mathbf{s} \right) =& {{ \partial } \over { \partial \mathbf{s} }} \left( \mathbf{y} - X \mathbf{s} \right)^{T} R \left( \mathbf{y} - X \mathbf{s} \right) \\ =& {{ \partial } \over { \partial \mathbf{s} }} \left( \mathbf{y}^{T} - \mathbf{s}^{T} X^{T} \right) R \left( \mathbf{y} - X \mathbf{s} \right) \\ =& {{ \partial } \over { \partial \mathbf{s} }} \left( - \mathbf{s}^{T} X^{T} R \mathbf{y} - \mathbf{y}^{T} R X \mathbf{s} + \mathbf{s}^{T} X^{T} R X \mathbf{s} \right) \\ =& - X^{T} R \mathbf{y} - X^{T} R^{T} \mathbf{y} + X^{T} \left( R + R^{T} \right) X \mathbf{s} \\ =& - X^{T} \left( R + R^{T} \right) \mathbf{y} + X^{T} \left( R + R^{T} \right) X \mathbf{s} \\ =& - X^{T} \left( R + R^{T} \right) \left( \mathbf{y} - X \mathbf{s} \right) \end{align*} $$

系1

系として、$R$が対称行列ならば $$ {{ \partial f \left( \mathbf{s} \right) } \over { \partial \mathbf{s} }} = - 2 X^{T} R \left( \mathbf{y} - X \mathbf{s} \right) $$ そして、単位行列ならば、次を得る。 $$ {{ \partial f \left( \mathbf{s} \right) } \over { \partial \mathbf{s} }} = - 2 X^{T} \left( \mathbf{y} - X \mathbf{s} \right) $$

系2

アダマール積$\odot$について、$f(\mathbf{s}) := \left\| X(\boldsymbol{\tau} \odot \mathbf{s}) - \mathbf{y} \right\|_{2}^{2}$と定義すると、$X(\boldsymbol{\tau} \odot \mathbf{s}) = X \diag(\boldsymbol{\tau}) \mathbf{s}$が適用されるので

$$ \begin{align*} \dfrac{\partial f(\mathbf{s})}{\partial \mathbf{s}} & = 2 \left( X \diag(\boldsymbol{\tau}) \right)^{T} \left( X \diag(\boldsymbol{\tau})\mathbf{s} - \mathbf{y}\right) \\ & = 2 \diag(\boldsymbol{\tau})^{T} X^{T} \left( X (\boldsymbol{\tau} \odot \mathbf{s}) - \mathbf{y}\right) \\ & = 2 \boldsymbol{\tau} \odot X^{T} \left( X (\boldsymbol{\tau} \odot \mathbf{s}) - \mathbf{y}\right) \\ \end{align*} $$


  1. Petersen. (2008). The Matrix Cookbook: p10. ↩︎