-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathhearts.stan
More file actions
45 lines (43 loc) · 959 Bytes
/
hearts.stan
File metadata and controls
45 lines (43 loc) · 959 Bytes
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
/**
* Hearts: a mixture model for count data
* http://www.openbugs.net/Examples/Hearts.html
*
* integrate out the binary parameters in hearts.stan.0
*/
data {
int<lower=0> N;
array[N] int<lower=0> x;
array[N] int<lower=0> y;
array[N] int<lower=0> t;
}
parameters {
real alpha;
real delta;
}
transformed parameters {
real<lower=0, upper=1> theta;
theta = inv_logit(delta);
}
model {
real p;
real log1m_theta;
p = inv_logit(alpha);
log1m_theta = log1m(theta);
alpha ~ normal(0, 100);
delta ~ normal(0, 100);
for (i in 1 : N) {
// P(y_i = 0 | t_i)
// = theta + (1 - theta) * (1 - p)^{t_i}
// p(y_i | t_i)
// = (1 - theta) * Binomial(y_i|t_i, p) for y_i = 1,2,...,t_i
if (y[i] == 0) {
target += log(theta + (1 - theta) * pow(1 - p, t[i]));
} else {
target += log1m_theta + binomial_lpmf(y[i] | t[i], p);
}
}
}
generated quantities {
real beta;
beta = exp(alpha);
}