//
// This Stan program defines a simple model, with a
// vector of values 'y' modeled as normally distributed
// with mean 'mu' and standard deviation 'sigma'.
//
// Learn more about model development with Stan at:
//
//    http://mc-stan.org/users/interfaces/rstan.html
//    https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
//
// The input data is a vector 'y' of length 'N'.
data {
  int<lower=1> D;
  int<lower=1> N;
  int<lower=1> L;
  int<lower=0, upper=1> y[N];
  int<lower=1, upper=L> ll[N];
  row_vector[D] x[N];
}
parameters {
  real mu[D];
  real<lower=0> sigma[D];
  vector[D] beta[L];
}
model {
  sigma ~ gamma(2,1); //shape and inverse scale
  mu ~ normal(0, 1); //convert to mvnormal
  for (l in 1:L) {
    beta[l] ~ normal(mu, sigma);
  }
  {
    vector[N] x_beta_ll;
    for (n in 1:N) {
      x_beta_ll[n] = x[n] * beta[ll[n]];
    }
    y ~ bernoulli_logit(x_beta_ll);
  }
}

CXXFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-MkHsSu/r-base-4.2.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 $(LTO)