froglog

プログラミングや統計の話など

Mac OS X に PyStan をインストールした

このエントリについて

PyStan とは、MCMC サンプリング等を高速に実行するために C++ で実装されたプログラミング言語 StanPython インターフェースです。 このエントリでは PyStan を Mac のインストールしたときの手順を残します。

事前準備

ドキュメントによると PyStan は

に依存しているため、まずこれらをインストールします。 購入間もない Mac なので諸々一からインストールしていきました…。

Python3 をインストール

Mac に元々入っているのは 2.7 ですが、3系を使いたかったので homebrew でインストールしました。 Homebrewを使ってMacにPython3とかNumpyとかScipyとかをインストールする - 開発のヒホ を参考、というかそのままやっています。。。

brew update
brew install python3
pip3 install --upgrade setuptools
pip3 install --upgrade pip
brew linkapps

次のコマンドでバージョンが表示されれば OK。

python3 -V

エントリ投稿時点では 3.4 がインストールされますが、たぶん大丈夫でしょう。たぶん…

Cython をインストール

pip でインストールできます。

pip3 insall cython

Pythonimport してエラーが出なければ OK。

python3

>>> import cython

NumPy をインストール

同じく pip でインストールします。

pip3 install numpy

同様に import できることを確認。

PyStan インストール

依存物をインストールしたので PyStan 本体をインストールします。 これも pip でインストールできます。 pip 最高!

pip3 install pystan

import の確認もします。

PyStan を使ってみる

ドキュメントの Example 1: Eight Schools にコードサンプルが載っているのでそれをやってみます。 Eight Schools という問題設定で、元論文をすぐに見つけられなかったんですがおそらく Gelman, et al 2006 の「5 Application to the 8-schools example」あたりの話ではないかと。

テストの効果を検証するみたいな話のようですが、よくわからないままサンプルコードを実行してみます。

schools_code = """
data {
    int<lower=0> J; // number of schools
    real y[J]; // estimated treatment effects
    real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    real eta[J];
}
transformed parameters {
    real theta[J];
    for (j in 1:J)
    theta[j] <- mu + tau * eta[j];
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

fit = pystan.stan(model_code=schools_code, data=schools_dat,
                  iter=1000, chains=4)

C++ コードをコンパイル中みたいなメッセージが出た後にばーっとログが流れて演算終了。 結果の fit オブジェクトを出力してみます。

print(fit)
Inference for Stan model: anon_model_95013624776d537c3cd7cd4d641c30e0.
4 chains, each with iter=1000; warmup=500; thin=1;
post-warmup draws per chain=500, total post-warmup draws=2000.

           mean se_mean     sd   2.5%    25%    50%    75%  97.5%  n_eff   Rhat
mu         7.77    0.15   5.21   -2.2   4.27   7.75  11.28  18.56   1179    1.0
tau         6.7    0.17    5.8   0.19   2.38   5.42   9.16  21.54   1120    1.0
eta[0]     0.39    0.02   0.97   -1.7  -0.24   0.39   1.02   2.25   2565    1.0
eta[1]     0.02    0.02   0.86  -1.69  -0.52 2.5e-3   0.57   1.75   2737    1.0
eta[2]    -0.23    0.02   0.96  -2.13  -0.86  -0.26    0.4   1.64   2711    1.0
eta[3]    -0.01    0.02    0.9  -1.85  -0.62   0.02   0.58   1.79   2339    1.0
eta[4]    -0.32    0.02    0.9  -2.13  -0.89  -0.33   0.25   1.49   2469    1.0
eta[5]    -0.23    0.02    0.9  -1.95  -0.83  -0.26   0.36   1.66   2523    1.0
eta[6]     0.34    0.02   0.92  -1.67  -0.19   0.35   0.94   2.07   2247    1.0
eta[7]     0.08    0.02   0.91  -1.77  -0.51   0.11   0.69   1.86   2895    1.0
theta[0]  11.31     0.2   8.38  -2.93   5.98  10.19  15.62  30.79   1775    1.0
theta[1]    8.0    0.13   6.01  -3.71    4.2    7.8  11.84  20.21   2278    1.0
theta[2]    5.8    0.16   7.93 -11.48   1.61   6.18  10.57   20.5   2414    1.0
theta[3]   7.61    0.16    6.8  -6.54   3.24   7.63  11.94  20.88   1720    1.0
theta[4]   4.92    0.15   6.45  -8.94    1.0   5.47   9.22  16.19   1817    1.0
theta[5]    6.1    0.14   7.03  -9.72   2.26   6.35   10.6  19.23   2446    1.0
theta[6]  10.52    0.15    6.7   -1.4   5.96  10.14  14.41  24.89   2068    1.0
theta[7]   8.49    0.16   7.78  -6.79   3.74   8.28  12.97  25.29   2389    1.0
lp__      -4.99     0.1   2.78 -11.67  -6.61  -4.73  -2.97  -0.53    853    1.0

Samples were drawn using NUTS(diag_e) at Sat Aug 23 14:52:02 2014.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).

なんかできてるっぽい。

Python のグラフ描画モジュールである matplotlib がインストールされていれば次のコマンドでグラフ描画できるとのことなのでやってみます。

fit.plot().show()

グラフが表示されました。

f:id:soonraah:20140823145853p:plain

ええやんええやん。

おわりに

まだ Stan 自体よく理解できていませんが、とりあえずインストールして使えることは確認できました。 勉強しながら使っていきたいと思います。(小並感)