This is an old version, view current version.

2 Example Model and Data

The following is a simple, complete Stan program for a Bernoulli model of binary data.4 The model assumes the binary observed data y[1],...,y[N] are i.i.d. with Bernoulli chance-of-success theta.

data { 
  int<lower=0> N; 
  array[N] int<lower=0, upper=1> y;
parameters {
  real<lower=0, upper=1> theta;
model {
  theta ~ beta(1, 1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta);

The input data file contains definitions for the two variables N and y which are specified in the data block of program bernoulli.stan (above).

A data set of N=10 observations is included in the example Bernoulli model directory in both JSON notation and Rdump data format where 8 out of 10 trials had outcome 0 (failure) and 2 trials had outcome 1 (success). In JSON, this data is:

    "N" : 10,
    "y" : [0,1,0,0,0,0,0,0,0,1]

  1. The model is available with the CmdStan distribution at the path <cmdstan-home>/examples/bernoulli/bernoulli.stan.↩︎