RStan2.11.1 - error while running GP example from manual

90 views
Skip to first unread message

Wiktor Soral

unread,
Jul 31, 2016, 3:44:49 PM7/31/16
to Stan users mailing list
Hi,
I've encountered a problem while trying to replicate Gaussian Process simulation from the Users Guide (p. 167). I was trying to get used to the new syntax and redo some familiar analyses the new way, and this one seems broken.
Interestingly when I change ' i==j ? 0.1 : 0.0' to old version 'if_else(i==j, 0.1, 0.0)', I'm getting warnings from the parser, but everything works fine. 
I haven't seen similar topic, so I thought I'll let you know. 

Best,
Wiktor

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] pl_PL.UTF-8/pl_PL.UTF-8/pl_PL.UTF-8/C/pl_PL.UTF-8/pl_PL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.11.1       StanHeaders_2.11.0 ggplot2_2.1.0     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6      codetools_0.2-14 grid_3.3.1       plyr_1.8.4      
 [5] gtable_0.2.0     stats4_3.3.1     scales_0.4.0     labeling_0.3    
 [9] tools_3.3.1      munsell_0.4.3    parallel_3.3.1   rsconnect_0.4.3 
[13] inline_0.3.14    colorspace_1.2-6 gridExtra_2.2.1 

simulate_gp = "
data {
  int<lower=1> N;
  real x[N];
}
transformed data {
  vector[N] mu;
  cov_matrix[N] Sigma;
  for (i in 1:N) 
    mu[i] = 0;
  for (i in 1:N) 
    for (j in 1:N)
      Sigma[i,j] = exp(-pow(x[i] - x[j],2)) 
                  + i==j ? 0.1 : 0.0;
}
parameters {
  vector[N] y;
}
model {
  y ~ multi_normal(mu,Sigma);
}
"
simulate_gp_data = list("N" = 20,
                        "x" = seq(0,1,le=20))
simulate_gp_fit = stan(model_code = simulate_gp,
                       data = simulate_gp_data)

Error : model53762d492ad3_bc5595b9f9828bdf9502aead3874622e_namespace::model53762d492ad3_bc5595b9f9828bdf9502aead3874622e: Sigma is not positive definite:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
failed to create the sampler; sampling not done

Bob Carpenter

unread,
Jul 31, 2016, 6:21:22 PM7/31/16
to stan-...@googlegroups.com
Thanks for reporting.

Can you send the whole example program?

Do you get the same result if you put parens
around it, as in the following?

(i==j ? 0.1 : 0.0)

The ternary operator binds very weakly, so it may just
be a bug in the manual code.

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

Wiktor Soral

unread,
Aug 1, 2016, 4:13:01 AM8/1/16
to Stan users mailing list
Including parens solved the issue - then it is perhaps error in the manual. Anyway thanks!
I've included the faulty example at the bottom of the previous post. Now the example look like this:

simulate_gp = "
data {
  int<lower=1> N;
  real x[N];
}
transformed data {
  vector[N] mu;
  cov_matrix[N] Sigma;
  for (i in 1:N) 
    mu[i] = 0;
  for (i in 1:N) 
    for (j in 1:N)
      Sigma[i,j] = exp(-pow(x[i] - x[j],2)) 
                  + (i==j ? 0.1 : 0.0);
}
parameters {
  vector[N] y;
}
model {
  y ~ multi_normal(mu,Sigma);
}
"

Best,
Wiktor

Bob Carpenter

unread,
Aug 1, 2016, 1:57:33 PM8/1/16
to stan-...@googlegroups.com
Thanks much for pointing this out. I'll fix this and
acknowledge your help in the next manual:

https://github.com/stan-dev/stan/issues/1868#issuecomment-236656510

- Bob

> On Jul 31, 2016, at 3:44 PM, Wiktor Soral <wiktor...@gmail.com> wrote:
>
Reply all
Reply to author
Forward
0 new messages