際際滷

際際滷Share a Scribd company logo
1
PyMCPyMCによる_楕議プログラミングとによる_楕議プログラミングとMCMCMCMC
ととTheanoTheano
2014/7/12 BUGS,stan茶氏 #3
@xiangze750
2
Agenda
¢ Pythonでのベイズモデリング
¢ PyMCの聞い圭
¢ ^Probabilistic Programming and Bayesian Methods for
Hackers ̄
¢ 歌孚すべきPyMCブログ ^While My MCMC Gently Samples ^
¢ Theano, GPUとのB亊
¢ Appendix: Theano, HMC
3
Pythonでのベイズモデリング
¢ Pystan
¢ PyMC
4
PyMCの旋泣
¢ Installがg
¢ pythonでモデリング、g佩、辛晒ができる。
¢ c++での互堀晒 (Theano)
C HMC,NUTS
C GPUの聞喘
5
Install
¢ #PyMC 2.3
pip install pymc
¢ #PyMC 3(_k嶄)
pip install git+https://github.com/pymc-devs/pymc
¢ うまくいかない栽
git clone https://github.com/pymc-devs/pymc
cd pymc
python setup.py install
6
Documents
¢ User's guide
C http://pymc-devs.github.io/pymc/
¢ Tutorial
C https://github.com/fonnesbeck/pymc_tutorial
¢ Probabilistic Programming and Bayesian Methods for
Hackers
C http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-
Programming-and-Bayesian-Methods-for-Hackers/blob/master/Prologue/
Prologue.ipynb
7
PyMCの聞い圭
¢ 児云議猟隈
¢ モデルB
C _楕篳(蛍下)
C Q協議篳 @pm.deterministic
C Qy篳
¢ Sampling
¢ Traceplot, histogram
8
Probabilistic Programming
and Bayesian Methods for Hackers
¢ 1. Introduction
¢ 2. MorePyMC
¢ 3. MCMC
¢ 4. The Greatest Theorem Never Told (The Law of Large
Numbers)
¢ 5. Loss Functions
¢ 6. Priorities
¢ 7. Bayesian Machine Learning
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesi
an-Methods-for-Hackers/tree/master/
Ipython notebook、pymcを聞喘したベイズの縮親
9
PyMCの聞い圭: 篳
#並念蛍下
lambda_1 = pm.Exponential("lambda_1", 1)
lambda_2 = pm.Exponential("lambda_2", 1)
tau = pm.DiscreteUniform("tau", lower=0, upper=10)
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
Stanの parameter block
に
¢
_楕篳
10
PyMCの聞い圭: 篳
#@pm.stochasticを喘いて鏡徭蛍下を協xすることも辛嬬
@pm.stochastic(dtype=int)
def switchpoint(value=1900, t_l=1851, t_h=1962):
if value > t_h or value < t_l:
return -np.inf # Invalid values
else:
return -np.log(t_h - t_l + 1)# Uniform log-
likelihood
¢
_楕篳
http://pymc-devs.github.io/pymc/modelbuilding.html#the-stochastic-class
11
PyMCの聞い圭: 篳
¢ _楕蛍下の匯E
http://pymc-devs.github.io/pymc/distributions.html#chap-distributions
12
PyMCの聞い圭: 篳
#v方協xの念に@pm.deterministicを原ける
n_data_points = 5 # in CH1 we had ~70 data points
@pm.deterministic
def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
out = np.zeros(n_data_points)
out[:tau] = lambda_1 # lambda before tau is lambda1
out[tau:] = lambda_2 # lambda after tau is lambda2
return out
lambdaの、tauで俳り紋える。返Aき議峰
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
Stanのtransformed
parameter blockに
¢
Q協議篳
13
PyMCの聞い圭: 篳
#observed=True
data = np.array([10, 25, 15, 20, 35])
obs = pm.Poisson("obs", lambda_, value=data,
observed=True)
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
Stanのdata blockに
¢
Qy篳
14
PyMCの聞い圭: モデルB
#協xした篳のリストを局す
model = pm.Model([obs, lambda_,
lambda_1, lambda_2, tau])
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
Stanのmodel blockに
¢
モデル
15
PyMCの聞い圭: sampling
#MCMCのための兜豚ヘ洞
model = pm.Model( [p, assignment, taus, centers ] )
map_ = pm.MAP( model )
map_.fit() #stores the fitted variables'values in
foo.value
#MCMC
mcmc = pm.MCMC( model )
mcmc.sample( 100000, 50000 )
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesi
an-Methods-for-Hackers/blob/master/Chapter3_MCMC/IntroMCMC.ipynb
16
PyMCの聞い圭: histogram, random
samples = [lambda_1.random() for i in range(20000)]
plt.hist(samples, bins=70, normed=True,
histtype="stepfilled")
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
17
PyMCの聞い圭: traceplot
with pm.Model() as model:
x = pm.Normal('x', mu=0., sd=1)
y = pm.Normal('y', mu=pm.exp(x), sd=2., shape=(ndims, 1)) # here,
shape is telling us it's a vector rather than a scalar.
z = pm.Normal('z', mu=x + y, sd=.75, observed=zdata) # shape is
inferred from zdata
with model:
  start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(3000, step, start)
pm.traceplot(trace)
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
18
PyMCの聞い圭: traceplot
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
19
その麿の箭
¢ Bayesian A/B testing
¢ Cheating among students
¢ Challenger Space Shuttle Disaster
http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayes
ian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/MorePyMC.ipynb
20
廣朕すべきPyMCブログ
While My MCMC Gently Samples
http://twiecki.github.io/
21
廣朕すべきpymcブログ
Repos
https://github.com/twiecki
¢ mpi4py_map
C provides a simple map() interface to mpi4py that
allows easy parallelization of function evaluations
over sequential input.
¢ CythonGSL
C Cython interface for the GNU Scientific Library
(GSL).
22
匯違晒侘モデル(glm)
¢ code(i)
http://twiecki.github.io/blog/2013/08/27/bayesian-glms-2/
with pm.Model() as model_robust:
family = pm.glm.families.T()
pm.glm.glm('y ~ x', data, family=family)
trace_robust = pm.sample(2000, pm.NUTS(), progressbar=False)
plt.figure(figsize=(5, 5))
plt.plot(x_out, y_out, 'x')
pm.glm.plot_posterior_predictive(trace_robust,
label='posterior predictive
regression lines')
plt.plot(x, true_regression_line,
label='true regression line', lw=3., c='y')
plt.legend();
23
箭: A啜直侘モデル
社の嶄のラドン盒
¢ 85 countries
¢ 2 to 116 measurements
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
24
箭: A啜直侘モデル
¢ Pooling of measurements
C 光仇泣で慌宥のパラメ`タθ
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
25
箭: A啜直侘モデル
¢ Unpooled measurements
C パラメ`タθが仇泣c阿邦なる。
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
26
箭: A啜直侘モデル
¢ Partial pooling: Hierarchical Regression
C ハイパ`パラメ`タμ, σ
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
27
箭: A啜直侘モデル code
¢ code(i)
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
with pm.Model() as hierarchical_model:
# ハイパ`パラメ`タ(峠譲と蛍柊)
mu_a = pm.Normal('mu_alpha', mu=0., sd=100**2)
sigma_a = pm.Uniform('sigma_alpha', lower=0, upper=100)
mu_b = pm.Normal('mu_beta', mu=0., sd=100**2)
sigma_b = pm.Uniform('sigma_beta', lower=0, upper=100)
#w侭ごとのAきと俳頭, 屎キ峅
a = pm.Normal('alpha', mu=mu_a, sd=sigma_a, shape=n_counties)
b = pm.Normal('beta', mu=mu_b, sd=sigma_b, shape=n_counties)
# Model error
eps = pm.Uniform('eps', lower=0, upper=100)
radon_est = a[county_idx] + b[county_idx] * data.floor.values
#喩業
radon_like = pm.Normal('radon_like', mu=radon_est, sd=eps,
observed=data.log_radon)
28
箭: A啜直侘モデル code
¢ code(i)
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
#modelのg佩
with hierarchical_model:
start = pm.find_MAP()
step = pm.NUTS(scaling=start)
hierarchical_trace = pm.sample(2000, step, start=start,
progressbar=False)
29
箭: A啜直侘モデル
¢ CASSでは匯カ侭でしかy協していない
C
http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/
C
30
箭: A啜直侘モデル
¢ Root Mean Square Deviation
individual/non-hierarchical model 0.13
hierarchical model 0.08
31
Theano, GPUとのB亊
PyMC3 https://github.com/pymc-devs/pymc
瀏鉾鉾鉾鉾鉾鉾鉾鉾鉾烹
  _k嶄。  
Y^Y^Y^Y^Y^Y^Y^Y^Y
32
Appendix: Theanoについて
¢ PythonでDeep learningvSのアルゴリズムg廾が竃栖る
ライブラリ(http://deeplearning.net/software/theano/)
C (Stacked) Auto Denoising Encoder, RBMなどのg廾巷_
C 協xした塀を催裏蛍で簧里気察▲灰鵐僖ぅ襪垢誂諒
C GPU(Nvidia CUDA)で麻をK双晒辛嬬
C 坪何でgcc, nvcc(CUDAのコンパイラ)を柵んでいる。
C HMCのサンプルg廾(http://deeplearning.net/tutorial/hmc.html)
33
Appendix: HMC (Hamilton Monte-Carlo)
¢ Hamilton (Hyblid) Monte-Carlo
C ^\啻拭韻肇魯潺襯肇縫▲Hを協xして蛍下v方が
弌さい何蛍での卞啖を寄きくし、紳糞弔縫汽鵐廛
ング。
C e蛍が1w卞あたりの麻楚が宥械のMCMCより寄き
くなる。
http://xiangze.hatenablog.com/entry/2014/06/21/234930
34
Appendix: HMC (Hamilton Monte-Carlo)
歌深:
¢ BDA 12.4 ^Hamiltonian Monte Carlo ̄のメモ
C http://ito-hi.blog.so-net.ne.jp/2014-06-12
¢ はじめてのMCMC (ハイブリッド?モンテカルロ)
C http://tatsyblog.wordpress.com/2014/03/22/%E3%81%AF%E3%81%98%
E3%82%81%E3%81%A6%E3%81%AEmcmc-%E3%83%8F%E3%82%A4%E3%83%96%E3
%83%AA%E3%83%83%E3%83%89%E3%83%BB%E3%83%A2%E3%83%B3%E3%83%86%
E3%82%AB%E3%83%AB%E3%83%AD/
¢ Theanog廾の盾h
C http://nbviewer.ipython.org/gist/xiangze/c2719235434bee796288

More Related Content

PyMC mcmc