Stanï¼é ããã«ã³ãã¢ãã«â
ã¯ããã«
ä»åã¯é ããã«ã³ãã¢ãã«ã
Stan
ã§å®è£
ãããé ããã«ã³ãã¢ãã«èªä½ã¯ä»¥åã«æ¸ããã
kento1109.hatenablog.com
ä»åã¯æ師ããã¢ãã«ãèããã
æ師ãããé ãç¶æ
ããæ¢ç¥ã®ã¢ãã«ã
ååã®ä¾ã§èããã¨ããæ´ãâé¨ããªã©ã®é·ç§»ç¶æ
ãä¸ãããã¦ããã¢ãã«ã
ãã®ç¶æ
ããããé·ç§»è¡åãã¨ãåºåè¡åããæ¨å®ããã
ã¢ãã«
data { int<lower=1> K; // ã«ãã´ãªã¼ã®æ° int<lower=1> V; // åèª(word)ã®æ° int<lower=0> T; // æç¹ã®æ° int<lower=1,upper=V> w[T]; // åèª(word) int<lower=1,upper=K> z[T]; // ã«ãã´ãªã¼ vector<lower=0>[K] alpha; // æ¨ç§»(transit)確çã®äºå確ç vector<lower=0>[V] beta; // åèªvãåºå(emit)ãã確çã®äºå確ç } parameters { simplex[K] theta[K]; // æ¨ç§»(transit)確ç simplex[V] phi[K]; // åèªvãåºå(emit)ãã確ç } model { for (k in 1:K) theta[k] ~ dirichlet(alpha); for (k in 1:K) phi[k] ~ dirichlet(beta); for (t in 1:T) w[t] ~ categorical(phi[z[t]]); for (t in 2:T) z[t] ~ categorical(theta[z[t - 1]]); }
ããã¯ã
Stan モデリング言語: ユーザーガイド・リファレンスマニュアル
ã®ã10.6. é ããã«ã³ãã¢ãã«ãã®ã¾ãã¾ã
ãé·ç§»è¡åããåºåè¡åã«è©²å½ããã
å®è¡
ãã¼ã¿ã¯ä»¥ä¸ã®ããã«ä½æããã
emission <- c(1,2,3,3,3,2,2,1,2) hidden_state <- c(1,1,2,2,2,2,1,1,1) stan.dat <- list(K=2, V=3, T=9, w=emission, z=hidden_state, alpha=c(1.0,1.0), beta=c(1.0,1.0,1.0))
ãã¼ã¿ãååã®ä¾ãç¨ãã¦è¡¨ã«ããã¨ãããªæãã
day | emission | hidden_state |
1 | walk | Sunny |
2 | shop | Sunny |
3 | clean | Rainy |
4 | clean | Rainy |
5 | clean | Rainy |
6 | shop | Rainy |
7 | shop | Sunny |
8 | walk | Sunny |
9 | shop | Sunny |
ããã
stan.fit <- stan("supervised_hmm.stan", data=stan.dat)
çµæ
以ä¸ã®éããåæå¤å®ã¯åé¡ãªãããã
summary(stan.fit) $summary mean se_mean sd 2.5% 25% theta[1,1] 0.6687044 0.002852309 0.1803958 0.285459555 0.54520998 theta[1,2] 0.3312956 0.002852309 0.1803958 0.050861661 0.18992343 theta[2,1] 0.3390158 0.002919518 0.1846466 0.052310405 0.19037473 theta[2,2] 0.6609842 0.002919518 0.1846466 0.272114157 0.53280306 phi[1,1] 0.3739677 0.002550008 0.1612767 0.102355908 0.24580265 phi[1,2] 0.4998130 0.002675016 0.1691828 0.179900530 0.37727482 phi[1,3] 0.1262193 0.001702130 0.1076522 0.003180911 0.04258686 phi[2,1] 0.1434164 0.001966619 0.1243799 0.004601771 0.04711737 phi[2,2] 0.2863562 0.002532753 0.1601854 0.044368247 0.15728610 phi[2,3] 0.5702274 0.002743551 0.1735174 0.223375958 0.44862691 50% 75% 97.5% n_eff Rhat theta[1,1] 0.68911775 0.8100766 0.9491383 4000.000 0.9997148 theta[1,2] 0.31088225 0.4547900 0.7145404 4000.000 0.9997148 theta[2,1] 0.31960050 0.4671969 0.7278858 4000.000 0.9999138 theta[2,2] 0.68039950 0.8096253 0.9476896 4000.000 0.9999138 phi[1,1] 0.36392534 0.4872123 0.7034251 4000.000 0.9997530 phi[1,2] 0.50182133 0.6216414 0.8188262 4000.000 1.0001396 phi[1,3] 0.09887729 0.1803597 0.3941908 4000.000 0.9998056 phi[2,1] 0.10823715 0.2085361 0.4664891 4000.000 0.9999784 phi[2,2] 0.26591206 0.3916398 0.6373429 4000.000 0.9993131 phi[2,3] 0.57647391 0.6975200 0.8830145 4000.000 0.9997687
ãé·ç§»è¡åãã¨ãåºåè¡åãã表ã«ãã¦ã¿ãã
- é·ç§»è¡åï¼ï¼
Sunny | Rainy | |
Sunny | 0.67 | 0.33 |
Rainy | 0.34 | 0.66 |
âç´66ï¼ ã§æ¬¡ã®æ¥ãåã天æ°ã§ãããã¨ã示ãã¦ããã
- åºåè¡åï¼ï¼
walk | shop | clean | |
Sunny | 0.37 | 0.50 | 0.13 |
Rainy | 0.14 | 0.29 | 0.57 |
âæ´ãã®æ¥ã¯ãæé¤ãããã確çã¯ä½ããé¨ã®æ¥ã¯ãæ£æ©ãããã確çã¯ä½ããã¨ã示ãã¦ããã
ãã®ä»
è¤æ°äººããã¼ã¿ã¨ããå ´åã以ä¸ã®ãããªè¡åãç¨æããã°ããã
(emission <- rbind(c(1,1,3,1,2,1,2,1,3,1), c(2,2,3,2,2,1,1,2,2,1), c(1,2,2,2,1,1,1,1,1,2))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 3 1 2 1 2 1 3 1 [2,] 2 2 3 2 2 1 1 2 2 1 [3,] 1 2 2 2 1 1 1 1 1 2
data { int<lower=1> K; // ã«ãã´ãªã¼ã®æ° int<lower=1> V; // åèª(word)ã®æ° int<lower=0> T; // æç¹ã®æ° int<lower=1> N; // äººæ° int<lower=1,upper=V> w[N,T]; // åèª(word) int<lower=1,upper=K> z[T]; // ã«ãã´ãªã¼ vector<lower=0>[K] alpha; // æ¨ç§»(transit)確çã®äºå確ç vector<lower=0>[V] beta; // åèªvãåºå(emit)ãã確çã®äºå確ç } parameters { simplex[K] theta[K]; // æ¨ç§»(transit)確ç simplex[V] phi[K]; // åèªvãåºå(emit)ãã確ç } model { for (k in 1:K) theta[k] ~ dirichlet(alpha); for (k in 1:K) phi[k] ~ dirichlet(beta); for (n in 1:N){ for (t in 1:T) w[n,t] ~ categorical(phi[z[t]]); } for (t in 2:T) z[t] ~ categorical(theta[z[t - 1]]); }
ãããã«
ä»åã¯é·ç§»è¡åãåºåè¡åã®æ¨å®ãè¡ã£ãã
ãã ãé·ç§»è¡åãåºåè¡åã¯ãã©ã¡ã¼ã¿ã«éããããèªä½ã«å¤§ããªæå³ããããã®ã§ã¯ãªãã
ãããæ¢ç¥ã®å ´åã次ã¯æ½å¨å¤æ°ã®æ¨å®ã«èå³ãããã
次ã¯ããã®ããããã¾ã¨ãããã