Slide 25
Slide 25 text
"EWFSUJTJOH
605
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
24 ষɹɹRcpp
Rcpp
RcppRͷؔΛC++Ͱ࣮Ͱ͖ΔύοέʔδͰ͢ɻRͱྨࣅͨ͠ελΠϧͰهड़Ͱ͖
ΔΑ͏ʹ࣮͞Ε͍ͯΔͨΊɺC++ʹਂ͍͕ࣝͳͯ͘ར༻͘͢͠ͳ͍ͬͯ·͢ɻ͠
͔ɺͦͷͨΊͷ࣮ߦ٘ਜ਼ʹ͞Ε͍ͯͳ͍ͷͰɺ୭ͰϋΠύϑΥʔϚϯεͳ݁ՌΛ
ಘΔ͜ͱ͕Ͱ͖·͢ɻ
24-1 Rcppͷ׆༻γʔϯ
࣍ͷΑ͏ͳέʔεC++Ͱ࣮͢Δ͜ͱʹΑΓɺRͱൺͯߴԽ͕ݟࠐΊ·͢ɻ
ɾ ܁Γฦ͠ॲཧɺಛʹ࣍ͷॲཧ͕લͷॲཧʹґଘ͓ͯ͠ΓฒྻԽͰ͖ͳ͍
ɾ ϕΫ
τϧߦྻͷݸʑͷཁૉΞΫηε͢Δඞཁ͕͋Δ
ɾ ϕΫ
τϧͷαΠζΛಈతʹมߋ͍ͨ͠
ɾ ߴͳσʔλߏΞϧΰϦζϜΛ༻͍ͨॲཧΛߦ͍͍ͨ
RcppͷύϑΥʔϚϯεΛࣔͨ͢Ίɺ܁Γฦ͠ॲཧͷྫͱͯ͠MCMCΞϧΰϦζϜͷҰछͰ͋
ΔΪϒεαϯϓϥʔ
ʢ1ʣ
ͷ࣮ྫΛࣔ͠·͢ɻ͜ͷྫͰɺΪϒεαϯϓϥʔʹΑΓඪ४2ม
ਖ਼ن͔ΒnαϯϓϦϯά͍ͯ͠·͢ɻ
·ͣൺֱͷͨΊRΛ༻͍࣮ͨྫΛࣔ͠·͢
ʢϦετ24.1ʣ
ɻ
Ϧε
τ24.1ɹGibbs.R
GibbsR <- function(b, n, t){
# 2มඪ४ਖ਼ن͔ΒnαϯϓϦ
ϯά
# b : 2มͷڞࢄ
# n : αϯϓϧ
# t : αϯϓϦ
ϯάࣺͤͣͯΔִؒ
X <- matrix(0, nrow = n, ncol = 2)
x1 <- x2 <- 0
sd <- sqrt(1-b^2)
for(i in 1:n){
for(j in 1:t){
x1 <- rnorm(1, b*x2, sd)
x2 <- rnorm(1, b*x1, sd)
ʢ1ʣ ߴ࣍ݩͷෳࡶͳ֬ʹै͏ཚΛੜ͢ΔϚϧίϑ࿈ϞϯςΧϧϩ๏ͱݺΕΔΞϧΰϦζϜͷҰछͰ͢ɻ
24-1
24 ষ
ٕज़ධࣾɹ੫ࠐԁ