| 1 |
\documentclass{article} |
| 2 |
|
| 3 |
\title{Examples for the \textsf{lstbayes} package} |
| 4 |
\author{Jeffrey B. Arnold} |
| 5 |
|
| 6 |
\usepackage{lstbayes} |
| 7 |
\usepackage{hyperref} |
| 8 |
|
| 9 |
\begin{document} |
| 10 |
|
| 11 |
\maketitle{} |
| 12 |
|
| 13 |
Some example programs typset using the \textsf{listings} language drivers provideb by the \textsf{lstbayes} package. |
| 14 |
|
| 15 |
\section{BUGS} |
| 16 |
|
| 17 |
The Rats model from the OpenBUGS Examples Volume I: \url{http://www.openbugs.net/Examples/Rats.html}. |
| 18 |
\begin{lstlisting}[language=BUGS] |
| 19 |
model { |
| 20 |
for( i in 1 : N ) { |
| 21 |
for( j in 1 : T ) { |
| 22 |
Y[i , j] ~ dnorm(mu[i , j],tau.c) |
| 23 |
mu[i , j] <- alpha[i] + beta[i] * (x[j] - xbar) |
| 24 |
culmative.Y[i , j] <- culmative(Y[i , j], Y[i , j]) |
| 25 |
post.pv.Y[i , j] <- post.p.value(Y[i , j]) |
| 26 |
prior.pv.Y[i , j] <- prior.p.value(Y[i , j]) |
| 27 |
replicate.post.Y[i , j] <- replicate.post(Y[i , j]) |
| 28 |
pv.post.Y[i , j] <- step(Y[i , j] - replicate.post.Y[i , j]) |
| 29 |
replicate.prior.Y[i , j] <- replicate.prior(Y[i , j]) |
| 30 |
pv.prior.Y[i , j] <- step(Y[i , j] - replicate.prior.Y[i , j]) |
| 31 |
} |
| 32 |
alpha[i] ~ dnorm(alpha.c,alpha.tau) |
| 33 |
beta[i] ~ dnorm(beta.c,beta.tau) |
| 34 |
} |
| 35 |
tau.c ~ dgamma(0.001,0.001) |
| 36 |
sigma <- 1 / sqrt(tau.c) |
| 37 |
alpha.c ~ dnorm(0.0,1.0E-6) |
| 38 |
alpha.tau ~ dgamma(0.001,0.001) |
| 39 |
beta.c ~ dnorm(0.0,1.0E-6) |
| 40 |
beta.tau ~ dgamma(0.001,0.001) |
| 41 |
alpha0 <- alpha.c - xbar * beta.c |
| 42 |
} |
| 43 |
\end{lstlisting} |
| 44 |
|
| 45 |
|
| 46 |
\section{JAGS} |
| 47 |
|
| 48 |
Linear regression example from John Myles White, \url{http://www.johnmyleswhite.com/notebook/2010/08/20/using-jags-in-r-with-the-rjags-package/}. |
| 49 |
|
| 50 |
\begin{lstlisting}[language=JAGS] |
| 51 |
model { |
| 52 |
for (i in 1:N){ |
| 53 |
y[i] ~ dnorm(y.hat[i], tau) |
| 54 |
y.hat[i] <- a + b * x[i] |
| 55 |
} |
| 56 |
a ~ dnorm(0, .0001) |
| 57 |
b ~ dnorm(0, .0001) |
| 58 |
tau <- pow(sigma, -2) |
| 59 |
sigma ~ dunif(0, 100) |
| 60 |
} |
| 61 |
\end{lstlisting} |
| 62 |
|
| 63 |
|
| 64 |
\section{Stan} |
| 65 |
|
| 66 |
Rats example from \url{https://github.com/stan-dev/example-models/blob/master/bugs_examples/vol1/rats/rats_vec.stan}. |
| 67 |
|
| 68 |
\begin{lstlisting}[language=Stan] |
| 69 |
// http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/Vol1.pdf |
| 70 |
// Page 3: Rats |
| 71 |
data { |
| 72 |
int<lower=0> N; |
| 73 |
int<lower=0> T; |
| 74 |
real x[T]; |
| 75 |
real y[N,T]; |
| 76 |
real xbar; |
| 77 |
} |
| 78 |
transformed data { |
| 79 |
real x_minus_xbar[T]; |
| 80 |
real y_linear[N*T]; |
| 81 |
|
| 82 |
for (t in 1:T) |
| 83 |
x_minus_xbar[t] <- x[t] - xbar; |
| 84 |
|
| 85 |
for (n in 1:N) |
| 86 |
for (t in 1:T) |
| 87 |
y_linear[(n-1)*T + t] <- y[n, t]; |
| 88 |
} |
| 89 |
parameters { |
| 90 |
real alpha[N]; |
| 91 |
real beta[N]; |
| 92 |
|
| 93 |
real mu_alpha; |
| 94 |
real mu_beta; |
| 95 |
|
| 96 |
real<lower=0> sigmasq_y; |
| 97 |
real<lower=0> sigmasq_alpha; |
| 98 |
real<lower=0> sigmasq_beta; |
| 99 |
} |
| 100 |
transformed parameters { |
| 101 |
real<lower=0> sigma_y; |
| 102 |
real<lower=0> sigma_alpha; |
| 103 |
real<lower=0> sigma_beta; |
| 104 |
|
| 105 |
sigma_y <- sqrt(sigmasq_y); |
| 106 |
sigma_alpha <- sqrt(sigmasq_alpha); |
| 107 |
sigma_beta <- sqrt(sigmasq_beta); |
| 108 |
} |
| 109 |
model { |
| 110 |
real pred[N*T]; |
| 111 |
|
| 112 |
for (n in 1:N) |
| 113 |
for (t in 1:T) |
| 114 |
pred[(n-1)*T + t] <- fma(beta[n], x_minus_xbar[t], alpha[n]); |
| 115 |
|
| 116 |
mu_alpha ~ normal(0, 100); |
| 117 |
mu_beta ~ normal(0, 100); |
| 118 |
sigmasq_y ~ inv_gamma(0.001, 0.001); |
| 119 |
sigmasq_alpha ~ inv_gamma(0.001, 0.001); |
| 120 |
sigmasq_beta ~ inv_gamma(0.001, 0.001); |
| 121 |
alpha ~ normal(mu_alpha, sigma_alpha); // vectorized |
| 122 |
beta ~ normal(mu_beta, sigma_beta); // vectorized |
| 123 |
|
| 124 |
y_linear ~ normal(pred, sigma_y); // vectorized |
| 125 |
} |
| 126 |
\end{lstlisting} |
| 127 |
|
| 128 |
\end{document} |