Unexpected floats generated

With these inputs


this program

data {
  int<lower=0> N;
  int<lower=1> J;
  int<lower=1> K;
generated quantities {
  real x[N];
  real u[N];
  for (n in 1:N) {
    x[n] = normal_rng(0,1);
    u[n] = normal_rng(0,1);
  real beta[K];
  real gamma[K];
  vector[K] b0;
  vector[K] g0;
  for (k in 1:K) {
      beta[k] = normal_rng(1,1);
      gamma[k] = normal_rng(3,1);
      b0[k] = normal_rng(0,1);
      g0[k] = normal_rng(0,1);
  real<lower=0> sigma = uniform_rng(.6,.8);
  real mu[N];
  real y[N];
  simplex[K] theta;
  int<lower=1,upper=J> jj[N];
  int<lower=1,upper=K> kk[N];
  theta = dirichlet_rng(rep_vector(1,J));
  vector[K] h0[J];
  for (j in 1:J) {
      for (k in 1:K) {
        h0[j][k] = normal_rng(0,1);
  for (n in 1:N) {
    jj[n] = categorical_rng(theta);
    kk[n] = categorical_logit_rng(h0[jj[n]] + x[n]*b0 + u[n]*g0);
    mu[n] = x[n] * beta[kk[n]] + u[n] * gamma[kk[n]];
    y[n] = normal_rng(mu[n], sigma);

produces a csv whose first two lines are


containing as kk.4 floating-point values where I expected small integers. How can I fix it?

Looking at the csv, it looks like all of the outputs are off by 1 (i.e., theta.3 appears to have the integer value for jj.1. Did you have initialisation errors about NaN or theta not being a simplex when you ran this?

I believe the cause is your construction of the rng for theta:

  simplex[K] theta;
  theta = dirichlet_rng(rep_vector(1,J));

You’ve declared theta as a simplex of size K, but your dirichlet rng is returning a vector of size J. Given the data input that you posted above, this means that the first two elements of theta are initialised to a two-element simplex, and the last element is uninitialised (i.e., NaN)

No, no message like that, but that did solve the issue.

1 Like