# Exception: normal_rng: Location parameter is nan, but must be finite!

Hello,
i’m new to stan.I am trying to fit a exponential curve for a time series data and forecast the predicted values . I’m getting a error: Exception: normal_rng: Location parameter is nan, but must be finite!
Here is my code:
model=
“”"stan
data {
int<lower=0> N;
vector[N] y;
int<lower=1> n_pred;
}
parameters {
real beta0;
real beta1;
real<lower=0> sigma;
}
transformed parameters {
real m;
real b0;
real b1;
b0 = exp(beta0);
b1 = exp(beta1);

}
model {
// priors
beta0 ~ normal(0,10);
beta1 ~ normal(0, 10);
sigma ~ cauchy(0,5);
// likelihood
for (n in 2:N)
y[n] ~ normal(beta0 + beta1 * y[n-1], sigma);
}
generated quantities {
vector[n_pred] pred;
for (k in 2:n_pred)
pred[k] = normal_rng(b0 + b1 * pred[k-1], sigma);
}

“”"
And my data is:
data2 = {‘N’:df.shape[0] ,
‘y’: y,
‘n_pred’:df.shape[0]
}

sm1 = pystan.StanModel(model_code=model)
fit = sm1.sampling(data=data2, iter=5000, chains=1).

Please provide me some help.

Hi, is because pred[0] in your Stan code is empty. And Stan cannot deal with empty values. You can estimate that value or at least its mean…
mu0 ~normal(0,10);

And then pred[1] = normal_rng(mu0,sigma);

1 Like

Thanks for your reply. Can you please help me what changes do i need to make in the code. It would be very helpful.

A simple solution can be something like this, tell me if works

data {
int<lower=0> N;
vector[N] y;
int<lower=1> n_pred;
}
parameters {
real beta0;
real beta1;
real<lower=0> sigma;
}
transformed parameters {
real m;
real b0;
real b1;
b0 = exp(beta0);
b1 = exp(beta1);

}
model {
// priors
beta0 ~ normal(0,10);
beta1 ~ normal(0, 10);
sigma ~ cauchy(0,5);

y[1] ~ normal(beta0,sigma);
// likelihood
for (n in 2:N)
y[n] ~ normal(beta0 + beta1 * y[n-1], sigma);
}
generated quantities {
vector[n_pred] pred;

pred[1] = normal_rng(b0, sigma);
for (k in 2:n_pred)
pred[k] = normal_rng(b0 + b1 * pred[k-1], sigma);
}

you should also check what with that transformations you made b0 = exp(beta0); I don´t understand their reason.

1 Like

Thanks for the solution , it worked.

1 Like