summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lstbayes/examples.tex
blob: 9ff02ab83dc5ebab00422b79b45dde2034e3f3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
\documentclass{article}

\title{Examples for the \textsf{lstbayes} package}
\author{Jeffrey B. Arnold}

\usepackage{lstbayes}
\usepackage{hyperref}

\begin{document}

\maketitle{}

Some example programs typset using the \textsf{listings} language drivers provideb by the \textsf{lstbayes} package.

\section{BUGS}

The Rats model from the OpenBUGS Examples Volume I: \url{http://www.openbugs.net/Examples/Rats.html}.
\begin{lstlisting}[language=BUGS]
model {
  for( i in 1 : N ) {
     for( j in 1 : T ) {
        Y[i , j] ~ dnorm(mu[i , j],tau.c)
        mu[i , j] <- alpha[i] + beta[i] * (x[j] - xbar)
        culmative.Y[i , j] <- culmative(Y[i , j], Y[i , j])
        post.pv.Y[i , j] <- post.p.value(Y[i , j])
        prior.pv.Y[i , j] <- prior.p.value(Y[i , j])
        replicate.post.Y[i , j] <- replicate.post(Y[i , j])
        pv.post.Y[i , j] <- step(Y[i , j] - replicate.post.Y[i , j])
        replicate.prior.Y[i , j] <- replicate.prior(Y[i , j])
        pv.prior.Y[i , j] <- step(Y[i , j] - replicate.prior.Y[i , j])
     }
     alpha[i] ~ dnorm(alpha.c,alpha.tau)
     beta[i] ~ dnorm(beta.c,beta.tau)
  }
  tau.c ~ dgamma(0.001,0.001)
  sigma <- 1 / sqrt(tau.c)
  alpha.c ~ dnorm(0.0,1.0E-6)
  alpha.tau ~ dgamma(0.001,0.001)
  beta.c ~ dnorm(0.0,1.0E-6)
  beta.tau ~ dgamma(0.001,0.001)
  alpha0 <- alpha.c - xbar * beta.c
}
\end{lstlisting}


\section{JAGS}

Linear regression example from John Myles White, \url{http://www.johnmyleswhite.com/notebook/2010/08/20/using-jags-in-r-with-the-rjags-package/}.

\begin{lstlisting}[language=JAGS]
model {
	for (i in 1:N){
		y[i] ~ dnorm(y.hat[i], tau)
		y.hat[i] <- a + b * x[i]
	}
	a ~ dnorm(0, .0001)
	b ~ dnorm(0, .0001)
	tau <- pow(sigma, -2)
	sigma ~ dunif(0, 100)
}
\end{lstlisting}


\section{Stan}

Rats example from \url{https://github.com/stan-dev/example-models/blob/master/bugs_examples/vol1/rats/rats_vec.stan}.

\begin{lstlisting}[language=Stan]
// http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/Vol1.pdf
// Page 3: Rats
data {
  int<lower=0> N;
  int<lower=0> T;
  real x[T];
  real y[N,T];
  real xbar;
}
transformed data {
  real x_minus_xbar[T];
  real y_linear[N*T];

  for (t in 1:T)
    x_minus_xbar[t] <- x[t] - xbar;

  for (n in 1:N)
    for (t in 1:T)
      y_linear[(n-1)*T + t] <- y[n, t];
}
parameters {
  real alpha[N];
  real beta[N];

  real mu_alpha;
  real mu_beta;

  real<lower=0> sigmasq_y;
  real<lower=0> sigmasq_alpha;
  real<lower=0> sigmasq_beta;
}
transformed parameters {
  real<lower=0> sigma_y;
  real<lower=0> sigma_alpha;
  real<lower=0> sigma_beta;

  sigma_y <- sqrt(sigmasq_y);
  sigma_alpha <- sqrt(sigmasq_alpha);
  sigma_beta <- sqrt(sigmasq_beta);
}
model {
  real pred[N*T];

  for (n in 1:N)
    for (t in 1:T)
      pred[(n-1)*T + t] <- fma(beta[n], x_minus_xbar[t], alpha[n]);

  mu_alpha ~ normal(0, 100);
  mu_beta ~ normal(0, 100);
  sigmasq_y ~ inv_gamma(0.001, 0.001);
  sigmasq_alpha ~ inv_gamma(0.001, 0.001);
  sigmasq_beta ~ inv_gamma(0.001, 0.001);
  alpha ~ normal(mu_alpha, sigma_alpha); // vectorized
  beta ~ normal(mu_beta, sigma_beta);  // vectorized

  y_linear ~ normal(pred, sigma_y);  // vectorized
}
\end{lstlisting}

\end{document}