Sampling problems by using inv_wishart(...) prior

I’m a beginner at English and Rstan. So I’m sorry for my poor English and coding. When i tried to use Rstan to analyze Time series data, i got 19 warnings at compile and some error massages. Can someone help me with this error? Thanks in advance.

R version 3.5.1 / rstan ‘2.17.3’

[stan code]

data {
  int<lower=1> T;
  matrix[4,T] y;
  cov_matrix[8] P;
}

parameters {
  vector<lower=-1,upper=1>[4] phi;
  vector[4] alp_t[T];
  vector[8] eps_eta[T];
  real<lower=0.00001> lam_t[T];
  real<lower=0.00001> nu;
 }

transformed parameters{
 matrix[4,4] V_t[T];
 matrix[4,4] SIGMA_0;
 cov_matrix[8] SIGMA;
 matrix[4,4] SIGMA_eps;
 matrix[4,4] SIGMA_eta;

 
   V_t[T] = diag_matrix(exp(alp_t[T]));
 
   for(i in 1:4){
      for(j in 1:4){
          SIGMA_0[i,j] = SIGMA[i+4,j+4]/(1-phi[i]*phi[j]);
      }
    }
    for(i in 1:4){
      for(j in 1:4){
          SIGMA_eta[i,j] = SIGMA[i+4,j+4];
      }
    }
    for(i in 1:4){
      for(j in 1:4){
          SIGMA_eps[i,j] = SIGMA[i,j];
      }
    }    
}    

model {

//state-space model

 SIGMA ~ inv_wishart(8,P);
 eps_eta[T] ~ multi_normal(rep_vector(0,8),SIGMA);
 alp_t[1] ~ multi_normal(rep_vector(0,4),SIGMA_0);

 for(j in 1:T-1){
       alp_t[j+1] ~ multi_normal(diag_matrix(phi)*alp_t[j], SIGMA_eta);
    }

 for(j in 1:T){
       y[,j] ~  multi_normal(rep_vector(0,4), SIGMA_eps);
    }
    
//prior

   for(i in 1:4){
       (phi[i]+1)/2 ~ beta(20,1.5);
   }

   nu ~ gamma(0.01,0.01);
   
   lam_t[T] ~ gamma(nu/2,nu/2);
    
}


matrix P is positively defined, and i use the diag_matrix((rep_vector(1,8)) instead of P, but the result didn’t change.

[warnings]

Warning (non-fatal):
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:
    SIGMA ~ inv_wishart(...)

sv4.stan is syntactically correct.

[R]

fitchan <- stan (file = "sv4.stan",
                 data = list(y=t(fund),T=4538,P=P), 
                 iter  = 4000, 
                 chain = 1
                 )

[ERROR ON CONSOLE]

hash mismatch so recompiling; make sure Stan code ends with a blank line
...
...
Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: validate transformed params: SIGMA is not symmetric. SIGMA[1,2] = nan, but SIGMA[2,1] = nan  (in 'model2aae10fe6da9_sv4' at line 19)

Initialization between (-2, 2) failed after 100 attempts. 
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

SIGMA is a non-linear function of the parameters in your model. If you want to put a prior on it, you’ll need to do some extra stuff. This situation is outlined in Section 22.3, “Changes of Variables” in the 2.17 manual.

You might be better off parameterizing your covariance differently. Check out Section 9.13 “Multivariate Priors for Hierarchical Models” in the 2.17 manual. There’s example code showing how to parameterize something like this. It uses LKJ priors instead of inverse Wisharts, but if you aren’t stuck with inverse Wishart for a reason I’m missing, then maybe you can consider switching.

The initialization errors happen when Stan fails to find a set of parameters that satisfy all the constraints of your model. In this case at least one of your problems is apparently:

SIGMA is not symmetric. SIGMA[1,2] = nan

Which will violate the cov_matrix[8] constraints (so if you switch your parameterizations you might just avoid having to fix this problem).

Thank you for your help!! I’ll try it.