RuntimeError - Initialization failed

I’m trying to fit two AR(1) latent state variables using one time series.
c_t = \mu + s_t + l_t + \sigma_g \varepsilon_{g,t}
where
s_t = \rho_s s_{t-1} + \sigma_s \varepsilon_t
and
l_t = \rho_l l_{t-1} - sigma_l \varepsilon_{z,t}
where both shocks s_t and l_t are inversely affected by the same noise term (\varepsilon_t). Coding this up gives me the following STAN model:

data {
  int<lower=0> T;
  vector[T] c_t;
}
parameters {
  // Growth rate hyper parameters
  real mu;
  real<lower=0> sigma_g;
  // Disaster effect hyper-parameters
  real<lower=0> sigma_s;
  real<lower=0> sigma_l;
  real<lower=0, upper=1> rho_s;
  real<lower=0, upper=1> rho_l;
}
model {
  // Define the latent variable and noise vectors
  vector[T] s_t;
  vector[T] l_t;
  vector[T] eps_t;
  // *** Model  ***
  s_t[1] = sigma_s * eps_t[1];
  l_t[1] = -sigma_l * eps_t[1];
  for (t in 2:T) {
    s_t[t] = rho_s * s_t[t-1] + sigma_s * eps_t[t];
    l_t[t] = rho_l * l_t[t-1] - sigma_l * eps_t[t];
    c_t[t] ~ normal(mu + s_t[t] + l_t[t], sigma_g);
  }
  // Shocks
  eps_t ~ normal(0, 1);
  // *** Priors ***
  mu ~ normal(0,2);
  sigma_g ~ cauchy(0, 2.5);
  sigma_s ~ cauchy(0, 2.5);
  sigma_l ~ cauchy(0, 2.5);
  rho_s ~ normal(0.5, 0.5);
  rho_l ~ normal(0.5, 0.5);
}

Which yields a RuntimeError:
"RuntimeError: Exception during call to services function: `ValueError(“Initialization failed. Rejecting initial value: Error evaluating the log probability at the initial value. Exception: normal_lpdf: Location parameter is nan, but must be finite!”
From what I read on this forum, this could be solved by defining bounds when initializing my variables. I have done this insofar as possible. What could be driving this problem? I don’t see where this trivial model would be undefined for the parameters given.

In the construction here:

  vector[T] s_t;
  vector[T] l_t;
  vector[T] eps_t;
  // *** Model  ***
  s_t[1] = sigma_s * eps_t[1];
  l_t[1] = -sigma_l * eps_t[1];
  for (t in 2:T) {
    s_t[t] = rho_s * s_t[t-1] + sigma_s * eps_t[t];
    l_t[t] = rho_l * l_t[t-1] - sigma_l * eps_t[t];
    c_t[t] ~ normal(mu + s_t[t] + l_t[t], sigma_g);
  }

You’ve declared the vector eps_t, but you haven’t assigned any values to it. This means that all elements of eps_tare still nan and all operations involving eps_t then result in nan

Thanks for the swift response.

When I reorder my code and move the declaration of the \varepsilon_t distribution up, I still get that same error message.

model {
  // Define the latent variable and noise vectors
  vector[T] s_t;
  vector[T] l_t;
  vector[T] eps_t;
  // *** Model  ***
  // Shocks
  eps_t ~ normal(0, 1);
  // Latent variables
  s_t[1] = sigma_s * eps_t[1];
  l_t[1] = -sigma_l * eps_t[1];
  for (t in 2:T) {
    s_t[t] = rho_s * s_t[t-1] + sigma_s * eps_t[t];
    l_t[t] = rho_l * l_t[t-1] - sigma_l * eps_t[t];
    c_t[t] ~ normal(mu + s_t[t] + l_t[t], sigma_g);
  }

Overall, the reference manual and user guide have been convenient. However, I also noticed that I could rewrite this model using “transformed parameters” instead of putting it all in “model”. Are there some parts I should move there for this to start working?

vector[T] eps_t;
  // *** Model  ***
  // Shocks
  eps_t ~ normal(0, 1);

If you want to give something a prior, you have to declare it in parameters, not model

Thanks a lot. I’m new to this Bayesian estimation method and interpreting these noise terms as having a prior escaped my mind.