Parser warning from example Multivariate Probit

46 views
Skip to first unread message

Patrick Sadil

unread,
Sep 17, 2016, 1:58:54 PM9/17/16
to Stan users mailing list
Hi all,
     I'm working on a multivariate probit model. But, I'm confused because the example code from the manual produces the following warning: 

DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If so, you need to call increment_log_prob() with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
    z ~ multi_normal_cholesky(...)

What I'm trying to compile is identical to the code that lives at https://github.com/stan-dev/example-models/blob/master/misc/multivariate-probit/probit-multi.stan, excluding a replacement of the '<-' assignments to '='. A couple of questions about this warning:

1) Can I safely ignore it? 

2) If it is a false positive, what set it off? 

Thanks so for your time,
Patrick

> sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] docopt_0.4.5

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7        plyr_1.8.4         grid_3.3.1         gtable_0.2.0       stats4_3.3.1      
 [6] magrittr_1.5       StanHeaders_2.12.0 scales_0.4.0       ggplot2_2.1.0      stringi_1.1.1     
[11] tools_3.3.1        stringr_1.1.0      munsell_0.4.3      rstan_2.12.1       rsconnect_0.4.3   
[16] inline_0.3.14      colorspace_1.2-6   gridExtra_2.2.1  

Ben Goodrich

unread,
Sep 17, 2016, 2:10:19 PM9/17/16
to Stan users mailing list
On Saturday, September 17, 2016 at 1:58:54 PM UTC-4, Patrick Sadil wrote:
1) Can I safely ignore it? 

http://mc-stan.org/misc/warnings.html#parser-warnings

Does the function that fills z have a constant derivative?
 
2) If it is a false positive, what set it off? 
Left-hand-side of sampling statement:
    z ~ multi_normal_cholesky(...)

Ben

Patrick Sadil

unread,
Sep 17, 2016, 2:37:01 PM9/17/16
to Stan users mailing list
Hi Ben,
     Thanks for the quick reply. As you may have guessed that I'm pretty new to this, and I'm not sure how to provide the most useful information. 


On Saturday, September 17, 2016 at 2:10:19 PM UTC-4, Ben Goodrich wrote:
On Saturday, September 17, 2016 at 1:58:54 PM UTC-4, Patrick Sadil wrote:
1) Can I safely ignore it? 

http://mc-stan.org/misc/warnings.html#parser-warnings

Does the function that fills z have a constant derivative?

I'm sorry, but I'm not entirely sure. I think so? By 'function that fills z' do you mean the line: z ~ multi_normal_cholesky(beta_x, L_Omega); ? If so, then I'd assume so since there's nothing nonlinear about beta_x (which is just the result of a multiplication of a coefficient matrix with the design matrix). But, do you instead mean the part in the transformed parameters block: 

transformed parameters {
  vector[D] z[N];
  for (n in 1:N_pos)
    z[n_pos[n], d_pos[n]] = z_pos[n];
  for (n in 1:N_neg)
    z[n_neg[n], d_neg[n]] = z_neg[n];
}
 
where the latent variable, z, is being constructed from z_pos and z_neg (which were themselves given implicit, improper uniform priors)?

 
2) If it is a false positive, what set it off? 

Left-hand-side of sampling statement:
    z ~ multi_normal_cholesky(...)


To clarify, what I'm confused about is why the parser would have told me that there might be a non-linear transform on the left-hand side of this sampling statement when there's nothing besides the 'z' to the left of the '~'.
 
Thanks again,
Patrick 

Ben Goodrich

unread,
Sep 17, 2016, 3:39:32 PM9/17/16
to Stan users mailing list
On Saturday, September 17, 2016 at 2:37:01 PM UTC-4, Patrick Sadil wrote:
Hi Ben,
     Thanks for the quick reply. As you may have guessed that I'm pretty new to this, and I'm not sure how to provide the most useful information. 

On Saturday, September 17, 2016 at 2:10:19 PM UTC-4, Ben Goodrich wrote:
On Saturday, September 17, 2016 at 1:58:54 PM UTC-4, Patrick Sadil wrote:
1) Can I safely ignore it? 

http://mc-stan.org/misc/warnings.html#parser-warnings

Does the function that fills z have a constant derivative?

I'm sorry, but I'm not entirely sure. I think so? By 'function that fills z' do you mean the line: z ~ multi_normal_cholesky(beta_x, L_Omega); ? I

I mean the lines in the transformed parameters block.
 
But, do you instead mean the part in the transformed parameters block: 

Yes
 
To clarify, what I'm confused about is why the parser would have told me that there might be a non-linear transform on the left-hand side of this sampling statement when there's nothing besides the 'z' to the left of the '~'.

The parser does not know what is a linear transformation and what is a nonlinear transformation. All it knows is that z is defined in transformed parameters in this case (or if it is defined in the model block).

Ben

Daniel Lee

unread,
Sep 17, 2016, 4:09:22 PM9/17/16
to stan-...@googlegroups.com
@ben, the transformed parameter is just assignment. 
--
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.

Patrick Sadil

unread,
Sep 17, 2016, 5:21:34 PM9/17/16
to Stan users mailing list
Okay. So, is it accurate to summarise this as, the parser threw a warning at the sampling statement because it was being asked to perform operations on the target for a variable that was not declared in the parameters block?

Could I safely avoid the warning by instead transforming my data outside of stan? So, rather passing an N by D array, y, of 0s and 1s, I would instead give stan an N by D matrix of reals such that each 0 is replaced by a call to

> runif(n=1, min = -1, max = 0)

and each 1 is replaced by  

> runif(n = 1, min = 0, max = 1)?

Then, the sampling statement could just be:

    y ~ multi_normal_cholesky(beta_x, L_Omega);

Thanks,
Patrick

Ben Goodrich

unread,
Sep 17, 2016, 6:27:13 PM9/17/16
to Stan users mailing list
On Saturday, September 17, 2016 at 5:21:34 PM UTC-4, Patrick Sadil wrote:
Okay. So, is it accurate to summarise this as, the parser threw a warning at the sampling statement because it was being asked to perform operations on the target for a variable that was not declared in the parameters block?

Yes, although not just any operation but rather one that increments the posterior kernel in log units.
 
Could I safely avoid the warning by instead transforming my data outside of stan? So, rather passing an N by D array, y, of 0s and 1s, I would instead give stan an N by D matrix of reals such that each 0 is replaced by a call to

> runif(n=1, min = -1, max = 0)

and each 1 is replaced by  

> runif(n = 1, min = 0, max = 1)?

Then, the sampling statement could just be:

    y ~ multi_normal_cholesky(beta_x, L_Omega);
 
That would avoid the parser warning and have the side effect of estimating a completely different model that is neither a multivariate probit model nor a model where y depends on x.

It would be better to estimate the multivariate probit model and either increment the target keyword by the logarithm of the absolute value of the derivative of the transformation from z to its parent variable(s) or recognize that this derivative is a constant (indeed 1.0), in which case you would be incrementing the target keyword by a constant (indeed 0.0) which has no effect on what values are proposed for the parameters of what proposals are accepted. Better still would be to understand the principles behind a change-of-variables including the circumstances when essentially nothing is changed.

Ben

Bob Carpenter

unread,
Sep 17, 2016, 6:55:42 PM9/17/16
to stan-...@googlegroups.com
First, y ~ foo(theta) statements don't fill in y. y has
to be defined before they're called and it just increments
the log desnity.

Second, you can't move parameter transforms into the preprocessor
because they depend on parameters at every step. The transformed
parameters block is this:


transformed parameters {
vector[D] z[N];
for (n in 1:N_pos)
z[n_pos[n], d_pos[n]] <- z_pos[n];
for (n in 1:N_neg)
z[n_neg[n], d_neg[n]] <- z_neg[n];
}

And it's just defining all of z in terms of z_pos and z_neg.
So there's nothing non-linear there, so the

The sampling statement in question is just

z ~ multi_normal_cholesky(beta_x, L_Omega);

So that's OK.

- Bob

Patrick Sadil

unread,
Sep 18, 2016, 6:08:04 PM9/18/16
to Stan users mailing list
Sounds like I need to do more reading. Thanks for walking me through this.

Best,
Patrick
Reply all
Reply to author
Forward
0 new messages