Which is more efficient in Stan?

Viewed 477

Is it more efficient to declare a transformed parameter in the transformed parameters block or in the model block if I do not require the transformed parameter to be returned in the output.

For example

data {
  real<lower=0> tt[N]; 
  ...
}
parameters {
  real <lower = 0> mu;
  real <lower = 0> eta;
  ...
}
transformed parameters{
  real Mu[N];
  for(i in 1:N){
    Mu[i] = eta + mu*log(tt[i]);
  }
  ...
}

Compared to

data {
  real<lower=0> tt[N]; 
  ...
}
parameters {
  real <lower = 0> mu;
  real <lower = 0> eta;
  ...
}
model{
  real Mu[N];
  for(i in 1:N){
    Mu[i] = eta + mu*log(tt[i]);
  }
  ...
}

Does it take more run time in order to store all of the values of Mu in the first block of code (when I declare Mu in the transformed parameter block)?

1 Answers

The efficiency (with respect to time) is essentially the same. If you declare something in the transformed parameters blocks it takes a usually small amount of time to validate the constraints that you declare on each transformed parameter. Basically the only situation in which the constraint-checking is noticeable is when you are forming a correlation matrix or covariance matrix, which is has to factor in order to verify that it is positive definite. In the example you gave, Mu is unconstrained so there is nothing to check. If you are not interested in the intermediate parameters, it makes sense to declare them in the model block, in which case you will save some RAM (in RStan or PyStan) by not storing all of them and perhaps a little bit of time by avoiding the corresponding I / O.

Related