I have a model where I construct a partially fixed matrix in the transformed parameters block, so that diagonal and parts of the upper diagonal matrix are estimated and others are set to -Inf. However, this seems to result in nonfinite gradient errors, although the likelihood (and priors) are always finite. Here is a toy example of the issue which results in the same error:
data {
int<lower=1> N;
real y[N];
}
parameters {
real mu;
}
transformed parameters {
vector[2] x;
x[1] = mu;
x[2] = negative_infinity();
}
model {
mu ~ normal(0, 1);
y ~ normal(x[1], 1);
}
This is of course very artificial example where the issue could be circumvented, but in my practical application, this “x” variable is input to custom likelihood computation function, where it is much easier to do the computations with full matrix containing -Inf values instead of creating custom elementwise computations (the location of -Inf values are fixed case-by-case). Edit: Maybe I should be more direct with the application I’m having: Essentially I am building a constrained transition matrix for a Markov model where some of the transition probabilities are fixed to zero (leading to -Inf in the log-scale).
It seems that this issue was also raised at Github years ago (Get "infinite gradient" error for code with finite gradient · Issue #275 · stan-dev/rstan · GitHub). Any ideas on how to deal with this?