-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathbern.stan
35 lines (35 loc) · 1020 Bytes
/
bern.stan
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
// Bernoulli model
data {
int<lower=0> N; // number of observations
array[N] int<lower=0, upper=1> y; // vector of binary observations
}
parameters {
real<lower=0, upper=1> theta; // probability of success
}
model {
// model block creates the log density to be sampled
theta ~ beta(1, 1); // prior
y ~ bernoulli(theta); // observation model / likelihood
// the notation using ~ is syntactic sugar for
// target += beta_lpdf(theta | 1, 1); // lpdf for continuous theta
// target += bernoulli_lpmf(y | theta); // lpmf for discrete y
// target is the log density to be sampled
//
// y is an array of integers and
// y ~ bernoulli(theta);
// is equivalent to
// for (i in 1:N) {
// y[i] ~ bernoulli(theta);
// }
// which is equivalent to
// for (i in 1:N) {
// target += bernoulli_lpmf(y[i] | theta);
// }
}
generated quantities {
vector[N] log_lik;
real lprior = beta_lpdf(theta | 1, 1);
for (i in 1:N) {
log_lik[i] = bernoulli_lpmf(y[i] | theta);
}
}