My stan code involves a sequence of matrix multiplication (to transform rotation angle vector into Givens matrix). There is error:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”
I wonder: Can Stan compute the analytical log-posterior and its gradient for such complex code?
Below are the Stan code and the print you may refer to.
Thank you.
Stan code:
functions{
matrix theta2U(real theta1, real theta_ge2, int TT){
matrix[TT,TT] U;
real cos1;
real sin1;
row_vector[TT] linear1;
row_vector[TT] linear2;
int index;
index=1;
for(j in 2:TT){
cos1=cos(theta1[j-1]);
sin1=sin(theta1[j-1]);
linear1=cos1U[1,:]-sin1U[j,:];
linear2=sin1U[1,:]+cos1U[j,:];
U[1,:]=linear1;
U[j,:]=linear2;
}
for(i in 2:(TT-1)){
for(j in (i+1):TT){
cos1=cos(theta_ge2[index]);
sin1=sin(theta_ge2[index]);
linear1=cos1U[i,:]-sin1U[j,:];
linear2=sin1U[i,:]+cos1U[j,:];
U[i,:]=linear1;
U[j,:]=linear2;
index+=1;
// print(“i=”,i,“, j=”,j,“, index=”,index);
}
}
// print(“theta1=”,theta1);
// print(“theta_ge2=”,theta_ge2);
// print(“U=”,U);
return U;
}
}
data {
int<lower=0> V; // number of data points
int<lower=0> TT; // dimensionality
int<lower=0> C; // number of GMM components
matrix[TT,V] X; // prewhitened data matrix: Notice that each column is a sample
int<lower=0,upper=C> z[V,TT];
vector<lower=0>[C] alpha_w;
real<lower=0> sd_prior_mu_prime; //mu’~N(0,sd_prior_mu_prime^2)
real<lower=0> shape_gamma_prime;
real<lower=0> scale_gamma_prime;
}
parameters {
simplex[C] w[TT];
vector[C] mu_prime[TT];
vector<lower=0>[C] sigma_prime_sq[TT];
real<lower=0,upper=2pi()> theta1[TT-1]; //theta[1,j]: j=2,…,TT
real<lower=0,upper=pi()> theta_ge2[(TT-2)(TT-1)/2]; //theta[i,j]: i=2,…,TT, j=i+1,…,TT
}
transformed parameters {
vector[C] mu[TT];
vector[C] mu_prime_prime[TT];
vector<lower=0>[C] sigma[TT];
real sd1;
matrix[TT,TT] U;for(q in 1:TT){
mu_prime_prime[q]=mu_prime[q]-sum(w[q] .* mu_prime[q]);
sd1=sqrt(sum(w[q] .* (mu_prime_prime[q] .* mu_prime_prime[q]+sigma_prime_sq[q])));
mu[q]=mu_prime_prime[q]/sd1;
sigma[q]=sqrt(sigma_prime_sq[q])/sd1;
}
U=theta2U(theta1,theta_ge2,TT);
}
model {
print(“initial lp outside for loop=”,target());
theta1~uniform(0,2*pi()); //theta[1,j]: j=2,…,TT
theta_ge2~uniform(0,pi()); //theta[i,j]: i=2,…,TT, j=i+1,…,TT
print(“lp outside for loop after theta_ge2=”,target());for(q in 1:TT){
print(“initial lp in for loop=”,target());
w[q] ~ dirichlet(alpha_w);
// print(“q=”,q);
// print(“w=”,w);
print(“lp after w=”,target());mu_prime[q] ~ normal(0,sd_prior_mu_prime); // print("mu_prime=",mu_prime); print("lp after mu'=",target()); sigma_prime_sq[q] ~ inv_gamma(shape_gamma_prime,scale_gamma_prime); // print("sigma_prime_sq=",sigma_prime_sq); print("lp after sigma'=",target()); z[,q] ~ categorical(w[q]); // target += categorical_lpmf(z | w); print("lp after z=",target()); target += normal_lpdf(U[q,]*X | mu[q][z[,q]],sigma[q][z[,q]]); // print("U[q,]=",U[q,]); // print("mu=",mu); // print("sigma=",sigma); print("Final lp=",target());
}
}
R code:
MH=sampling(object=GMM.stan.model,data=list(V=V,TT=TT,C=Ck.max,X=t(x.prewhiten),z=z.mid,
alpha_w=rep(0.02,Ck.max),sd_prior_mu_prime=2,
shape_gamma_prime=1,scale_gamma_prime=1),
init=list(list(w=array(1/Ck.max,c(TT,Ck.max)),mu_prime=array(0,c(TT,Ck.max)),
sigma_prime_sq=array(1,c(TT,Ck.max)),
theta1=rep(pi/3,TT-1),theta_ge2=rep(pi,(TT-2)*(TT-1)/2))),
iter=10, warmup=5, chains = 1, control=list(adapt_delta=0.9),verbose=FALSE)
R console output:
SAMPLING FOR MODEL ‘GMMBLCAdesign20181030_standardize_withZ_redundant_1level’ NOW (CHAIN 1).
Chain 1: initial lp outside for loop=-inf
lp outside for loop after theta_ge2=-inf
initial lp in for loop=-inf
lp after w=-inf
lp after mu’=-inf
lp after sigma’=-inf
lp after z=-inf…(these repeats many times)
Chain 1: initial lp outside for loop=-inf
lp outside for loop after theta_ge2=-inf
initial lp in for loop=-inf
lp after w=-inf
lp after mu’=-inf
lp after sigma’=-inf
lp after z=-infChain 1: Initialization between (-2, 2) failed after 100 attempts.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”