Waldo: normal_lpdf: Location parameter is nan, but must be finite!`

normal_lpdf: Location parameter is nan, but must be finite!

I seem to be blind. I’m using code similar to this (my goal is something like BVAR, or a way to forecast five time series with a hierarchical element). The cmdstanr syntax check comes back fine, but then sampling fails—something about the mu line. What are the chances someone can see what I am missing and causing the above error?

I’ve read some of the other posts about this but haven’t been able to solve this with initializing the starting values, either, like drift and the sigmas starting at value 1.

(Bonus points if you help me resolve this too: “Declaration of arrays by placing brackets after a variable name is deprecated…”)

data {
 int <lower = 0> N;
 vector[N] pmpm;
 vector[N] size;
}

parameters {
 real <lower = 0> sig_obs;
 real <lower = 0> sig_proc;
 real drift;
}

transformed parameters {
  real mu[N]; 
  mu[1] = pmpm[1];
}

model {
  drift ~ normal(0, 1);
  sig_obs ~ exponential(10);
  sig_proc ~ exponential(1);

  for (i in 2:N) {
    pmpm[i] ~ normal(mu[i], sig_proc / sqrt(size[i]));
    mu[i] ~ normal(mu[i - 1] + drift, sig_obs / sqrt(size[i]));
  }
}

The inputs are like this:

N
[1] 14

pmpm (centered and scaled)
 [1] -0.36  1.13  0.42 -0.08 -1.11 -1.17 -0.71 -0.34 -1.16 -0.73 -0.06  1.01 1.86  1.31

size
 [1] 526 559 570 564 551 520 530 580 615 652 668 649 636 581

Thank you.

Here, you only assign one mu within a size N array of mus anything, and it is only to data.

This warning refers to depreciated syntax being used here,

real mu[N];

which is equivalent to the new syntax,

array[N] real mu;

Thanks! So, I treated the mu vector as two different things (not sure of the right terms, but maybe something static like data and something variable like a distribution). This code works—I declare mu in the parameter block and then set mu[1] with a distribution assignment instead of a “=” assignment in the transformed parameter block. (I also updated the declaration syntax.)

data {
 int <lower = 0> N;
 array[N] real pmpm;
 array[N] real size;
}

parameters {
 array[N] real mu; 
 real <lower = 0> sig_obs;
 real <lower = 0> sig_proc;
 real drift;
}

model {
  drift ~ normal(0, 1);
  sig_obs ~ exponential(10);
  sig_proc ~ exponential(1);

  mu[1] ~ normal(pmpm[1], 0.001);

  for (i in 2:N) {
    pmpm[i] ~ normal(mu[i], sig_proc / sqrt(size[i]));
    mu[i] ~ normal((mu[i - 1] + drift), sig_obs / sqrt(size[i]));
  }
}

Second bonus question: In Stan, is there a difference between declaring a variable an array vs. vector vs. matrix? Or do they all act the same, and it’s a matter of defining the dimensions—like a vector is a special case of 1xn matrix?

Yes, they have different properties: 5.6 Array data types | Stan Reference Manual