잔차제곱합의 그래디언트
개요
통계학 및 머신러닝의 많은 회귀문제에서는 다음과 같이 잔차제곱합을 목적 함수로 사용하며, 특히 $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*} $$
따름정리3
한 점 $\mathbf{a}$ 과 벡터 $\mathbf{x} = \mathbf{x} (t)$ 사이의 거리로써 $l^{2}$-놈 $\left\| \mathbf{x} - \mathbf{a} \right\|$ 의 도함수는 다음과 같다. $$ {\frac{ d \left\| \mathbf{x} - \mathbf{a} \right\| }{ d t }} = \dot{\mathbf{x}} \cdot {\frac{ \mathbf{x} - \mathbf{a} }{ \left\| \mathbf{x} - \mathbf{a} \right\| }} $$ 이는 $R$ 와 $X$ 가 항등행렬이면서, $\sqrt{\cdot}$ 에 체인룰이 들어가면서 유도된다. $$ \begin{align*} & {\frac{ d \left\| \mathbf{x} - \mathbf{a} \right\| }{ d t }} \\ =& {\frac{ d }{ d t }} \sqrt{ \left\| \mathbf{a} - \mathbf{x} \right\|_{2}^{2} } \\ =& {\frac{ 1 }{ 2 \sqrt{ \left\| \mathbf{a} - \mathbf{x} \right\|_{2}^{2} } }} {\frac{ d }{ d t }} \left( \mathbf{a} - \mathbf{x} \right)^{T} \left( \mathbf{a} - \mathbf{x} \right) \\ =& {\frac{ - 2 \left( \mathbf{a} - \mathbf{x} \right) }{ 2 \left\| \mathbf{x} - \mathbf{a} \right\| } } \cdot {\frac{ d }{ d t }} \mathbf{x} \\ =& {\frac{ \mathbf{x} - \mathbf{a} }{ \left\| \mathbf{x} - \mathbf{a} \right\| }} \cdot \dot{\mathbf{x}} \end{align*} $$
- 따름정리3은 타원의 광학적 성질을 증명하는데 쓰일 수도 있다.
Petersen. (2008). The Matrix Cookbook: p10. ↩︎