二項分布のパラメータ(ベータ分布)の事後分布を尤度関数で求める


確率変数xが二項分布に従うものとする。
そしてその二項分布は、パラメータuを持ち、uはベータ分布に従うとする。

N回の試行のうち、x=1の事象がm回、x=0の事象がN-m回発生したとする。

N,mとベータ分布のa,bの値を以下のように定めたとき、
事前分布、尤度関数、事後分布を求め、更新の様子を可視化してみた。

import math
import numpy as np
import matplotlib.pyplot as plt

N = 1
m = 1
a = 2
b = 2
x = np.linspace(0, 1, 100)

# 事前分布
y_prior = (math.gamma(a+b) * (x ** (a-1)) * ((1-x) ** (b-1))) / (math.gamma(a) * math.gamma(b))
# 尤度関数
y_likelihood_function = (math.factorial(N) * (x ** m) * ((1-x) ** (N-m))) / (math.factorial(N-m) * math.factorial(m))
# 事後分布
y_posterior = (math.gamma(N+a+b) * (x ** (m+a-1)) * ((1-x) ** (N-m+b-1))) / (math.gamma(m+a) * math.gamma(N-m+b))

plt.plot(x,y_prior, label='prior')
plt.plot(x,y_likelihood_function, label='lilelihood function')
plt.plot(x,y_posterior, label='posterior')
plt.legend()
plt.show()

結果