# Sampling problems by using multi_normal(...)

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 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=-0.999,upper=0.999>[4] phi;
cov_matrix[8] SIGMA;
matrix[4,T] alp_t;
real<lower=0.0000001> lam_t[T];
real<lower=0.001> nu;
}

transformed parameters{
cov_matrix[4] SIGMA_0;
cov_matrix[4] SIGMA_eps;
cov_matrix[4] SIGMA_eta;

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

model {

//state-space model

SIGMA ~ inv_wishart(8,P);
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){
lam_t[j] ~ gamma(nu/2,nu/2);
y[,j] ~  multi_normal(rep_vector(0,4), diag_matrix(exp( alp_t[,j]/2 ))*SIGMA_eps/sqrt(lam_t[j]) );
}

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

nu ~ gamma(0.01,0.01);
}



[ERROR ON CONSOLE]

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: multi_normal_lpdf: Covariance matrix is not symmetric. Covariance matrix[1,2] = -1.35377, but Covariance matrix[2,1] = -1.84979  (in 'model4cd64b0970bb_sv6' at line 45)


On console, i think " y[,j] ~ multi_normal(rep_vector(0,4), diag_matrix(exp( alp_t[,j]/2 ))*SIGMA_eps/sqrt(lam_t[j]) ); " may cause this error.

And i changed this part to

" y[,j] ~ multi_normal(rep_vector(0,4), diag_matrix(exp( alp_t[,j]/2 )) ); " ,

or

" y[,j] ~ multi_normal(rep_vector(0,4), SIGMA_eps/sqrt(lam_t[j]) ); "

, i didnt get any error. So, i think “diag_matrix(exp( alp_t[,j]/2 ))” and “SIGMA_eps/sqrt(lam_t[j])” are recognized symmetric.

I cant understand when “diag_matrix(exp( alp_t[,j]/2 ))” and “SIGMA_eps/sqrt(lam_t[j])” are recognized symmetric, but these multiplication is asymmetric.

A diagonal matrix times a symmetric matrix is not necessarily symmetric:

\begin{bmatrix} 1 & 0\\ 0 & 2 \end{bmatrix} \begin{bmatrix} 1 & 1\\ 1 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 1\\ 2 & 2 \end{bmatrix}

I’ve definitely made that mistake before :D.

edit: edited to not sound sarcastic

Oh, I was insane… Thank you so, so much!