Hi Stan,
I am trying to set up a gaussian process regression model to predict the performance of football teams. In the model, each team has an intrinsic ability level theta that varies as a gaussian process over a number of seasons. The full code is here:
"
data {
int<lower=1> matches;
int<lower=2> teams;
int<lower=3> levels;
int<lower=1> seasons;
int<lower=1,upper=teams> home[matches,seasons];
int<lower=1,upper=teams> away[matches,seasons];
int<lower=1,upper=levels> results[matches,seasons];
int<lower=0,upper=1> div[teams,seasons];
}
parameters {
ordered[levels-1] c;
matrix[teams,seasons] theta;
real mu_rel;
real<lower=0> rho_sq;
}
model {
real gamma;
vector[levels] d;
vector[seasons] mu;
matrix[seasons,seasons] Sigma;
for (team in 1:teams){
for (i in 1:seasons){
for (j in 1:seasons){
Sigma[i,j] <- exp(-rho_sq * pow(theta[team,i] - theta[team,j],2));
}
}
for (k in 1:seasons){
if (div[team,k] == 1){
mu[k] <- 0;
}
if (div[team,k] == 0){
mu[k] <- mu_rel;
}
theta[team] ~ multi_normal(mu,Sigma);
}
}
for (season in 1:seasons){
for (match in 1:matches){
gamma <- theta[home[match,season],season] - theta[away[match,season],season];
d[1] <- 1 - inv_logit(gamma - c[1]);
for (level in 2:(levels-1))
d[level] <- inv_logit(gamma - c[level-1]) - inv_logit(gamma - c[level]);
d[levels] <- inv_logit(gamma - c[levels-1]);
results[match,season] ~ categorical(d);
}
}
}
"
The model compiles just fine, but it doesn't run appropriately. When I try to sample from it, I get the following error message:
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
stan::prob::multi_normal_log(N4stan5agrad3varE): Location parameter[2] is nan:0, but must be finite!
I kind of know where the issue is. I have a matrix with dimensions teams x seasons, and I want to sample from a multi normal for one team at a time, i.e., one row at a time. In R, for instance, one could select the first row of an a x b matrix with mat[1,]. I do not know how to do the equivalent in stan, or even whether it is possible. In any case, the problematic line of code is:
theta[team] ~ multi_normal(mu,Sigma);
theta is a matrix; I want to select the row team. mu should be a n-dimensional vector, and Sigma an n by n covariance matrix
Anyone has any ideas how to fix this?