thumbnail

リッジ回帰の数式の導出方法を徹底解説

リッジ回帰の数式を導出・解説します。流れが詳しくわかるよう、丁寧に式変形するよう心がけております。まずは通常の回帰の数式の導出から始め、リッジ回帰の数式の理解につなげます。

線形回帰

リッジ回帰の数式の解説の前に、線形回帰の概要と数式を解説します。

線形回帰の概要

線形回帰では、データの分布を最も良く近似する線形関数を求める。

次のようなデータ{(xi,yi)}i=1N\left\{(\mathbf{x}_i, y_i)\right\}_{i=1}^Nがあるとき、これらのデータを最もよく近似する超平面ax=0\mathbf{a}^\top\mathbf{x}=\mathbf{0}を求める。

線形回帰の数式

オレンジの超平面と各データ点の誤差を最小化するような超平面を求めるために、誤差を2乗した損失関数を考え、それを最小化する。2乗誤差を損失関数として利用する理由は、凸なため微分により最小値が求まるためである。

各データ点と超平面の2乗誤差は次のとおりである。

L(a)=i=1N(yiaxi)2=i=1N(axiyi)2=[x1ay1xNayN][x1ay1xNayN]=[x1ay1xNayN][x1ay1xNayN]=[x1ay1xNayN]2=[x1xN]a[y1yN]2=Xay2 \begin{align*} L(\mathbf{a}) &= \sum_{i=1}^N(y_i-\mathbf{a}^\top\mathbf{x}_i)^2 \\ &= \sum_{i=1}^N(\mathbf{a}^\top\mathbf{x}_i-y_i)^2 \\ &=\begin{bmatrix} \mathbf{x}_1^\top\mathbf{a}-y_1 \cdots \mathbf{x}_N^\top\mathbf{a}-y_N \end{bmatrix}\begin{bmatrix} \mathbf{x}_1^\top\mathbf{a}-y_1 \\ \vdots \\\mathbf{x}_N^\top\mathbf{a}-y_N \end{bmatrix} \\ &=\begin{bmatrix} \mathbf{x}_1^\top\mathbf{a}-y_1 \\ \vdots \\\mathbf{x}_N^\top\mathbf{a}-y_N \end{bmatrix}^\top\begin{bmatrix} \mathbf{x}_1^\top\mathbf{a}-y_1 \\ \vdots \\\mathbf{x}_N^\top\mathbf{a}-y_N \end{bmatrix} \\ &= \left\| \begin{bmatrix} \mathbf{x}_1^\top\mathbf{a}-y_1 \\ \vdots \\\mathbf{x}_N^\top\mathbf{a}-y_N \end{bmatrix} \right\|^2 \\ &= \left\| \begin{bmatrix} \mathbf{x}_1^\top \\ \vdots \\\mathbf{x}_N^\top \end{bmatrix}\mathbf{a}- \begin{bmatrix}y_1\\\vdots\\y_N\end{bmatrix}\right\|^2 \\ &= \|\mathbf{X}\mathbf{a} – \mathbf{y} \|^2 \end{align*}

aL=0\nabla_{\mathbf{a}}L=\mathbf{0}と微分して0とおき、a\mathbf{a}を求めると、

0=2X(Xay)XXa=Xya=(XX)1Xy\begin{align*}0&=2\mathbf{X}^\top(\mathbf{X}\mathbf{a}-\mathbf{y}) \\ \mathbf{X}^\top\mathbf{X}\mathbf{a} &= \mathbf{X}^\top\mathbf{y} \\ \therefore \mathbf{a} &= (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y} \end{align*}

このaaax=0\mathbf{a}^\top\mathbf{x}=\mathbf{0}に代入することにより、超平面が求まる。

なお、行列の微分による式変形がわからない方は、以下の記事を参考にされたし。

リッジ回帰

リッジ回帰の概要

線形回帰の2乗誤差を最小化する過程において、複雑さを抑えるために、超平面の重みベクトルaのl2ノルムも同時に最小化する。

すなわち、次の式を最小化する。

i=1N(yiaxi)2+λa22\sum_{i=1}^N(y_i-\mathbf{a}^\top\mathbf{x}_i)^2+\lambda\|\mathbf{a}\|_2^2

リッジ回帰の数式

aL=0\nabla_\mathbf{a}L=\mathbf{0}と微分して0とおき、a\mathbf{a}を求める。線形回帰の式と同様に変形すると、

a=(XX+λI)1Xy\mathbf{a}= (\mathbf{X}^\top\mathbf{X}+\lambda\mathbf{I})^{-1}\mathbf{X}^\top\mathbf{y}

このaaax=0\mathbf{a}^\top\mathbf{x}=\mathbf{0}に代入することにより、超平面が求まる。

発展

カーネルリッジ回帰については、次の記事をご覧ください。