階層ベイズで野球データ解析

打率を推定したい

gg_hatano
Mathematical Informatics, M2

Outline

1. 問題設定: シーズン打率を推定したい


2. 最尤推定で打率推定


3. 階層ベイズモデルで打率推定

問題設定

2013年メジャーリーグ, 4月20日までのデータ

FULLNAME ATBAT HITS AVG AVG_SEASON
1 Pedro Alvarez 52 6 0.12 0.23
2 Elvis Andrus 59 13 0.22 0.27
3 Norichika Aoki 61 18 0.29 0.29
4 Erick Aybar 28 9 0.32 0.27
5 Darwin Barney 11 1 0.09 0.21
6 Adrian Beltre 55 14 0.26 0.32
7 Carlos Beltran 51 13 0.26 0.30
8 Michael Bourn 45 15 0.33 0.26
9 Michael Brantley 50 12 0.24 0.28
10 Jay Bruce 71 21 0.30 0.26

Data : Major League Baseball Games (2013) ... Open Data

http://www.retrosheet.org/boxesetc/2013/Y_2013.htm

用語

ATBAT : 試行回数

HIT : 成功数

AVG : 打率

batter: 89人 (500打席以上)


FULLNAME ATBAT HITS AVG AVG_SEASON
1 Pedro Alvarez 52 6 0.12 0.23
2 Elvis Andrus 59 13 0.22 0.27
3 Norichika Aoki 61 18 0.29 0.29
4 Erick Aybar 28 9 0.32 0.27
5 Darwin Barney 11 1 0.09 0.21

目次

1. 問題設定: シーズン打率を推定したい


2. 最尤推定で打率推定


3. 階層ベイズモデルで打率推定

打席結果にランダム性を仮定


Ex. イチローの打席結果 (April,2013)

HIT, OUT, OUT, HIT, OUT, HIT, OUT, OUT , OUT, ...


\(n\) 安打数 / \(N\) 打席数 \(\to\) 尤度 \(\mathrm{L}(q)\):

\[\begin{align*} \mathrm{L}(q) &= \binom{N}{n}q^n (1-q)^{N-n}. \end{align*}\]

where \(q\) : 真の打率

\(\to\) 真の打率を予測したい

最尤推定

尤度: \(\mathrm{L}(q)\): \[\begin{align*} \mathrm{L}(q) = \binom{N}{n}q^n (1-q)^{N-n}. \end{align*}\]


最尤推定量 \(\hat{q}\):

\[\begin{align*} \hat{q} = \mathrm{argmax} ~ \mathrm{L}(q) \end{align*}\]

plot of chunk unnamed-chunk-3

打率を最尤推定

尤度 \(\mathrm{L}(q)\): \[\begin{align*} \mathrm{L}(q) = \binom{N}{n}q^n (1-q)^{N-n}. \end{align*}\]


尤度を最大化するパラメータ... \[\begin{align*} \frac{\partial}{\partial q_i} \log \mathrm{L}(q_i) &= 0, \\ \frac{n_i}{q_i} - \frac{N_i-n_i}{1-q_i} &= 0, \\ \hat{q_i} &= \frac{n_i}{N_i}. \end{align*}\]

FULLNAME AVG MLE AVG_SEASON
1 Pedro Alvarez 0.12 0.12 0.23
2 Elvis Andrus 0.22 0.22 0.27
3 Norichika Aoki 0.30 0.30 0.29
4 Erick Aybar 0.32 0.32 0.27
5 Darwin Barney 0.09 0.09 0.21
6 Adrian Beltre 0.25 0.25 0.32
7 Carlos Beltran 0.25 0.25 0.30
8 Michael Bourn 0.33 0.33 0.26
9 Michael Brantley 0.24 0.24 0.28
10 Jay Bruce 0.30 0.30 0.26

打率を最尤推定

plot of chunk unnamed-chunk-5

FULLNAME AVG MLE AVG_SEASON
1 Pedro Alvarez 0.12 0.12 0.23
2 Elvis Andrus 0.22 0.22 0.27
3 Norichika Aoki 0.30 0.30 0.29
4 Erick Aybar 0.32 0.32 0.27
5 Darwin Barney 0.09 0.09 0.21
6 Adrian Beltre 0.25 0.25 0.32
7 Carlos Beltran 0.25 0.25 0.30
8 Michael Bourn 0.33 0.33 0.26
9 Michael Brantley 0.24 0.24 0.28
10 Jay Bruce 0.30 0.30 0.26

... 推定量の散らばりを抑えたい

目次

1. 問題設定: シーズン打率を推定したい


2. 最尤推定で打率推定


3. 階層ベイズモデルで打率推定

ベイズ推定

  • 事前分布, 事後分布...

    • \(P(D)\) : 打席結果$D$が得られる確率
    • \(P(q_i)\) : $q_i$の事前分布.
    • \(P(q_i | D)\) : $q_i$の事後分布.


ベイズの公式

\[\begin{align*} \mathrm{P}(q_i ~ | ~ D) &= \frac{\mathrm{P}(D~|~q_i) \mathrm{P}(q_i)}{\mathrm{P}(D)} \\ & \propto \mathrm{P}(D~|~q_i) \mathrm{P}(q_i). \end{align*}\]


\(\mathrm{P}(q_i)\) ... "平均周りに縮小" させるような事前分布にしたい.

ベイズ推定

\(q_i\) 事後分布: \[\begin{align*} \mathrm{P}(q_i | D) \propto \mathrm{P}(D|q_i)\mathrm{P}(q_i).\\ \end{align*}\]

打率 \(q_i\) \(\to\) 打者スキル \(r_i\)

\[ q_i = \frac{1}{1 + \mathrm{e}^{-r_i}}.\]

\(r_i\) ... 打者スキル. 正規分布に従うと仮定.

\[ r_i \sim \mathcal{N}(a,b).\]

\(a\), \(b\): 事前分布のパラメータ... どうやって決める?

図は[久保, 2009]より

ベイズ推定

$r_i$の事後分布: \[\begin{align*} \mathrm{P}(r_i | D) &\propto \mathrm{P}(D|r_i)\mathrm{P}(r_i;a,b), \\ r_i &\sim \mathcal{N}(a,b). \end{align*}\]

事前分布のパラメータ\(a\), \(b\) ... どうやって決める?


2012 年のデータを使う? .. \(a = ~~~\), \(b = ~~~\) ?


2013 年のデータだけで何とかしたい

階層ベイズモデル

$r_i$の事後分布: \(P(D|r_i)\) \[\begin{align*} \mathrm{P}(q_i | D) \propto \mathrm{P}(D|q_i)\mathrm{P}(q_i).\\ \end{align*}\]

パラメータの変換

\[ q_i = \frac{1}{1 + \mathrm{e}^{-r_i}}.\]

\(r_i\) : 打者スキル \(\sim\) 正規分布

\[ r_i \sim \mathcal{N}(a,b).\]

無情報事前分布

\[\begin{align*} a &\sim \mathcal{N}(0, 100^2), \\ b &\sim \mathcal{U}(0, 100). \end{align*}\]

階層ベイズモデル

モデル まとめ

$q_i$の事後分布: \[\begin{align*} \mathrm{P}(q_i | D) &\propto \mathrm{P}(D|q_i)\mathrm{P}(q_i)\\ &= \mathrm{P}(D|r_i)\mathrm{P}(r_i)\\ &= \int \mathrm{P}(D|r_i)\mathrm{P}(r_i;a,b)\mathrm{P}(a)\mathrm{P}(b) ~ \mathrm{d}a\mathrm{d}b. \end{align*}\]

where

\[\begin{align*} & n_i \sim \mathcal{Binom}(N_i, q_i),\\ & q_i = \frac{1}{1 + \mathrm{e}^{-r_i}}, ~ r_i \sim \mathcal{N} (a,b),\\ & a \sim \mathcal{N}(0,100^2), ~ b \sim \mathcal{U}(0,100). \end{align*}\]

~

階層ベイズモデル

モデル まとめ

$q_i$の事後分布: \[\begin{align*} \mathrm{P}(q_i | D) &\propto \mathrm{P}(D|q_i)\mathrm{P}(q_i)\\ &= \mathrm{P}(D|r_i)\mathrm{P}(r_i)\\ &= \int \mathrm{P}(D|r_i)\mathrm{P}(r_i;a,b)\mathrm{P}(a)\mathrm{P}(b) ~ \mathrm{d}a\mathrm{d}b. \end{align*}\]

where

\[\begin{align*} & n_i \sim \mathcal{Binom}(N_i, q_i),\\ & q_i = \frac{1}{1 + \mathrm{e}^{-r_i}}, ~ r_i \sim \mathcal{N} (a,b),\\ & a \sim \mathcal{N}(0,100^2), ~ b \sim \mathcal{U}(0,100). \end{align*}\]

\(\mathrm{P}(q_i|D)\) : 分布を解析的に計算するのは難しい ... \(\to\) StanでMCMCサンプリング

結果

サンプリング \(\sim \mathrm{P}(q_1 | D) \propto \int \mathrm{P}(D|r_1)\mathrm{P}(r_1|a,b)\mathrm{P}(a)\mathrm{P}(b) \mathrm{d}a\mathrm{d}b.\)

plot of chunk unnamed-chunk-7

結果

サンプリング \(\sim \mathrm{P}(q_1 | D) \propto \int \mathrm{P}(D|r_1)\mathrm{P}(r_1|a,b)\mathrm{P}(a)\mathrm{P}(b) \mathrm{d}a\mathrm{d}b.\)

plot of chunk unnamed-chunk-8 --- &twocol

結果

結果

plot of chunk unnamed-chunk-11

plot of chunk unnamed-chunk-12

結果

plot of chunk unnamed-chunk-13

結果

\[\text{Mean Absolute Error} = \frac{1}{N}\sum\limits \left| q_i - q_{\text{season}}\right|\]

plot of chunk unnamed-chunk-14

plot of chunk unnamed-chunk-15

応用

  • 代打起用問題

    • 打者 A : 2安打 / 7打数
    • 打者 B : 20安打 / 70打数


\[\begin{align*} \text{Exploitation} \leftrightarrow \text{Exploration} \end{align*}\]


  • 多腕バンディット問題に?
    • Upper-Confidence-Bound

plot of chunk unnamed-chunk-16

応用

  • 代打起用問題

    • 打者 A : 2安打 / 7打数
    • 打者 B : 20安打 / 70打数


\[\begin{align*} \text{Exploitation} \leftrightarrow \text{Exploration} \end{align*}\]


  • 多腕バンディット問題に?
    • Upper-Credible-Bound??

plot of chunk unnamed-chunk-17

まとめ

~

  • 階層ベイズモデルで打率の推定をしました

    • 平均絶対誤差で精度を測ると, 最尤推定よりも良い
    • 平均周りに縮小しすぎに見える
    • モデルの改善がしたい
  • 改善のために...

    • スキル ~ 正規分布という仮定 ... そんなに悪くなさそう
    • 打席結果 ~ 二項分布という仮定 ... そんなに悪くなさそう
    • ハイパーパラメータの事前分布?
    • 助けて

参考文献

  • Data
    • The information used here was obtained free of charge from and is copyrighted by Retrosheet. Interested parties may contact Retrosheet at 20 Sunset Rd., Newark, DE 19711.
  • Hierarchical Bayesian Model
    • 最近のベイズモデルの進展と応用 (久保, 2009)