I am a Japanese postgraduate student studying cognitive psychology.
I am looking for someone who can show me how to increase the computational speed of Stan. Details are as follows.
I am using CmdStan to perform parameter estimation in cognitive diagnostic models.
As cognitive diagnostic models assume discrete variables in the latent variables, Stan needs to eliminate the discrete latent variables using the log_sum_exp function.
In my model, the log_sum_exp function needs to be applied for each value in a 1000 x 15 matrix, which is currently handled in a double for loop. This is taking a very long time to compute.
If anyone is familiar with how to speed up the processing of the log_sum_exp function, I would be very interested in your advice.
The actual code is as follows. The last part of the model block is a double for loop and I want to improve this.
I would be very grateful for your help!
data {
int<lower=0> N;
int<lower=0> J;
int<lower=0> K;
matrix[K,J] Q;
matrix[N, 2] X;
array[J, N] int Y;
}
transformed data {
vector[2] Zeros = rep_vector(0, 2);
matrix[2,2] X_inv = inverse(X'*X);
}
parameters {
matrix[K, 2] beta;
vector<lower=0, upper=1>[J] s;
vector<lower=0, upper=1>[J] g_base;
real<lower=0> sigma;
}
transformed parameters {
vector<lower=0, upper=1>[J] g;
for (j in 1:J) {
if (g_base[j] > 1-s[j]) {
g[j] = 1-s[j];
} else {
g[j] = g_base[j];
}
}
matrix[N, K] delta;
delta = inv_logit(X*beta');
}
model {
target += -log(sigma);
for (k in 1:K) {
beta[k,] ~ multi_normal(Zeros, N*(sigma^2)*X_inv);
}
s ~ beta(2, 5);
g_base ~ beta(2, 5);
matrix[N, J] eta;
eta = exp(log(delta + 1e-8)*Q);
for (j in 1:J) {
for (n in 1:N) {
target += log_sum_exp(
log(eta[n, j]) + bernoulli_lpmf(Y[j,n] | 1-s[j]),
log(1-eta[n, j]) + bernoulli_lpmf(Y[j,n] | g[j])
);
}
}
}