Calculate likelihood of a sample given an arbitrary model

Dear Stan developers,

Hi!
I want to calculate likelihood of a sample given an arbitrary model.
An example of an arbitrary model is the two-normal-distributions-model such as
y ~ normal(mu1,sigma) + normal(mu2,sigma)
Therefore, a full stan model code would be

model = "
data {
  int nsample;
  vector[nsample] y;
}

parameters {
  real mu1;
  real mu2;
  real<lower=0> sigma;
}

model {
  mu1 ~ normal(0, 10); // prior for mu
  mu2 ~ normal(0, 10); // prior for mu
  sigma ~ chi_square(5); // prior for sigma
  y ~ normal(mu1, sigma) + normal(mu2, sigma); // likelihood
}
"

But, what I implemented this code, I got an error message.


   15:   mu2 ~ normal(0, 10); // prior for mu
    16:   sigma ~ chi_square(5); // prior for sigma
    17:   y ~ normal(mu1, sigma) + normal(mu2, sigma); // likelihood
                                ^
    18: }
  -------------------------------------------------

PARSER EXPECTED: ";"
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model '602c2ee08ea87ded2ea72999f07f9640' due to the above error.

How can I implement the two-normal-distributions-model?

Also, I have another question.
I know the log-likelihood from a single normal distribution model is

generated quantities {
  vector[nsample] log_lik;
  for (i in 1:nsample) {
    log_lik[i] = normal_lpdf(y[i] | mu, sigma);
  }
}

If I want to calculate the log-likelihood from the two-normal-distributions-model in the generated quantities block, how can I implement that?

Thanks in advance!
Heeseung

The problem is: what does

mean? What is the probability density function f_Y(y) you want to define as target for sampling here?

I am sorry for the unclarity. I want to fit the three parameters, mu1, mu2, sigma , of the probability density function (normal(mu1,sigma)+normal(mu2,sigma))/2 .

In which case the SUG chapter on Mixture modelling should be of help.

1 Like