LoginSignup
18
19

More than 5 years have passed since last update.

線形回帰の求め方で二乗和を2で割る理由

Last updated at Posted at 2016-11-05

TL;DR

ないけれど、この先こうしておくと式が書きやすいことが知られている。コード規約みたいなもの。

本題

やる夫で学ぶ機械学習 - 単回帰問題 -でなぜ誤差の自乗を2で割っているのか分からないという疑問を見かけたので、ざくっと説明する。

こういう数式をサクッと書きたいのだけど何かいい方法はないかなあ…。
と言っていたらMathJaxの数式に書き換え、修正していただけました。(@yajuさんありがとうございました。)

説明1

今解こうとしている問題は、$E(\theta) = \displaystyle{\sum_i ( y^{(i)} - f_\theta (x^{(i)} )^2}$ を最小
にする $\theta = (\theta_0,\theta_1)$ を求めることだが、任意の $k > 0$に対し
「$\theta = \hat{\theta}$ で $E(\theta)$ が最小」と「$\theta = \hat{\theta}$ で $kE(\theta)$ が最小」
は同値なので、$\displaystyle{\frac{1}{2}E(\theta)}$ を改めて $E(\theta)$ とする。

説明2

$E(\theta) = \displaystyle{\sum_i ( y^{(i)} - f_\theta (x^{(i)} ))^2}$ とした場合
この時、最急降下法では、$\eta > 0$ として

\left\{\begin{matrix}
\theta_0 := \displaystyle{\theta_0 - \eta( \frac{\partial E}{\partial \theta_0})} & \\ \theta_1 := \displaystyle{\theta_1 - \eta( \frac{\partial E}{\partial \theta_1})} & \end{matrix}\right.

ここで

\theta_0 - \eta \frac{\partial E}{\partial \theta_0} = \theta_0 - 2\eta(\frac{1}{2}\frac{\partial E}{\partial \theta_0}) \\
 \quad \quad \quad \quad \quad \quad = \theta_0 - 2\eta(\frac{\partial}{\partial \theta_0}(\frac{1}{2}E))
 \quad \quad \theta_1 - \eta \frac{\partial E}{\partial \theta_1} = \theta_1 - 2\eta(\frac{\partial}{\partial \theta_1}(\frac{1}{2}E)) 

より、$E'(\theta) = \displaystyle{\frac{1}{2}E(\theta)}$ , ${\eta}' = 2\eta$ として($\eta$は任意で良いため。)

\left\{\begin{matrix}
\theta_0 := \displaystyle{\theta_0 - \eta( \frac{\partial E}{\partial \theta_0}) = \theta_0 - \eta'( \frac{\partial E'}{\partial \theta_0})}  & \\ 
\theta_1 := \displaystyle{\theta_1 - \eta( \frac{\partial E}{\partial \theta_1}) = \theta_1 - \eta'( \frac{\partial E}{\partial \theta_1})} & 
\end{matrix}\right.

なので、$E(\theta) = \displaystyle{\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2}$ に対する
最急降下法と $E'(\theta) = \displaystyle{\frac{1}{2}\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2}$
に対する最急降下法は同じ

説明3

より具体的な計算を与える。
問題を次のように定式化する。

Problem
列 $(x^{(i)}, y^{(i)})$ $\small{i=0,...,n}$ が与えられたとき、
$f_0(x) = \theta_0 + \theta_1x$ に対し
$E(\theta) = \displaystyle{\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2}$ を最小にする
$\theta = (\theta_0,\theta_1)$ を見つける

なお、最小性については最急降下法により
満たされるものとし、ここでは考えない。
このとき、最急降下法の更新式は $\eta > 0$ として

\theta_0 := \theta_0 - \eta \frac{\partial E}{\partial \theta_0} = \theta_0 - \eta \frac{\partial}{\partial \theta_0}\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2)}  \\
\quad \quad\ \quad\quad \quad \  = \theta_0 - \eta\displaystyle{(\sum_i 2( y^{(i)} - f_\theta (x^{(i)}) )^2)}\\
\theta_0 := \theta_0 - 2\eta\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)} ))^2)} \ \ \quad \quad\ \quad \quad\ \quad

同様に

\theta_1 := \theta_1 - 2\eta\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2)} \ \ \quad \quad\ \quad \quad\ \quad

この式において次に注意する。

\theta_0 := \theta_0 - 2\eta\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)} ))^2)} \ \ \quad \quad\ \quad \quad\ \quad

$\theta_0$ … $f_\theta(x) = \theta_0 + \theta_1x$ は自分で決め打ちした
$2\eta$ … $2\eta$ のうち、$\eta > 0$ は適当な値で良く、特に「2」自体には何の意味がない
→ $\eta' = 2\eta$ として $2$を消したい
$y^{(i)}$と$x^{(i)}$ … $(x^{(i)},y^{(i)})$ は実データなので重要

同様に $\theta_1$ の更新式は

\theta_1 := \theta_1 - 2\eta\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)} ))x^{(i)})} \ \ \quad \quad\ \quad \quad\ \quad

なので、$\eta' = 2\eta$ として

\left\{\begin{matrix}
\theta_0 := \theta_0 - \eta'\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)} )))}\quad & \\ 
\theta_1 := \theta_1 - \eta'\displaystyle{(\sum_i ( y^{(i)} - f_\theta (x^{(i)} ))x^{(i)})} & 
\end{matrix}\right.

とすると更新式がすっきりする。
$\eta'$ を用いると $\eta = \displaystyle{\frac{1}{2}\eta'}$ なので

\theta_0 - \eta \frac{\partial E}{\partial \theta_0} = \theta_0 - \frac{1}{2}\eta'(\frac{\partial E}{\partial \theta_0}) \\
 \quad \quad \quad \quad \quad \ \ = \theta_0 - \eta'\frac{\partial}{\partial \theta_0}(\frac{1}{2}E)

同様に

 \quad \theta_1 - \eta \frac{\partial E}{\partial \theta_1} = \theta_1 - \eta'\frac{\partial}{\partial \theta_1}(\frac{1}{2}E)

よって、 $E'(\theta) = \displaystyle{\frac{1}{2}E}$ とすると

\left\{\begin{matrix}
\theta_0 := \displaystyle{\theta_0 - \eta'\frac{\partial E'}{\partial \theta_0} = \theta_0 - \eta'(\sum_i ( y^{(i)} - f_\theta (x^{(i)}) ))}\quad & \\ 
\theta_1 := \displaystyle{\theta_1 - \eta'\frac{\partial E'}{\partial \theta_1} = \theta_1 -  \eta'(\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )x^{(i)})} & 
\end{matrix}\right.

となり式がすっきり書ける。

このため 
$E(\theta) = \displaystyle{\quad \sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2}$ とするよりは
$E(\theta) = \displaystyle{\frac{1}{2}\sum_i ( y^{(i)} - f_\theta (x^{(i)}) )^2}$ とする方が
計算が楽になる。


手描き版はこちら。
https://goo.gl/photos/KkFM68cmyDUExShA8

なお、誤りを含みます
(4枚目で$\eta$に'が一箇所ついていない & $\theta_0$と$\theta_1$が間違っている)

18
19
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19