I am trying to implement a straightforward Bayesian MDS model, based on the code from Gronau and Lee. The code is here and the relevant file is the demcmc_all_dim.cpp file. However, I’m running into a weird error with non-finite gradients and I’m wondering if anyone on this forum might have any ideas for how to fix it. I’ve copied my current code here – I know there are several optimizations that could be made with Stan but for now I’m hewing as close to the original C code as possible.
data {
int nDimensions;
int nStimuli;
int nSubjects;
}
transformed data {
int n_zero = nDimensions * (nDimensions + 1) / 2;
int n_nonzero = nStimuli * nDimensions - n_zero;
int n_pos = nDimensions;
int n_free = n_nonzero - n_pos;
}
parameters {
vector<lower=0, upper=1>[n_pos] xpos;
vector<lower=-1, upper=1>[n_free] xfree;
real<lower=0> sdS;
}
transformed parameters {
matrix[nStimuli, nDimensions] x;
// compute distances
matrix[nStimuli, nStimuli] d;
{
int i_free = 1;
int i_pos = 1;
for (i in 1:nStimuli) {
for (j in 1:nDimensions) {
if (i <= j) {
x[i,j] = 0;
} else if (i == j + 1) {
x[i,j] = xpos[i_pos];
i_pos += 1;
} else {
x[i,j] = xfree[i_free];
i_free += 1;
}
}
}
}
//the following block seems to be the issue
for (i in 1:nStimuli) {
for (j in 1:nStimuli) {
real tmp = 0.0;
for (k in 1:nDimensions) {
tmp += pow(x[i,k] - x[j,k], 2);
}
d[i, j] = sqrt(tmp);
}
}
}
model {
}
and the following code should allow for reproducing the error:
library(cmdstanr)
data <- list(nDimensions = 2,
nStimuli = 9,
nSubjects = 20)
mds_gronau_lee_test_cmdstan_model <- cmdstan_model(file.path(model_dir, "mds_gronau_lee_test.stan"))
mdsres.fixed.gronau.lee <- mds_gronau_lee_test_cmdstan_model$sample(data = data,
chains = 1,
iter_warmup = 1,
iter_sampling = 1)
I don’t really understand where the issue with the gradient could possibly come from since there is actually no likelihood evaluation! The parameters themselves are simply constrained parameters and I’m not sure I understand why the transformed parameters block would make a difference as is. For what it’s worth, the issue seems to be the block I’ve commented in the code. If I comment it out or set
d[i, j] = 1;
, there is no sampling issue. Thanks in advance for the help!