x <-setYuima(model = model)stancode <-yuima_to_stan(x)cat(stancode)
data {
int N;
vector[N+1] x;
real T;
real h;
}
parameters {
real theta ;
real mu ;
real sigma ;
}
model {
x[1] ~ normal(0,1);
for(n in 2:(N+1)){
x[n] ~ normal(x[n-1] + h * (theta * (mu - x[n-1])) ,sqrt(h) * (sigma) );
}
}
1.5CmdStanR による方法
同様のことが CmdStanR というパッケージを用いても実行可能である.
CmdStanR では一時ファイル上に stan ファイルを作成して,それをコンパイルして実行する.
data {
int N;
vector[N+1] x;
real T;
real h;
}
parameters {
real theta ;
real mu ;
real sigma ;
}
model {
x[1] ~ normal(0,1);
for(n in 2:(N+1)){
x[n] ~ normal(x[n-1] + h * (theta * (mu - x[n-1])) ,sqrt(h) * (sigma) );
}
}
Inference for Stan model: anon_model.
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
theta 0.88 0.34 0.95 -0.11 0.00 0.60 1.61 2.93 8 1.21
mu -45.22 55.65 78.78 -190.40 -50.57 -0.23 0.11 4.41 2 38.57
sigma 0.53 0.00 0.01 0.50 0.52 0.53 0.54 0.54 9 1.18
lp__ 3055.22 0.53 1.41 3052.24 3054.32 3055.20 3056.44 3057.30 7 1.21
Samples were drawn using NUTS(diag_e) at Mon Jan 13 17:26:50 2025.
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).
plot(fit)
ci_level: 0.8 (80% intervals)
outer_level: 0.95 (95% intervals)
library("bayesplot")library("rstanarm")library("ggplot2")posterior <-as.matrix(fit)plot_title <-ggtitle("Posterior distributions","with medians and 80% intervals")
library(glue)name <-" Hirofumi\n Shiba\n"major <-"Mathematics"glue::glue('My name is {name}. I study {major}. Nice to meet you!') # 名前空間の衝突を避けるために :: を使う
My name is Hirofumi
Shiba
. I study Mathematics. Nice to meet you!
glue::glue(" real {param};", param = yuima@model@parameter@all, .collapse ="\n")
data {
int N;
array[N+1] real x;
real T;
real h;
}
parameters {
real theta;
real mu;
real sigma;
}
model {
x[1] ~ normal(0, 1);
for(n in 2:(N+1)) {
x[n] ~ normal(x[n-1] + h * (theta * (mu - x[n-1])), sqrt(h) * (sigma));
}
}
3.2whisker パッケージ
install.packages("whisker")
whisker パッケージ(CRAN, GitHub)は Web を中心に採用されているテンプレートシステム Mustache に基づく機能 whisker.render() を提供している.
whisker.render(template, data =parent.frame(), partials =list(),debug =FALSE, strict =TRUE)
library(whisker)template <-'Hello {{name}}You have just won ${{value}}!{{#in_ca}}Well, ${{taxed_value}}, after taxes.{{/in_ca}}'data <-list(name ="Hirofumi", value =10000, taxed_value =10000- (10000*0.4), in_ca =TRUE)whisker.render(template, data)
[1] "Hello Hirofumi\nYou have just won $10000!\nWell, $6000, after taxes.\n"