Hello,
The following paragraph is my stan code.
I used this code for large data (N=45740).
It took very long time.
I really want to know how can I speeding up my code.
Thank you.
data {
int<lower=1> ns; // number of total locations
int<lower=1> nt; // number of data points
int<lower=1> npt; // number of temporal predictors
matrix[nt,ns] y; // observations
matrix [nt,npt] x; //temporal predictor matrix
matrix [ns,ns] dist; //cov matrix
matrix [npt,npt] Wp_b;
real mle_cop_range;
}
transformed data {
vector[nt] y_s[ns];
vector[ns] zero_vector_ns;
real <lower=0> df_b; //degrees of freedom, for inv-wishart dist for temporal.
for(s in 1:ns)
for(t in 1:nt){
y_s[s,t] = y[t,s];
df_b = npt+1;
}
for (i in 1:ns)
zero_vector_ns[i] = 0;
}
parameters {
vector <lower=0,upper=55> [ns] sigma;
// vector <lower=-1,upper=1> [ns] xi;
real <lower=-1,upper=1> xi;
real<lower=0> range_copula;
vector [ns] alphas; //intercepts, eventually vector [i]
matrix [ns,npt] betas; //Coefficients of temporal predictors
//hyperparameters
vector [npt] mu_b;
cov_matrix [npt] sigmas_b;
}
model {
matrix [npt,npt] L_b;
matrix [nt,ns] mu ;
vector [nt] mu_s[ns];
vector[nt] lpdf_s[ns];
matrix[ns,nt] lpdf;
vector[nt] cdf_s[ns];
matrix[ns,nt] cdf;
matrix[ns,ns] L_copula;
real eclp;
sigmas_b ~ inv_wishart(df_b,Wp_b);
L_b = cholesky_decompose(sigmas_b);
mu_b ~ normal(0,10);
// Priors
range_copula ~ normal(0, 100);
xi~ normal(0,0.3);
sigma ~ normal(0,100);
alphas ~ normal(0,100);
for (i in 1:ns){
betas[i] ~ multi_normal_cholesky(mu_b,L_b);
for (t in 1:nt){
mu[t,i] = alphas[i] +dot_product(betas[i],x[t]);
mu_s[i,t] = mu[t,i];
}
}
for(i in 1:ns){
lpdf_s[i] = gev_v_log_v(y_s[i], mu_s[i], sigma[i], xi);
cdf_s[i] = gev_v_cdf_v(y_s[i], mu_s[i], sigma[i], xi);
for(t in 1:nt){
lpdf[i,t] = lpdf_s[i,t];
cdf[i,t] = cdf_s[i,t];
}
}
L_copula = cholesky_decompose(dependogram_exp(dist, range_copula));
eclp = eliptical_copula_log_v(lpdf, cdf, L_copula, zero_vector_ns);
target+=(eclp);
}