Hi all, I am trying to model two time-series using an autoregressive order 1 model. Here is my code:
model_code = '
data{
int<lower=1> T; //Time
int<lower=2> K; //location
matrix[T,K] y; //Target time series
}
parameters {
real alpha;
real<lower=0> sigma;
vector[K] theta;
}
transformed parameters {
matrix[T,K] epsilon; //Residuals
matrix[T,K] mu; //Mean of the process
for(k in 1:K){
for (t in 1:T)
epsilon[t,k] = y[t,k] - mu[t,k] ;
}
for(k in 1:K){
for (t in 2:T)
mu[t,k] = alpha + theta[k] * epsilon[t - 1,k] ;
}
}
model{
alpha ~ normal(0,3);
sigma ~ normal(0, 5);
for(k in 1:K){
theta[k] ~ normal(0, 3);
}
for(k in 1:K){
mu[1,k] ~ normal(7,1);
}
for(k in 1:K){
for (t in 2:T)
y[t,k] ~ normal(mu[t,k], sigma);
}
}
'
I am getting this message after running:
" **DIAGNOSTIC(S) FROM PARSER:**
**Info:**
**Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.**
**If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.**
**Left-hand-side of sampling statement:**
** mu[[1, k]] ~ normal(...)**
**SAMPLING FOR MODEL '5961ab62c5b1362766f998204e5c7a75' NOW (CHAIN 1).**
**Chain 1: Rejecting initial value:**
**Chain 1: Error evaluating the log probability at the initial value.**
**Chain 1: Exception: normal_lpdf: Random variable is nan, but must not be nan! (in 'model1d26231bed03_5961ab62c5b1362766f998204e5c7a75' at line 36)"**
I am not sure what’s the problem and how to solve it. I would appreciate your helps.
In this case that warning is a false alarm. The parser isn’t smart enough to see that it’s only the first row of mu on which you’re putting a prior, and although all the other rows involve transforms of some other parameters, that row does not. However, your model is going to break because at no point do you assign any values to that row. What you want to do is:
parameters{
...
row_vector[k] mu_t1 ;
}
transformed_parameters{
...
matrix[T,K] mu; //Mean of the process
mu[1,] = mu_t1 ;
...
}
model{
...
mu_t1 ~ normal(7,1) ; // no need to loop, normal() is vectorized
...
}
Also note that when expressing the likelihood you’re skipping t=1, but that will leave mu_t1 completely unconstrained by data, so I think you want to have the inner loop as for (t in 1:T)
Thanks, I followed your suggestions and the warning disappeared doing:
model_code = '
data{
int<lower=1> T; //Time
int<lower=2> K; //location
matrix[T,K] y; //Target time series
}
parameters {
real alpha;
real<lower=0> sigma;
vector[K] theta;
row_vector[K] mu_t1 ;
}
transformed parameters {
matrix[T,K] epsilon; //Residuals
matrix[T,K] mu; //Mean of the process
mu[1,] = mu_t1 ;
for(k in 1:K){
for (t in 1:T)
epsilon[t,k] = y[t,k] - mu[t,k] ;
}
for(k in 1:K){
for (t in 2:T)
mu[t,k] = alpha + theta[k] * epsilon[t - 1,k] ;
}
}
model{
alpha ~ normal(0,3);
sigma ~ normal(0, 5);
for(k in 1:K){
theta[k] ~ normal(0, 3);
}
mu_t1 ~ normal(7,1) ;
for(k in 1:K){
for (t in 2:T)
y[t,k] ~ normal(mu[t,k], sigma);
}
}
'
However, the second Error didn’t go away and I am still getting this:
SAMPLING FOR MODEL 'dc8ed46f4cac1fe091ee98c8ff00ea39' NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Location parameter is nan, but must be finite! (in 'model1d26f3516e5_dc8ed46f4cac1fe091ee98c8ff00ea39' at line 41)
You need to put the computation of epsilon and my together in the same set of loops. Each is initialized full of NaNs when declared, so if you refer to an element of one before putting a value in that element, it’ll yield NaN.