Skip to content

Commit 5cc822d

Browse files
author
Bob Carpenter
committed
stagnant models from bugs volume 2
1 parent cc585d9 commit 5cc822d

File tree

5 files changed

+81
-37
lines changed

5 files changed

+81
-37
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library('rstan')
2+
source('stagnant.data.R')
3+
fit <- stan('stagnant2.stan',
4+
data=c("N","x","Y"),
5+
chains=4, iter=2000);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Change point model with very poor parameterization from BUGS, vol 2
2+
3+
// Bradley P. Carlin; Alan E. Gelfand; Adrian F. M. Smith.
4+
// Hierarchical Bayesian Analysis of Changepoint Problems
5+
// Applied Statistics, Vol. 41, No. 2. (1992), pp. 389-405.
6+
//
7+
// In these data, X represents the logarithm of the flow rate of
8+
// water down an inclined channel (grams per centimetre per second)
9+
// and Y represents the logarithm of the height of the stagnant
10+
// surface layer (centimetres) for different surfactants.
11+
12+
13+
data {
14+
int<lower=0> N;
15+
real x[N];
16+
real Y[N];
17+
}
18+
parameters {
19+
real<lower=0> sigma;
20+
real<lower=0> alpha;
21+
real beta[2];
22+
simplex[N] theta;
23+
}
24+
model {
25+
// local variables
26+
real log_probs[N];
27+
real mu[N];
28+
29+
// priors
30+
theta ~ dirichlet(rep_vector(0.01,N));
31+
alpha ~ normal(0,5);
32+
beta ~ normal(0,5);
33+
sigma ~ cauchy(0,5);
34+
35+
// mixture likelihood
36+
for (k in 1:N) {
37+
for (n in 1:N)
38+
mu[n] <- alpha + if_else(n <= k, beta[1], beta[2]) * (x[n] - x[k]);
39+
log_probs[k] <- log(theta[k]) + normal_log(Y, mu, sigma);
40+
}
41+
lp__ <- lp__ + log_sum_exp(log_probs);
42+
}

bugs_examples/vol2/stagnant/stagnant.stan.0

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"N" <- 29
2+
"x" <-
3+
c(-1.39, -1.39, -1.08, -1.08, -0.94, -0.8, -0.63, -0.63, -0.25,
4+
-0.25, -0.12, -0.12, 0.01, 0.11, 0.11, 0.11, 0.25, 0.25, 0.34,
5+
0.34, 0.44, 0.59, 0.7, 0.7, 0.85, 0.85, 0.99, 0.99, 1.19)
6+
"Y" <-
7+
c(1.12, 1.12, 0.99, 1.03, 0.92, 0.9, 0.81, 0.83, 0.65, 0.67,
8+
0.6, 0.59, 0.51, 0.44, 0.43, 0.43, 0.33, 0.3, 0.25, 0.24, 0.13,
9+
-0.01, -0.13, -0.14, -0.3, -0.33, -0.46, -0.43, -0.65)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data {
2+
int<lower=0> N;
3+
real x[N];
4+
real Y[N];
5+
}
6+
parameters {
7+
real<lower=0> sigma;
8+
real<lower=0> alpha;
9+
real beta[2];
10+
real<lower=min(x),upper=max(x)> x_change;
11+
}
12+
model {
13+
real mu[N];
14+
15+
alpha ~ normal(0,5);
16+
beta ~ normal(0,5);
17+
sigma ~ cauchy(0,5);
18+
19+
for (n in 1:N)
20+
mu[n] <- alpha
21+
+ if_else(x[n] < x_change, beta[1], beta[2]) * (x[n] - x_change);
22+
23+
Y ~ normal(mu,sigma);
24+
}
25+

0 commit comments

Comments
 (0)