Parser Error after changing to new notation

82 views
Skip to first unread message

Ved Deshpande

unread,
Aug 18, 2016, 11:39:19 PM8/18/16
to Stan users mailing list
I recently upgraded to rstan v2.11.1 and when I ran some previously functional code, I received warnings about changing "<-" to "=", "_log" to "_lpdf" etc. I updated my code accordingly, and now I get this error:


SYNTAX ERROR, MESSAGE(S) FROM PARSER:


ERROR at line 37

 35:      logl = 0.0;
 36:      for (i in 1:n){
 37:        s = log(dCphi_du1du2_s(normal_cdf(y1[i] | mu1[i], sigma1), logistic_cdf(z[i] | mu2[i], 1), phi)) + normal_lpdf(y1[i] | mu1[i], sigma1) + logistic_lpdf(z[i] | mu2[i], 1);
                                                ^
 38:        logl = logl + s;

PARSER EXPECTED: "("
Error in stanc(file = file, model_code = model_code, model_name = model_name,  :
  failed to parse Stan model 'FB_frank' due to the above error.


Here is the offending code block (it is part of the functions block):


real copulapdf_lpdf(real[] y1, real[] z, vector mu1, vector mu2, real sigma1, real phi, int n){
  real logl;
  real s;
  logl = 0.0;
  for (i in 1:n){
    s = log(dCphi_du1du2_s(normal_cdf(y1[i] | mu1[i], sigma1), logistic_cdf(z[i] | mu2[i], 1), phi)) + normal_lpdf(y1[i] | mu1[i], sigma1) + logistic_lpdf(z[i] | mu2[i], 1);
    logl = logl + s;
  }
  return logl;
}


dCphi_du1du2_s() is another function that I have defined:


real dCphi_du2_s(real u1, real u2, real phi) {
  real b;
  real a;
  b = exp(phi*(u1+u2)) - exp(phi*(1+u2)) - exp(phi*(1+u1)) + exp(phi);
  a = -exp(phi)*(exp(phi*u1) - 1)/b;
  return a;
}


The rest of the code:

data {
  int<lower=0> n; // number of subjects
  int<lower=0> k1; // number of predictors for y1
  int<lower=0> k2; // number of predictors for y2
  real y1[n]; // continuous data
  real y2[n]; // 0/1 binary data
  matrix[n, k1] x1; // predictor variables for y1
  matrix[n, k2] x2; // predictor variables for y2
}

transformed data{
  int<lower=-1, upper=1> sign[n];
  for (i in 1:n) {
    if (y2[i]==1)
      sign[i] = 1;
    else
      sign[i] = -1;
  }
}

parameters {
  real phi; // frank copula param
  vector[k1] b1; // beta coefficients for y1
  vector[k2] b2; // beta coefficients for y2
  real<lower=0> abs_z[n]; // abs value of latent variable
  real<lower=0> sigma1; // sd for y1's normal distribution
}

transformed parameters {
  real v[n];
  vector[n] mu1; // location for y1
  vector[n] mu2; // location for z
  for (i in 1:n) {
    v[i] = sign[i] * abs_z[i];
  }
  mu1 = x1 * b1;
  mu2 = x2 * b2;
}

model {
  b1 ~ normal(0, 100);
  b2 ~ normal(0, 100);
  phi ~ normal(0, 10);
  target += copulapdf_lpdf(y1, v, mu1, mu2, sigma1, phi, n);
}


Thank you in advance for the help!

Bob Carpenter

unread,
Aug 19, 2016, 8:01:16 AM8/19/16
to stan-...@googlegroups.com
That fragment looks OK to me, but we can't really diagnose
syntactic problems without the whole program.

Also, good formatting can really help understand the layout,
such as:

> s = log(dCphi_du1du2_s(normal_cdf(y1[i] | mu1[i], sigma1),
> logistic_cdf(z[i] | mu2[i], 1),
> phi))
> + normal_lpdf(y1[i] | mu1[i], sigma1)
> + logistic_lpdf(z[i] | mu2[i], 1);

It also makes it easier for others (including you in the future)
to read your code.

- Bob

Ved Deshpande

unread,
Aug 19, 2016, 10:13:59 AM8/19/16
to Stan users mailing list
Thanks Bob. Here is a MWE:

set.seed(0)
N <- 100
p <- 2
y1 <- rnorm(N)
y2 <- sample(0:1, N, replace=TRUE)
x1 <- x2 <- matrix(rnorm(N*p), N)
dat <- list(y1=y1, y2=y2, x1=x1, x2=x2, k1=p, k2=p, n=N)
fit <- stan(file = 'FB_frank.stan', data=dat, iter=500, chains=2, thin=1, warmup=100)

I have attached the stan file. You should be able to reproduce the error with this.

Thanks,
Ved
FB_frank.stan

Daniel Lee

unread,
Aug 19, 2016, 10:23:10 AM8/19/16
to stan-users mailing list
Hi Ved,

For *_cdf() functions, you'll need a comma between the first and second arguments.
If you use *_lcdf() functions, you'll use |.

If you fix that, you'll run into a problem with the lpdf function later on.


Daniel


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ved Deshpande

unread,
Aug 19, 2016, 10:45:36 AM8/19/16
to Stan users mailing list
Hi Daniel,

Indeed, after fixing the *_cdf() functions, I run into the error:

ERROR at line 70

 68:      b2 ~ normal(0, 100);
 69:      phi ~ normal(0, 10);
 70:      target += copulapdf_lpdf(y1, v, mu1, mu2, sigma1, phi, n);
                                              ^
 71:    }

PARSER EXPECTED: <probability function arguments>

which probably means that I have to put a | somewhere in copulapdf_lpdf(). Now, amongst the arguments of copulapdf_lpdf():

- y1, n are in the data block
- v, mu1 and mu2 are in the transformed parameters block
- sigma1, phi are in the parameters block

Conceptually, copulapdf_lpdf() is the bivariate probability density on y1 (which is data) and v(a latent variable). So, where should the | go?
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Daniel Lee

unread,
Aug 19, 2016, 10:48:25 AM8/19/16
to stan-users mailing list
The | is always between the first and second arguments.

You're evaluating the value the value of a density, copulapdf_lpdf(), of y1 given all the other arguments.


Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Ved Deshpande

unread,
Aug 19, 2016, 11:25:31 AM8/19/16
to Stan users mailing list
Hi Daniel,

I added the vertical bar as per your suggestion in the model block:

target += copulapdf_lpdf(y1 | v, mu1, mu2, sigma1, phi, n);

I now get this strange error:

Error in compileCode(f, code, language = language, verbose = verbose) :
  Compilation ERROR, function(s)/method(s) not created! file13504516527e.cpp: In instantiation of ‘T__ model135055e84018_FB_frank_namespace::model135055e84018_FB_frank::log_prob(std::vector<T2>&, std::vector<int>&, std::ostream*) const [with bool propto__ = true; bool jacobian__ = true; T__ = stan::math::var; std::ostream = std::basic_ostream<char>]’:
/home/ved/R/x86_64-pc-linux-gnu-library/3.3/StanHeaders/include/src/stan/model/util.hpp:108:39:   required from ‘double stan::model::log_prob_grad(const M&, std::vector<double>&, std::vector<int>&, std::vector<double>&, std::ostream*) [with bool propto = true; bool jacobian_adjust_transform = true; M = model135055e84018_FB_frank_namespace::model135055e84018_FB_frank; std::ostream = std::basic_ostream<char>]’
/home/ved/R/x86_64-pc-linux-gnu-library/3.3/rstan/include/rstan/stan_fit.hpp:1464:51:   required from ‘SEXPREC* rstan::stan_fit<Model, RNG_t>::grad_log_prob(SEXP, SEXP) [with Model = model135055e84018_FB_frank_namesp
In addition: Warning message:
running command '/usr/lib/R/bin/R CMD SHLIB file13504516527e.cpp 2> file13504516527e.cpp.err.txt' had status 1

Any clues as to what is going on?

Bob Carpenter

unread,
Aug 19, 2016, 11:34:16 AM8/19/16
to stan-...@googlegroups.com
Sorry, that's a bug with the new notation that has already
been patched on the develop branch (so it will go in the next
release).

https://github.com/stan-dev/stan/issues/1944

For now, I'm afraid you have to use the old notation.

We take these notation changes very seriously and spent a long
time thinking about the new notation, and have no plans to change
any of our notation in the future.

- Bob

Daniel Lee

unread,
Aug 19, 2016, 11:47:40 AM8/19/16
to stan-...@googlegroups.com
Thanks, Bob.
Reply all
Reply to author
Forward
0 new messages