Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

二乗誤差の分解

式の変形の仕方によって

  1. MSEをBiasの二乗とVarianceに分解できること

  2. 二乗誤差に対する最適解(ベイズ規則 Bayes rule)が条件付き期待値であること

という異なる2つの説明ができる

二乗誤差のBias-Variance分解

予測値E[y^]\mathbb{E}[\hat{y}]について引いて足す場合

ED[(y^,y)]=E[(yy^)2]=E[(yE[y^]+E[y^]y^)2](E[y^]を引いて足す)=E[(yE[y^])2+2(yE[y^])(E[y^]y^)+(E[y^]y^)2]=E[(yE[y^])2]+2E[(yE[y^])(E[y^]y^)]=2E[yE[y^]yy^E[y^]2+E[y^]y^]=2(yE[y^]yE[y^]E[y^]2+E[y^]2)=0+E[(E[y^]y^)2]=(yE[y^])2+Var[y^]=Bias2+Variance\begin{aligned} \mathbb{E}_D[\ell(\hat{y}, y)] &= \mathbb{E}[(y - \hat{y})^2]\\ &= \mathbb{E}[(y - \mathbb{E}[\hat{y}] + \mathbb{E}[\hat{y}] - \hat{y})^2] \quad (\mathbb{E}[\hat{y}] を引いて足す)\\ &= \mathbb{E}[ (y - \mathbb{E}[\hat{y}])^2 + 2(y - \mathbb{E}[\hat{y}])(\mathbb{E}[\hat{y}] - \hat{y}) + (\mathbb{E}[\hat{y}] - \hat{y})^2]\\ &= \mathbb{E}[(y - \mathbb{E}[\hat{y}])^2] + \underbrace{ 2 \mathbb{E}[(y - \mathbb{E}[\hat{y}])(\mathbb{E}[\hat{y}] - \hat{y})] }_{ \begin{aligned} &= 2 \mathbb{E}[ y \cdot \mathbb{E}[\hat{y}] - y \cdot \hat{y} - \mathbb{E}[\hat{y}]^2 + \mathbb{E}[\hat{y}] \cdot \hat{y}] \\ &= 2 ( y \cdot \mathbb{E}[\hat{y}] - y \cdot \mathbb{E}[\hat{y}] - \mathbb{E}[\hat{y}]^2 + \mathbb{E}[\hat{y}]^2 )\\ &=0 \end{aligned} } + \mathbb{E}[(\mathbb{E}[\hat{y}] - \hat{y})^2]\\ &= (y - \mathbb{E}[\hat{y}])^2 + \operatorname{Var}[\hat{y}]\\ &= \text{Bias}^2 + \text{Variance}\\ \end{aligned}
もっと丁寧に書くと…

目的変数YYが真の関数(信号成分)f(X)f(X)とノイズε\varepsilonの和 Y=f(X)+εY = f(X) + \varepsilon で構成されるとする。 また、データの分布について期待値をとる、つまり、訓練データを取得し直して推定するたびにばらつく分について期待値をとることをED[]\mathrm{E}_D[\cdot ] で表すとする。

あるデータ点X=xX=xについての真の関数f(x)f(x)と予測モデルf^(x)\hat{f}(x)の二乗誤差{f(x)f^(x)}2\{ f(x) - \hat{f}(x)\}^2ED[f^(x)]\mathrm{E}_D[\hat{f}(x)]を足して引いて展開すると以下のように整理できる。

{f(x)f^(x)}2={f(x)ED[f^(x)]+ED[f^(x)]f^(x)}2={f(x)ED[f^(x)]}2+2{f(x)ED[f^(x)]}{ED[f^(x)]f^(x)}+{ED[f^(x)]f^(x)}2\begin{aligned} \{ f(x) - \hat{f}(x)\}^2 = & \{ f(x) - \mathrm{E}_D[\hat{f}(x)] + \mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\}^2\\ = & \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \}^2 \\ &+2 \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \} \{ \mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\} \\ &+ \{\mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\}^2 \\ \end{aligned}

これについて期待値をとったED[{f(x)f^(x)}2]\mathrm{E}_D[\{ f(x) - \hat{f}(x)\}^2]について考えるとき、第2項は

2ED[{f(x)ED[f^(x)]}{ED[f^(x)]f^(x)}]=2ED[f(x)ED[f^(x)]f(x)f^(x)ED[f^(x)]2+ED[f^(x)]f^(x)]=2(ED[f(x)ED[f^(x)]]ED[f(x)f^(x)]ED[ED[f^(x)]2]+ED[ED[f^(x)]f^(x)])=2(ED[f(x)]ED[f^(x)]ED[f(x)]ED[f^(x)]ED[f^(x)]2+ED[f^(x)]2)=0\begin{aligned} & 2 \mathrm{E}_D[ \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \} \{ \mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\} ] \\ &= 2 \mathrm{E}_D\left[ f(x) \cdot \mathrm{E}_D[\hat{f}(x)] - f(x) \cdot \hat{f}(x) - \mathrm{E}_D[\hat{f}(x)]^2 + \mathrm{E}_D[\hat{f}(x)] \cdot \hat{f}(x) \right]\\ &= 2 \left( \mathrm{E}_D[ f(x) \cdot \mathrm{E}_D[\hat{f}(x)] ] - \mathrm{E}_D[f(x) \cdot \hat{f}(x)] - \mathrm{E}_D[\mathrm{E}_D[\hat{f}(x)]^2] + \mathrm{E}_D[ \mathrm{E}_D[\hat{f}(x)] \cdot \hat{f}(x) ]\right) \\ &= 2 \left( \mathrm{E}_D[ f(x) ] \cdot \mathrm{E}_D[\hat{f}(x)] - \mathrm{E}_D[f(x)] \cdot \mathrm{E}_D[\hat{f}(x)] - \mathrm{E}_D[\hat{f}(x)]^2 + \mathrm{E}_D[\hat{f}(x)]^2 \right) \\ &= 0 \end{aligned}

と消失するので、

ED[{f(x)f^(x)}2]=ED[{f(x)ED[f^(x)]}2]+2ED[{f(x)ED[f^(x)]}{ED[f^(x)]f^(x)}]+ED[{ED[f^(x)]f^(x)}2]={f(x)ED[f^(x)]}2+ED[{ED[f^(x)]f^(x)}2]=Bias2+Variance\begin{aligned} \mathrm{E}_D[\{ f(x) - \hat{f}(x)\}^2] = & \mathrm{E}_D[ \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \}^2 ] \\ &+ 2 \mathrm{E}_D[ \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \} \{ \mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\} ]\\ &+ \mathrm{E}_D[ \{\mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\}^2 ] \\ = & \{ f(x) - \mathrm{E}_D[\hat{f}(x)] \}^2 + \mathrm{E}_D[ \{\mathrm{E}_D[\hat{f}(x)] - \hat{f}(x)\}^2 ] \\ = & \mathrm{Bias}^2 + \mathrm{Variance} \end{aligned}

参考:はじパタ 第1章、PRML上3.2

実際に等式が成り立つのか実証的に確認

Source
# 二乗誤差の期待値とBias^2 + Varianceが一致するか
import pandas as pd
import numpy as np

# データを取り直して予測値を出し直したと仮定
n = 1000
y_true = 3
# 正規分布に従ってばらついたと仮定(実際も中心極限定理により正規分布に従うはず)
np.random.seed(0)
df = pd.DataFrame({"y_hat": np.random.normal(loc=y_true, scale=1, size=n)})
df["y"] = y_true # ある一点の実測値
# display(df.tail(3))

bias = df["y"].mean() - df["y_hat"].mean()
variance = df["y_hat"].var()
epe = ((df["y"] - df["y_hat"])**2).mean()

print(f"""
- bias: {bias:.3f}
- variance: {variance:.3f}

二乗誤差の期待値とBias^2 + Varianceが一致するか
- 二乗誤差の期待値 EPE: {epe:.3f}
- Bias^2 + variance: {bias**2 + variance:.3f}
- 差分(EPE - bias^2 + variance): {epe - (bias**2 + variance):.3f}
""")

- bias: 0.045
- variance: 0.975

二乗誤差の期待値とBias^2 + Varianceが一致するか
- 二乗誤差の期待値 EPE: 0.976
- Bias^2 + variance: 0.977
- 差分(EPE - bias^2 + variance): -0.001

二乗誤差のベイズ規則

実測値E[y]\mathbb{E}[y]について引いて足す場合。

損失関数として二乗損失 (y^,y)=(y^y)2\ell(\hat{y}, y) = (\hat{y} - y)^2を利用するとき、

E[(y^,Y)]=E[(y^Y)2]=E[(y^E[Y]+E[Y]Y)2](E[Y]を引いて足す)=E[(y^E[Y])2+2(y^E[Y])(E[Y]Y)+(E[Y]y)2]=E[(y^E[Y])2]=(y^E[Y])2+2E[(y^E[Y])(E[Y]Y)]=2E[y^E[Y]y^YE[Y]2+E[Y]Y]=2y^E[Y]2y^E[Y]2E[Y]2+2E[Y]2=0+E[(E[Y]Y)2]=(y^E[Y])2+Var[Y]\begin{aligned} \mathbb{E}[\ell(\hat{y}, Y)] &= \mathbb{E}[(\hat{y} - Y)^2]\\ &= \mathbb{E}[(\hat{y} - \mathbb{E}[Y] + \mathbb{E}[Y] - Y)^2] \quad (\mathbb{E}[Y]を引いて足す)\\ &= \mathbb{E}[ (\hat{y} - \mathbb{E}[Y])^2 + 2(\hat{y} - \mathbb{E}[Y])(\mathbb{E}[Y] - Y) + (\mathbb{E}[Y] - y)^2]\\ &= \underbrace{ \mathbb{E}[(\hat{y} - \mathbb{E}[Y])^2] }_{=(\hat{y} - \mathbb{E}[Y])^2} + \underbrace{ 2 \mathbb{E}[(\hat{y} - \mathbb{E}[Y])(\mathbb{E}[Y] - Y)] }_{ \begin{aligned} &=2 \mathbb{E}[\hat{y}\mathbb{E}[Y] - \hat{y}Y - \mathbb{E}[Y]^2 + \mathbb{E}[Y]Y]\\ &=2 \hat{y}\mathbb{E}[Y] - 2\hat{y}\mathbb{E}[Y] - 2\mathbb{E}[Y]^2 + 2\mathbb{E}[Y]^2\\ &=0 \end{aligned} } + \mathbb{E}[(\mathbb{E}[Y] - Y)^2]\\ &= (\hat{y} - \mathbb{E}[Y])^2 + \operatorname{Var}[Y]\\ \end{aligned}

となる。よってy^=E[Y]\hat{y}=\mathbb{E}[Y]とすれば予測誤差が最小になる。

この期待値を条件付き期待値に置き換えて考えると、ベイズ規則h0(X)h_0(X)

h0(X)=E[YX]h_0(X) = \mathbb{E}[Y|X]

によって与えられる。