// generated with brms 2.16.0
functions {
  /* dirichlet-logit log-PDF
   * Args: 
   *   y: vector of real response values
   *   mu: vector of category logit probabilities
   *   phi: precision parameter
   * Returns:  
   *   a scalar to be added to the log posterior 
   */ 
   real dirichlet_logit_lpdf(vector y, vector mu, real phi) {
     return dirichlet_lpdf(y | softmax(mu) * phi);
   }
}
data {
  int<lower=1> N;  // total number of observations
  int<lower=2> ncat;  // number of categories
  vector[ncat] Y[N];  // response array
  int<lower=1> K_muy2;  // number of population-level effects
  matrix[N, K_muy2] X_muy2;  // population-level design matrix
  int<lower=1> K_muy3;  // number of population-level effects
  matrix[N, K_muy3] X_muy3;  // population-level design matrix
  int prior_only;  // should the likelihood be ignored?
}
transformed data {
  int Kc_muy2 = K_muy2 - 1;
  matrix[N, Kc_muy2] Xc_muy2;  // centered version of X_muy2 without an intercept
  vector[Kc_muy2] means_X_muy2;  // column means of X_muy2 before centering
  int Kc_muy3 = K_muy3 - 1;
  matrix[N, Kc_muy3] Xc_muy3;  // centered version of X_muy3 without an intercept
  vector[Kc_muy3] means_X_muy3;  // column means of X_muy3 before centering
  for (i in 2:K_muy2) {
    means_X_muy2[i - 1] = mean(X_muy2[, i]);
    Xc_muy2[, i - 1] = X_muy2[, i] - means_X_muy2[i - 1];
  }
  for (i in 2:K_muy3) {
    means_X_muy3[i - 1] = mean(X_muy3[, i]);
    Xc_muy3[, i - 1] = X_muy3[, i] - means_X_muy3[i - 1];
  }
}
parameters {
  vector[Kc_muy2] b_muy2;  // population-level effects
  real Intercept_muy2;  // temporary intercept for centered predictors
  vector[Kc_muy3] b_muy3;  // population-level effects
  real Intercept_muy3;  // temporary intercept for centered predictors
  real<lower=0> phi;  // precision parameter
}
transformed parameters {
}
model {
  // likelihood including constants
  if (!prior_only) {
    // initialize linear predictor term
    vector[N] muy2 = Intercept_muy2 + Xc_muy2 * b_muy2;
    // initialize linear predictor term
    vector[N] muy3 = Intercept_muy3 + Xc_muy3 * b_muy3;
    // linear predictor matrix
    vector[ncat] mu[N];
    for (n in 1:N) {
      mu[n] = transpose([0, muy2[n], muy3[n]]);
    }
    for (n in 1:N) {
      target += dirichlet_logit_lpdf(Y[n] | mu[n], phi);
    }
  }
  // priors including constants
  target += student_t_lpdf(Intercept_muy2 | 3, 0, 2.5);
  target += student_t_lpdf(Intercept_muy3 | 3, 0, 2.5);
  target += gamma_lpdf(phi | 0.01, 0.01);
}
generated quantities {
  // actual population-level intercept
  real b_muy2_Intercept = Intercept_muy2 - dot_product(means_X_muy2, b_muy2);
  // actual population-level intercept
  real b_muy3_Intercept = Intercept_muy3 - dot_product(means_X_muy3, b_muy3);
}