3. はじめに
• このチャプターでは2系列の独立した二項確率
に関する推論をする
o 今までは1系列だけ扱ってた
• 階層モデル(Ch.9)への布石となる
o 多くのパラメータが出てくるので。
o 複数パラメータの扱いをここから始めよう
• ギブスサンプリングを初めて扱う
o 今まではBUGSという単語でのみ言及されてた
o 一般的なMetropolisアルゴリズムと違って、一回のジャン
プはひとつのパラメータ上で行われて、rejectされること
もない。出来れば非常に良い手法だが、いつも出来るわけ
ではない。
3
5. 補:もし頻度主義にたてば…
• フィッシャーの正確確率検定や、χ2検定が使われる
• p値が95%信頼区間内なので有意差は認められない
o つまり薬が効いたとはいえない
Rの例
fisher.test(matrix(c(12,38,5,45),nrow=2))
Fisher's Exact Test for Count Data
data: matrix(c(12, 38, 5, 45), nrow = 2)
p-value = 0.1084
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.8300494 11.1360458
sample estimates:
odds ratio
2.813081
5
9. 二系列の事前確率・尤度・事後確率(3)endence of sampled flips, the probability of D is just the product of the Be
bution functions for the individual flips:
p(D|θ1,θ2) =
y1i∈D1
p(y1i|θ1,θ2)
yj∈D2
p(yj|θ1,θ2)
=
y1i∈D1
θy1i
1
(1 − θ1)(1−y1i)
y2j∈D2
θ
y2j
2
(1 − θ2)(1−y2j)
= θz1
1 (1 − θ1)(N1−z1)
θz2
2 (1 − θ2)(N2−z2)
he posterior distribution of our beliefs about the underlying proportions is deri
ual way by applying Bayes’ rule, but now the functions involve two parameter
p(θ1,θ2|D) = p(D|θ1,θ2)p(θ1,θ2) /p(D)
= p(D|θ1,θ2)p(θ1,θ2) dθ1dθ2 p(D|θ1,θ2)p(θ1,θ2)
mber, as always in the expression of Bayes’ rule, the θi’s in left side of the eq
ution functions for the individual flips:
p(D|θ1,θ2) =
y1i∈D1
p(y1i|θ1,θ2)
yj∈D2
p(yj|θ1,θ2)
=
y1i∈D1
θy1i
1 (1 − θ1)(1−y1i)
y2j∈D2
θ
y2j
2 (1 − θ2)(1−y2j)
= θz1
1
(1 − θ1)(N1−z1)
θz2
2
(1 − θ2)(N2−z2)
e posterior distribution of our beliefs about the underlying proportions is deri
ual way by applying Bayes’ rule, but now the functions involve two parameters
p(θ1,θ2|D) = p(D|θ1,θ2)p(θ1,θ2) /p(D)
= p(D|θ1,θ2)p(θ1,θ2) dθ1dθ2 p(D|θ1,θ2)p(θ1,θ2)
mber, as always in the expression of Bayes’ rule, the θi’s in left side of the eq
the numerator of the right side are referring to specific values of θi, but the θi’s
al in the denominator range over all possible values of θi.
ベイズの法則より
左辺や右辺分子のθにはある具体的な値が入っているが、右辺分母は
あらゆるθ1,θ2についての積分となっている
(8.1)
(8.2)
2
9
11. 事後確率の解析的な分析(2)
1 − θ)(b−1)
/ B(a,b), where B(a,b) is the beta normalizing function, which by
is B(a,b) =
1
0
dθθ(a−1)(1 − θ)(b−1). We assume a beta(θ1|a1,b1) prior on θ1, a
endent beta(θ2|a2,b2) prior on θ2. Then
p(θ1,θ2|D) = p(D|θ1,θ2)p(θ1,θ2)/ p(D)
=
θ(z1+a1−1)
1 (1 − θ1)(N1−z1+b1−1)
θ(z2+a2−1)
2 (1 − θ2)(N2−z2+b2−1)
p(D)B(a1,b1)B(a2,b2)
ow that the left side of Equation 8.3 must be a probability density function, and w
e numerator of the right side has the form of a product of beta distributions, na
(8.3)
分母は分子の正規化関数なのだから、分子をθ1, θ2について積分すると
theta1
theta2
p(t1,t2|D)
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.6
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.6
shows the 95% high
est density region as
darker contour line.
code that generated th
graph is in Section 8.8
(Ber nTwoGr i d. R).
(θ1|z1+a1,N1−z1+b1) times beta(θ2|z2+a2,N2−z2+b2). Therefore the denominator o
ation 8.3 must be the corresponding normalizer for the product of beta distributions:
p(D)B(a1,b1)B(a2,b2) = B(z1+a1,N1−z1+b1) B(z2+a2,N2−z2+b2) (8.4
rearranging terms, a convenient consequence of Equation 8.4 is that
p(D) =
B(z1+a1,N1−z1+b1) B(z2+a2,N2−z2+b2)
B(a1,b1)B(a2,b2)
(8.5
s is exactly analogous to the result we found previously for one parameter, in Equ
5.10 (p. 73).
(8.4)
theta1
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40
darker contour line.
code that generated
graph is in Section 8
(Ber nTwoGr i d. R).
1, N1−z1+b1) times beta(θ2|z2+a2, N2−z2+b2). Therefore the denominato
must be the corresponding normalizer for the product of beta distributions
(D)B(a1,b1)B(a2,b2) = B(z1+a1, N1−z1+b1) B(z2+a2, N2−z2+b2) (8
ng terms, a convenient consequence of Equation 8.4 is that
p(D) =
B(z1+a1, N1−z1+b1) B(z2+a2, N2−z2+b2)
B(a1,b1)B(a2,b2)
(8
tly analogous to the result we found previously for one parameter, in Eq
73).
整理すると
(8.5)
11
13. 事後確率の解析的な分析(4).2. THE POSTERIOR VIA EXACT FORMAL ANALYSIS 131
theta1
theta2
p(t1,t2)
Prior
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
theta1
theta2
p(D|t1,t2)
Likelihood
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
z1=5,N1=7,z2=2,N2=7
Posterior
1.0
p(D)=5.74e−05
95% HD region
1.0
Figure 8.1: Bayesian
updating of independent
beta(θ|3,3) priors. Left
panels show perspective
surface plots; right panels
show contour plots of the
same distribution. The
posterior contour plot
(lower right) includes
the value of p(D), and
theta1
theta2
p(t1,t2)
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.
theta1
theta2
p(D|t1,t2)
Likelihood
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
z1=5,N1=7,z2=2,N2=7
theta1
theta2
p(t1,t2|D)
Posterior
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
p(D)=5.74e−05
95% HD region
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
beta(θ1|z1 +a1, N1−z1+b1) times beta(θ2|z2+a2, N2−z2+b2). The
Equation 8.3 must be the corresponding normalizer for the produ
theta1
theta2
p(t1,t2)
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.
theta1
theta2
p(D|t1,t2)
Likelihood
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
z1=5,N1=7,z2=2,N2=7
theta1
theta2
p(t1,t2|D)
Posterior
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
p(D)=5.74e−05
95% HD region
q1
q2
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
Figure 8.1: Bayesian
updating of independent
beta(θ|3,3) priors. Left
panels show perspective
surface plots; right panels
show contour plots of the
same distribution. The
posterior contour plot
(lower right) includes
the value of p(D), and
shows the 95% high-
est density region as a
darker contour line. R
code that generated this
graph is in Section 8.8.1
(Ber nTwoGr i d. R).
beta(θ1|z1 +a1, N1 −z1+b1) times beta(θ2|z2+a2, N2−z2+b2). Therefore the denominator of
事前確率:beta(θ|3,3)
0.5が平均になって広がっている
尤度:どのθiならデータがもっとも
適合するか。ピークはデータでの表の
割合。
事後確率:黒い枠内が95%の領域
13
17. Metropolis Algorithm
• Metropolis Algorithmの手順
o ランダム・ウォークによってジャンプ先を決める
• ある確率分布(提案分布;proposal distribution)に従っ
たランダムウォーク。
• 二次元なので、二次元の標準正規分布を使うことにす
る。文字通りの(3次元で思い浮かべる)ベル・カーブ。
o 分布の平均はジャンプ前の点とする
o ジャンプ先を受け入れるかどうかを下の確率で決める
. The proposed jump is randomly generated from a proposal distribution, from
sume it is easy to generate values. For our present purposes, the proposal distri
bivariate normal. You can visualize a bivariate normal distribution by imagi
imensional normal (as in Figure 3.3, p. 31), sticking a pin down vertically th
ak, and spinning it around the pin, to form a bell-shaped distribution. The us
ate normal proposal distribution implies that the proposed jumps will usually b
urrent position, but more distant jumps could be proposed with lesser probabilit
he proposed jump is definitely accepted if the posterior is more dense at the pro
on than at the current position, and the proposed jump is probabilistically acce
osterior is less dense at the proposed position than at the current position. The
bility of accepting the proposed jump, i.e., the probability of moving, is
pmove = min
P(θproposed)
P(θcurrent)
,1
e P(θ) is the (unnormalized) posterior probability at θ. (Equation 8.7 repeats
7.1 from p. 103.) This probability of moving is turned into an actual discrete
ve or stay by generating a random number from the uniform density on [0,1]:
ated number lies between 0 and pmove, then the move is made, otherwise the c
o P(θ)は事後確率;正規化されていなくて良い
o 今よりもいい確率なら必ず受容され、低い場合はその割合
の確率で受容される。却下された場合はまた上に戻る
17
18. Metropolis Algorithmの結果
8.4. THE POSTERIOR VIA MARKOV CHAIN MONTE CARLO 13
0.0 0.2 0.4 0.6 0.8 1.0
0.00.20.40.60.81.0
q1
q2
M=0.615,0.398; Npro=1000,
Nacc
Npro
=0.39
p(D) = 5.63e−05
Figure 8.3: Metropolis algorithm
applied to the prior and likelihoo
shown in Figure 8.1, p. 131. Com
pare this scatter plot with the lowe
right contour plot of Figure 8.1
Npro is the number of propose
jumps (after burn in), and Nac
is the number of accepted propos
als. R code is in Section 8.8.
(Ber nTwoMet r opol i s. R).
8.4.1 Metropolis algorithm
先ほどと同じ例(事前確率:beta)
大体Posteriorが近似されている
気がする。
あっちこっちに飛ぶのは提案分布
が二次元正規分布のせい。
例によってburn-in期間(ランダム
ウォークした最初の期間)を除いて
いる
Exerciseで提案分布を広くしたり
狭くしたりしましょう(各自)
18
19. MetropolisからGibbsへ
• Metropolisアルゴリズムは一般的かつ広く適応
可能。
• 問題点
o 提案分布を事後確率分布によって適切にチューニングしな
いとうまくいかない
• 15ページ目のような複雑な事後分布の場合、グループ
間を行き来できないでとどまってしまうかもしれない
o 提案分布が広すぎたり狭すぎたりすると、多くのジャンプ
が却下されたり一部にとどまり続けてしまう可能性も高く
なる
• そこで、チューニングが要らない、他の方法は
ないだろうか
o →Gibbs Sampling
19
20. Gibbs Sampling
• Gibbs Samplingで何が出来るか
o p(θ1, θ2, θ3|D)という同時事後確率のサンプルを生成でき
る
• それが出来る条件
o p(θ1|θ2, θ3,D) ,p(θ2|θ1, θ3,D) ,p(θ3|θ1, θ2,D)が分かる
場合
o かつそれらのサンプルが生成可能な場合
• つまり
o 適応できる場合が限定されている
o しかし出来る場合にはMetropolisアルゴリズムよりも効率
的かつ信頼出来る結果が得られる
20
21. Gibbs Samplingの手順
• Metropolis Algorithmの一種だがステップのと
り方が違う&却下されない
• 一回のジャンプは必ず一つのパラメータ(θi)上で
行われ、それ以外(θi)は(前の値に)固定されて
いる。普通θ1→θ2→θ3→…のように軸を順に変
えてジャンプする
• ジャンプ先はp(θi|{θj}i≠j,D)に従う
o MetropolisアルゴリズムではQ(xt+1|xt)=提案分布を用い
ていた。前の例だとxtを中心とした二次元の正規分布。
• サンプルが却下されることがない
o なぜなら提案分布が事後確率を反映しているため
o 細かい証明はGelman, Carlin, Stern, Rubin(2004 p293)
21
22. Gibbs Samplingの適用
• 先ほどの例を取ってみる。Gibbs Samplingをす
るにはまず条件付き事後確率を求める
tribution is again a product of beta distributions, as was derived in Equation 8
s joint posterior is easily dealt with directly, and so there is no real need to app
ing, but we will go ahead and do Gibbs sampling of this posterior distributi
of illustration and comparison with other methods.
mplish Gibbs sampling, we must determine the conditional posterior distrib
parameter. By definition of conditional probability,
p(θ1|θ2, D) = p(θ1,θ2|D)/ p(θ2|D)
= p(θ1,θ2|D) dθ1 p(θ1,θ2|D)
ent application, the joint posterior is a product of beta distributions as in Equ
erefore
1|θ2, D) = p(θ1,θ2|D) dθ1 p(θ1,θ2|D)
=
beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
dθ1 beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
ベータ分布としているので
osterior distribution is again a product of beta distributions, as was derived in Equation 8
p. 130). This joint posterior is easily dealt with directly, and so there is no real need to app
ibbs sampling, but we will go ahead and do Gibbs sampling of this posterior distributio
or purposes of illustration and comparison with other methods.
To accomplish Gibbs sampling, we must determine the conditional posterior distrib
on for each parameter. By definition of conditional probability,
p(θ1|θ2, D) = p(θ1,θ2|D)/ p(θ2|D)
= p(θ1,θ2|D) dθ1 p(θ1,θ2|D)
or our current application, the joint posterior is a product of beta distributions as in Equ
on 8.3. Therefore
p(θ1|θ2, D) = p(θ1,θ2|D) dθ1 p(θ1,θ2|D)
=
beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
dθ1 beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
=
beta(θ1|z1+a1, N1−z1+b1) beta(θ2|z2+a2, N2−z2+b2)
beta(θ2|z2+a2, N2−z2+b2)
= beta(θ1|z1+a1, N1−z1+b1) (8.(8.8)
22
23. Gibbs Samplingの適用
• 8.8式はある意味明らか
o 事後確率は独立したベータ分布の積なので、
p(θ1|θ2,D)=p(θ1|D)である
• 条件付き事後確率がもとまったら、次はそこか
らサンプリングできるかを考えなければならな
い
o この場合はベータ分布なので直接サンプリングできる。
o 直接サンプリングできない場合は別の方法でサンプリング
する
• これで計算してみると…
23