[Please include Stan program and accompanying data if possible]
Hello,
I’m fairly new to Stan and I’m trying to estimate a factor stochastic volatility model , but I get the following error message
> fit <- stan(file = "2DimFactor.stan",data = dat,chains = 1)
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
c++ exception (unknown reason)
I don’t know how to interpret this error. Is there something wrong with my code, is it a problem with the compiler?
I have seen this error before in other questions but I have not managed to find the solution to my problem.
Stan code for my model:
//2 dim factor model
data {
int<lower=0> n; // Sample size
int<lower=0> p; // Number of time series
matrix[p,n] y; // data
}
parameters {
vector[n] h_std; //standardized factors
matrix[p,n] x_std; //standardized idiosyncratic processes
vector[p] beta; //loading matrix (vector in this case)
int<lower=-1,upper=1> phi_h;
int<lower=0> sigma_h;
vector<lower=-1,upper=1>[p] phi_x;
vector[p] mu_x;
vector<lower = 0>[p] sigma_x;
}
transformed parameters {
matrix[p,n] x; //indiosyncratic processes
vector[n] h; // factor volatility
//Scale with standard deviation
h = h_std * sigma_h;
h[1] = h[1]/sqrt(1 - square(phi_h));
for (t in 2:n)
h[t] = h[t] + phi_h*h[t-1];
}
//Scale with standard deviation
for(t in 1:p){
for(i in 1:n)
x[t,] = x[t,]*sigma_x[t];
x[t,] = x[t,] + mu_x[t];
x[t,1] = x[t,1]/sqrt(1 - square(phi_x[t]));
}
for(t in 2:n){
for(i in 1:p){
x[i,t] = x[i,t] + phi_x(i)*(x[i,t-1] - mu_x(i));
}
}
}
model {
//Prior parameters
int B_beta = 1;
int b_mu = 0;
int B_mu = 100;
int a_0 = 20;
int b_0 = 1.5;
int B_sigma = 1;
//Covariance matrix for observations as function of parameters
matrix[p,p] Sigma;
//Prior sigma_h
target += log(2*sigma_h) + gamma_lpdf(square(sigma_h),0.5,1/(2*B_sigma)); //density of sigma if sigma^2 is gamma(0.5,0.5)
//Prior phi_h
target += log(2) + beta_lpdf(2*phi_h - 1,a0,b0); // density of phi if (1 + phi)/2 is beta(a0,b0)
for(i in 1:p){
target += log(2*sigma_x[i]) + gamma_lpdf(square(sigma_x[i]),0.5,1/(2*B_sigma)); //prior sigma_x if sigma^2 is gamma(0.5,0.5)
target += log(2) + beta_lpdf(2*phi_x[i]-1,a0,b0); //prior phi_h
mu_x[i] ~ normal(b_mu,B_mu); //prior mu
}
beta ~ normal(B_beta,1);
h_std ~ normal(0,1);
x_std[1,] ~ normal(0,1);
x_std[2,] ~ normal(0,1);
//Constribution from observations
for(i in 1:n){
Sigma[1,1] = square(beta[1])*exp(h[i]) + exp(x[1,i]);
Sigma[1,2] = beta[1]*beta[2]*exp(h[i]);
Sigma[2,2] = square(beta[2])*exp(h[i]) + exp(x[2,i]);
Sigma[2,1] = Sigma[1,2];
y[,i] ~ multi_normal(0,Sigma);
}
}
If it helps, this is what my Makevars file looks like:
# The following statements are required to use the clang4 binary
#CC=clang
#CXX=clang++
#CXX=g++-mp-4.8 -arch x86_64
#CC=gcc-mp-4.8
CC=clang
CXX=clang++
# End clan4 inclusion statements
Any tips are much appreciated!
Thanks,
Jens