Selecting rows/columns of a matrix

1,094 views
Skip to first unread message

Luis Usier

unread,
Jan 10, 2015, 10:58:56 PM1/10/15
to stan-...@googlegroups.com
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?

Ben Goodrich

unread,
Jan 10, 2015, 11:15:20 PM1/10/15
to stan-...@googlegroups.com
row(theta, team)

Ben

Luis Usier

unread,
Jan 10, 2015, 11:35:18 PM1/10/15
to stan-...@googlegroups.com
Thanks a lot Ben! It seems like that is not enough to solve the issue though. I still get this 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 assume that this error message means the mean vector is not actually a vector, so when stan tries to access the second element of the array it breaks down; I'm not sure though. The line where I am stuck now reads: 

row(theta,team) ~ multi_normal(mu,Sigma);

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/ShDA98wSqSw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Goodrich

unread,
Jan 11, 2015, 1:24:14 AM1/11/15
to stan-...@googlegroups.com
It's an informational message rather than an error message, which indicates a proposal overflowed. It subsequently said not to worry if it just happens a few times. So,
is there anything else that suggests the sampler is not working in your case?

Ben
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Marcus Brubaker

unread,
Jan 11, 2015, 10:38:36 AM1/11/15
to stan-...@googlegroups.com
The multi_normal sampling statement is in the wrong set of braces, it's inside the loop over k, so the values of mu aren't getting set properly.

Cheers,
Marcus


--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages