7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

確率変数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()

結果
image.png

7
2
0

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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?