MCMC issues with: Location parameter is nan, but must be finite!

Hi all,

I am running the following code, which initially instead of a normal distribution I was using a lognormal but it gave me the same error, which is:

Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: normal_lpdf: Location parameter is nan, but must be finite! (in ‘C:/Users/IGARCI~1/AppData/Local/Temp/Rtmpm8knEn/model-29941dda214a.stan’, line 50, column 4 to column 40)

Removing the exp function does not solve this either. This is causing issues because it takes too long to converge, and I do not really know what is causing this nas, even if I had defined parameter constraints in the code.
I have also used in R in order to sample the step_size function, which has not solve this either:
cmd_fit ← modelcmd$sample(

  • step_size= 0.1, data = datalist,*
  • seed = 42L,*
  • refresh = 0)*

Any ideas on how can I fix this?

functions {
  vector model3(real t, vector y, real pAB, real uAB, real uASC) {
    vector[2] dydt;  
    dydt[1] = pAB*y[2]-uAB*y[1];
    dydt[2] = -uASC*y[2];
   return dydt;
data {
  int <lower=1> nobs;
  real t0;
  vector[2] y0;
  real ts[nobs];
  int <lower=1> indivs;
  real <lower=0> antib[nobs];
  //real <lower=1, upper=indivs> subj[nobs];
parameters {
  real <lower=0> pAB0;
  real <lower=0> uAB;
  real <lower=0> uASC0;
  real <lower=0> sigma;
  real <lower=0> sigmapAB;
  real <lower=0> sigmauASC;
  //real <lower=0> rpABmu;
  //real <lower=0> ruASCmu;
  real <lower=0> rpAB[indivs];
  real <lower=0> ruASC[indivs];
model {
  vector[2] yhat[nobs];
  //prior distributions
  pAB0 ~ normal(0.5, 0.5);
  uASC0 ~ normal(0.5, 0.5);
  //rpABmu ~ normal(0, 0.001);
  uAB ~ normal(0, 0.001);
  //ruASCmu ~ normal(0, 0.001);
  sigmapAB ~ inv_gamma(0.01, 0.01);
  sigmauASC ~ inv_gamma(0.01, 0.01);
  sigma ~ inv_gamma(0.01, 0.01);
  for (j in 1:indivs) {
    real pAB = exp(pAB0)*exp(rpAB[j]);
    real uABt = uAB;
    real uASC = exp(uASC0)*exp(ruASC[j]);
    yhat = ode_rk45(model3, y0, t0, ts, pAB, uABt, uASC);
  for (i in 1:nobs) {
    antib[i] ~ normal(yhat[i,2], sigma); 
    rpAB[j] ~ normal(0.1, sigmapAB);
    ruASC[j] ~ normal(0.1, sigmauASC);

generated quantities {
  real z_pred[nobs];
  for (t in 1:nobs){
    z_pred[t] = normal_rng(antib[t], sigma);

Do you see this message only during early warmup, or does it persist during sampling? In the former case it’s safe to ignore. In the latter case, probably there is some mistake in your ODE implementation that is causing the solver to return nan.


This happens during the whole sampling process. You do not think that this can be due to the lack of initial values in R? What exactly in the code may cause this since I have already defined constraints?

I just want to make sure we are communicating well here; you’re saying that this message gets output dozens or hundreds of times, including at least once after warmup is finished? If so, then the question is why integrating your ODE system is returning nan. It could be a numerical issue, a problem with the data that you are passing, or a bug in your model3 function. But before diving into this, I do want to make sure that you’re saying that this message is showing up after warmup. I saw on another thread that you ran the model overnight and didn’t make it out of warmup, so I want to double check this.

The issue is that the model that was running overnight it was through the statement of sampling() after running the stan_model. This time, I tried with cmdstan and model$sample(), which does not distinguish between the warning messages in the warmup and sampling. Is this clear? Or am I suppose to observe the same warning messages in both of them?

The question is whether the warning messages continue to appear after warmup has finished. Running under the defaults, this is after 1000 iterations (50%).

I get a similar error and I do not know how can I check if it’s during warm up or after it. Is there a way to print the iteration number in PyStan?