Simple Model Running Slowly in Stan

Hi everyone. It’s important to mention that I’m very new to Stan. I’m following this tutorial: http://nbviewer.jupyter.org/github/QuantEcon/QuantEcon.notebooks/blob/master/IntroToStan_basics_workflow.ipynb

The tutorial includes two regression models, one with a coefficient restricted to be non-negative, and with an additional parameter (student t degrees of freedom). I have the code running just fine, and the first regression model (quite standard) gets 2000 iterations done in less than 2 seconds. The second model, with the restricted coefficient, takes around 50 seconds. I found this surprising because a) it doesn’t seem like it should be much different than the first model, given that they are being run in identical contexts (machine, session, compiler, etc.) and b) the elapsed time is given in the tutorial I’m following for the author’s machine. The difference between his two elapsed times is around 3 seconds, not 48. Any ideas about why this might be happening?

The stan code for the second model follows.

UPDATE: I realized the first model runs quite slowly if I comment out rstan_options(auto_write = TRUE). This is extra confusing, because I figured that would only save on compile time, but it’s slow during sampling too.

data {
  int N; // number of observations
  int K; // number of covariates
  matrix[N, K] X; //covariate matrix
  vector[N] y; //outcome vector
}
parameters {
  // Two betas, restricted and unrestricted. We'll join these in the next block
  real<lower = 0> beta_1;
  vector[K-1] beta_2; // the regression coefficients
  real<lower = 0> sigma; // the residual scale (note that it's restricted to be non-negative)
  real<lower = 0> nu; 
}
transformed parameters {
  vector[K] beta;
  beta = append_row(rep_vector(beta_1, 1), beta_2);
}
model {
  // Define the priors
  beta ~ normal(0, 5); // same prior for all betas; first will automatically have prior N+(0, 5)
  sigma ~ cauchy(0, 2.5);
  nu ~ cauchy(7, 5);

  // The likelihood
  y ~ student_t(nu, X * beta, sigma);
}
generated quantities {
  // For model comparison, we'll want to keep the likelihood contribution of each point
  vector[N] log_lik;
  vector[N] y_sim;
  for(n in 1:N){
    log_lik[n] = student_t_lpdf(y[n] | nu, X[n,]*beta, sigma);
    y_sim[n] = student_t_rng(nu, X[n, ]*beta, sigma);
  }
}

I had edited Makevars to suppress compiler warnings. This was slowing down compilation, and that was getting included in the Elapsed Time. I’m not well-versed enough with C++ compilers to understand what’s going on. Anyway, once I removed the line to suppress compiler warnings, everything is fast.

To suppress warnings without hurting compiler speed, I found this: https://groups.google.com/forum/#!msg/stan-users/nNYn-WBuWEc/eQl6iELqBAAJ

Adding -g0 to CXXFLAGS solved my problem, though I still don’t know what’s happening.

1 Like