線形回帰
回帰問題。入力xに対する出力yを予測する。
シンプルかつ表現力も豊かで、あらゆる手法の基礎になる。
本シリーズで用いる数式の表記一覧
本シリーズでは独自記号として、以下の「列挙(enumerate)」を使う。
\overset{n}{\underset{i=1}{\sf E}} x _ i = x _ 1, x _ 2, \ldots, x _ n
たとえばこれを用いて集合を定義することができる。
\Bigl\{ \overset{n}{\underset{i=1}{\sf E}} x _ i \Bigr\} = \{ x _ 1, x _ 2, \ldots, x _ n \}
モデル
入力x \in \mathcal{X}、特徴量写像\phi(x) \colon \mathcal{X} \to \mathbb{R} ^ n、出力y \in \mathbb{R}の状況。
出力yについて、\phi(x)の線形結合に、正規分布に従うノイズが加わった生成モデルを仮定。
y = \sum _ {i = 1} ^ n w _ {i} \phi _ i (x) + \varepsilon \quad \mathrm{where} \,\, \varepsilon \sim \mathcal{N}(\varepsilon | 0, \sigma ^ 2)
見づらいのでベクトル表記する。
y = w ^ \mathrm{T} \phi (x) + \varepsilon
このとき、出力yは平均w ^ \mathrm{T} \phi (x)、分散\sigma ^ 2の正規分布に従う。
y \sim \mathcal{N}(y | w ^ \mathrm{T} \phi(x), \sigma ^ 2)
出力の予測値\hat{y} \in \mathbb{R}は以下で表現する。
\hat{y} = w ^ \mathrm{T} \phi(x)
学習
N個のデータを観測したデータセット\mathcal{D}が以下で与えられたとする。
\mathcal{D} = \Bigl\{ \overset{N}{\underset{k=1}{\sf E}} (x ^ {(k)}, y ^ {(k)}) \Bigr\}
すべてのデータが独立同分布から生起している(iid: independent and identically distributed)と仮定すると、このデータセットの尤度\mathcal{L}(w)は
\mathcal{L}(w) = \prod _ {k=1} ^ N \mathcal{N}(y ^ {(k)} | w ^ \mathrm{T} \phi (x ^ {(k)}), \sigma ^ 2)
である。
パラメータwを最尤推定により求める。尤度をそのまま最大化するのは難しいので、対数尤度を最大化する(最適解は尤度を最大化した場合と一致する)。
\begin{aligned}
&\underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{max}} \log \mathcal{L}(w) \\
=& \, \underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{max}} \sum _ {k=1} ^ N \log \mathcal{N}(y ^ {(k)} | w ^ \mathrm{T} \phi (x ^ {(k)}), \sigma ^ 2) \\
=& \, \underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{max}} \sum _ {k=1} \log \exp \left(- \frac{\bigl( y ^ {(k)} - w ^ \mathrm{T} \phi (x ^ {(k)}) \bigr) ^ 2}{2 \sigma ^ 2} \right) + C \\
=& \, \underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{max}} \sum _ {k=1} \left(- \frac{\bigl( y ^ {(k)} - w ^ \mathrm{T} \phi (x ^ {(k)}) \bigr) ^ 2}{2 \sigma ^ 2} \right) \\
=& \, \underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{max}} - \frac{1}{2 \sigma ^ 2} \sum _ {k=1} ^ N \bigl( y ^ {(k)} - w ^ \mathrm{T} \phi (x ^ {(k)}) \bigr) ^ 2 \\
=& \, \underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{min}} \sum _ {k=1} ^ N \bigl( y ^ {(k)} - w ^ \mathrm{T} \phi (x ^ {(k)}) \bigr) ^ 2
\end{aligned}
以上により最小二乗法に帰着される。行列表記で記述するときは、
\Phi = \left(
\begin{matrix}
\phi _ 1 (x ^ {(1)}) & \phi _ 1 (x ^ {(2)}) & \cdots & \phi _ 1 (x ^ {(N)}) \\
\phi _ 2 (x ^ {(1)}) & \phi _ 2 (x ^ {(2)}) & \cdots & \phi _ 2 (x ^ {(N)}) \\
\vdots & \vdots & \ddots & \vdots \\
\phi _ n (x ^ {(1)}) & \phi _ n (x ^ {(2)}) & \cdots & \phi _ n (x ^ {(N)}) \\
\end{matrix}
\right)
y = \left(
\begin{matrix}
y ^ {(1)} \\
y ^ {(2)} \\
\vdots \\
y ^ {(N)}
\end{matrix}
\right), \quad w = \left(
\begin{matrix}
w _ 1 \\
w _ 2 \\
\vdots \\
w _ n
\end{matrix}
\right)
とおいて、
\underset{w \in \mathbb{R ^ n}}{\operatorname{arg} \operatorname{min}} \| y - \Phi ^\mathrm{T} w \| _ 2 ^ 2
と書ける。目的関数を
f(w) = \| y - \Phi ^ \mathrm{T} w \| _ 2 ^ 2
とおくと、その勾配は
\nabla f(w) = - 2 \Phi (y - \Phi ^ \mathrm{T} w)
である。勾配法などで解いてもよいが、\nabla f(w) = 0の微分方程式を直接解くことができる場合もある。
\begin{aligned}
&\Phi (y - \Phi ^ \mathrm{T} w) = 0 \\
& \therefore \Phi y = \Phi \Phi ^ \mathrm{T} w \\
& \therefore w = (\Phi \Phi ^ \mathrm{T}) ^ {-1} \Phi y
\end{aligned}
第2式から第3式への変形は\Phi \Phi ^ \mathrm{T}に逆行列が存在する場合に限られるので、微分方程式が解ける条件は\Phi \Phi ^ \mathrm{T}が正則になることである。ちなみに第2式
\Phi = \Phi \Phi ^ \mathrm{T} w
は正規方程式(normal equation)と呼ばれる。
具体的に\Phi \Phi ^ \mathrm{T}がどんな場合に正則にならないかというと、特徴量写像後に同じ値を取る入力点がデータセット\mathcal{D}内に存在すること、つまりあるk _ 1, k _ 2について
\| \phi(x ^ {(k _ 1)}) - \phi(x ^ {(k _ 2)}) \| = 0
が成り立つことである。簡単な例としてはx ^ {(k _ 1)} = x ^ {(k _ 2)}のとき、すなわち同じ入力点について複数回観測している場合である。実際はコンピュータには数値誤差が存在するので、ある微小な正数\deltaが存在して
\| \phi(x ^ {(k _ 1)}) - \phi(x ^ {(k _ 2)}) \| < \delta
のときに逆行列の計算に失敗する。
発展
- 一般化線形モデル(GLM: Generalized Linear Model)
- ロジスティック回帰(Logistic Regression)
- 一般化加法モデル(GAM: Generalized Additive Model)
- リッジ回帰(Ridge Regression)
- LASSO(Least Absolute Shrinkage and Selection Operator)
- カーネル回帰(Kernel Regression)
scikit-learn
sklearn.linear_model.LinearRegression
実装