Categorical Random Number Generation with Fixed Parameters

Hi Everyone. I’m trying to figure out how to generate 100 random values from a categorical distribution with a fixed parameter set. However, I appear to be stuck and google and the manual have returned nothing I can piece together.

I am almost certain that it doesn’t appreciate my definition of the constant simplex[3] theta = {0.5,0.3,0.2}; line, in fact it tells me so, but I do not know how to properly define it so this runs correctly. Any suggestions?

Stan Code

data {
 int N;
}
generated quantities {
  simplex[3] theta = {0.5,0.3,0.2};
  int y[N];
  for (i in 1:N){
    y[i] = categorical_rng( theta );
  }
}

R code

simu_data <- list(N = 100)
fit <- sampling(generative2, data=simu_data, iter=1000, algorithm = 'Fixed_param')

Any assistance is greatly appreciated.

1 Like

You should’ve gotten an error message saying that you were trying to assign a real[] to a vector. These are different types. To create a vector, you need to create a row vector ([x1, ..., xN]) and transpose (with '):

simplex[3] theta = [0.5, 0.3, 0.2]';

You can also generate the vector of counts by using the multinomial rng.

1 Like

It’s the transpose! That was what was missing.
Awesome, thank you!