-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpybayes_mcmc_poisson.py
More file actions
68 lines (68 loc) · 2.69 KB
/
pybayes_mcmc_poisson.py
File metadata and controls
68 lines (68 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
#%% NumPyの読み込み
import numpy as np
# SciPyのstatsモジュールの読み込み
import scipy.stats as st
# PyMCの読み込み
import pymc as pm
# MatplotlibのPyplotモジュールの読み込み
import matplotlib.pyplot as plt
# 日本語フォントの設定
from matplotlib.font_manager import FontProperties
import sys
if sys.platform.startswith('win'):
FontPath = 'C:\\Windows\\Fonts\\meiryo.ttc'
elif sys.platform.startswith('darwin'):
FontPath = '/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'
elif sys.platform.startswith('linux'):
FontPath = '/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf'
else:
print('このPythonコードが対応していないOSを使用しています.')
sys.exit()
jpfont = FontProperties(fname=FontPath)
#%% ポアソン回帰モデルからのデータ生成
n = 500
np.random.seed(99)
x1 = st.uniform.rvs(loc=-np.sqrt(3.0), scale=2.0*np.sqrt(3.0), size=n)
x2 = st.uniform.rvs(loc=-np.sqrt(3.0), scale=2.0*np.sqrt(3.0), size=n)
lam = np.exp(0.5*x1 - 0.5*x2)
y = st.poisson.rvs(lam)
X = np.stack((np.ones(n), x1, x2), axis=1)
#%% ポアソン回帰モデルの係数の事後分布の設定
n, k = X.shape
b0 = np.zeros(k)
A0 = 0.01 * np.eye(k)
poisson_regression_model = pm.Model()
with poisson_regression_model:
b = pm.MvNormal('b', mu=b0, tau=A0, shape=k)
idx = pm.math.dot(X, b)
likelihood = pm.Poisson('y', mu=pm.math.exp(idx), observed=y)
#%% 事後分布からのサンプリング
n_draws = 5000
n_chains = 4
n_tune = 1000
with poisson_regression_model:
trace = pm.sample(draws=n_draws, chains=n_chains, tune=n_tune,
random_seed=123)
print(pm.summary(trace))
#%% 事後分布のグラフの作成
fig, ax = plt.subplots(k, 2, num=1, figsize=(8, 1.5*k), facecolor='w')
for index in range(k):
mc_trace = trace.posterior['b'].values[:, :, index].flatten()
x_min = mc_trace.min() - 0.2 * np.abs(mc_trace.min())
x_max = mc_trace.max() + 0.2 * np.abs(mc_trace.max())
x = np.linspace(x_min, x_max, 250)
posterior = st.gaussian_kde(mc_trace).evaluate(x)
ax[index, 0].plot(mc_trace, 'k-', linewidth=0.1)
ax[index, 0].set_xlim(1, n_draws*n_chains)
ax[index, 0].set_ylabel('$\\beta_{:d}$'.format(index+1),
fontproperties=jpfont)
ax[index, 1].plot(x, posterior, 'k-')
ax[index, 1].set_xlim(x_min, x_max)
ax[index, 1].set_ylim(0, 1.1*posterior.max())
ax[index, 1].set_ylabel('確率密度', fontproperties=jpfont)
ax[k-1, 0].set_xlabel('乱数系列', fontproperties=jpfont)
ax[k-1, 1].set_xlabel('周辺事後分布', fontproperties=jpfont)
plt.tight_layout()
plt.savefig('pybayes_fig_mcmc_poisson.png', dpi=300)
plt.show()