Search code examples
rstan

Stan - Mixture of Normals


I'm using RStan to fit a mixture of two normals.

data {
 int<lower = 1> K;
 int<lower = 1> N;
 real y[N];
}
parameters {
 simplex[K] theta;
 real mu[K];
 real<lower = 0> sigma;
}
model{
 real ps[K]; // place-holder for log component densities
 sigma ~ uniform(0.5, 1.5);
 mu ~ normal(0, 10);
 for (n in 1:N){
  for (k in 1:K) {
   ps[k] <- log(theta[k]) + normal_log(y[n], mu[k], sigma);
  }
  increment_log_prob(log_sum_exp(ps)); // log_sum_exp(lp1,lp2) =    log(exp(lp1) + exp(lp2))
  }
}

I'd like to add a condition that mu[1] is less than mu[2]. How can I do that?

Thanks for your help


Solution

  • There is an ordered type in Stan. Declare mu as ordered[K] mu.