How to write a right Multinomial distribution correctly?

Thank you Max!
Your advices are really helpful. And I have another question about valid simplex.

My data set y follows the multinomial distribution, but I just require two of three rows in it.
My final goal is that separate observation error in y, and estimates the parameters U ,B and process error sigma. So I want to have a feedback in my model.

My new code as follow,

data{
int<lower=0> y[3,18];
real log_max;
}

parameters{
vector<lower = 0>[2] sigma;
matrix<upper=log_max>[2,18]log_y;
vector<lower = 0>[2] U;
matrix[2,2] B;
}

transformed parameters{

simplex[3] theta[18];

for (i in 1:18){
theta[i,1]=exp(log_y[1,i])/exp(log_max);
theta[i,2]=exp(log_y[2,i])/exp(log_max);
theta[i,3]=(exp(log_max)-(exp(log_y[1,i])+exp(log_y[2,i])))/exp(log_max);
}

}

model{
for (i in 1:18) {
y[,i] ~ multinomial(theta[i]);
}

for(t in 1:18){
target+= multi_normal_lpdf(log_y[,t] |U+B*log_y[,t-1],diag_matrix(sigma));
}
//parameters
B[1,1]~normal(0,100);
B[2,2]~normal(0,100);
B[1,2]~normal(0,100);
B[2,1]~normal(0,100);

for (i in 1:2){
U[i]~normal(0,100);
}

for(i in 1:2){
sigma[i]~normal(0,10);
}
}

Mathematically, I think it will work. However, when I running it, this error occurred.

  • List item

Chain 1: Rejecting initial value: Chain 1: Error evaluating the log probability at the initial value. Chain 1: Exception: validate transformed params: theta[i_0__] is not a valid simplex. sum(theta[i_0__]) = 0.4409792075, but should be 1 (in ‘model5fc4cb65d0d_0219’ at line 15)

I think it was caused by sampling deviation, the sum of random sample could not be 1. I am not sure.
If possible, please tell me what should I do.

sincerely