Element-wise (vectorised) power operations do not work
Summary:
Element-wise (vectorised) power operations do not work
Description:
According to the stan functions reference, it should be possible to use .^ to do various element-wise power operations. However, rstudio_stanc(), rstanc(), and the alike come up with parsing errors in many cases.
Reproducible Steps:
The following stan program illustrates the problem. Any one of the commented out assignments for z in the transformed parameters block should be valid according to the stan functions reference, but the first four are coming up with different parser errors for me.
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real x;
}
transformed parameters{
vector[N] z;
// According to the Stan Function Reference (SFR), RHS should evaluate to vector for
// all these, but parser says RHS is real.
//z = x .^ y;
//z = y .^ x;
//z = y .^ 2.0;
//This should work according to the SFR but parser says ; is missing???
//z = y .^ y;
//element-wise multiplication and division seem to work
//z = x ./ y; //no parser error
//z = y ./ x; //no parser error -- though should be the same as y/x?
//z = y .* y; //no parser error
}
model {
y ~ normal(z, 1);
}
Current Output:
For z = x .^ y; I get the following error
Dimension mismatch in assignment; variable name = z, type = vector; right-hand side type = real.
Illegal statement beginning with non-void expression parsed as
z
Not a legal assignment, sampling, or function statement. Note that
* Assignment statements only allow variables (with optional indexes) on the left;
* Sampling statements allow arbitrary value-denoting expressions on the left.
* Functions used as statements must be declared to have void returns
error in 'modelad434da0cc83_TestScript' at line 11, column 2
-------------------------------------------------
9: transformed parameters{
10: vector[N] z;
11: z = x .^ y;
^
12: //z = y .^ x;
-------------------------------------------------
PARSER EXPECTED: <one of the following:
a variable declaration, beginning with type
(int, real, vector, row_vector, matrix, unit_vector,
simplex, ordered, positive_ordered,
corr_matrix, cov_matrix,
cholesky_corr, cholesky_cov
or a <statement>
or '}' to close variable declarations and definitions>
Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model 'TestScript' due to the above error.
RStan Version:
2.21.2
R Version:
4.0.3 (2020-10-10)
Operating System:
OS X 10.13.6
Hi Angus, that documentation is for Stan 2.26 whereas RStan is (currently) only at 2.21, which does not have the elementwise pow operator yet.
You have two options if you want to use the elementwise pow. You can install the experimental version of RStan 2.26 via:
remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
With the caveat that stability is *not* guaranteed.
Alternatively, you can use cmdstanR: https://mc-stan.org/cmdstanr/articles/cmdstanr.html, which uses the latest version of Stan
Thanks Andrew! Explains a lot. Are the version numbers consistent across RStan, PyStan and Stan? i.e. does RStan 2.26 implement Stan 2.26 etc? If this is the convention perhaps this could be make clear out somewhere with high visibility
A suggestion for the Stan team: Whatever the version number convention, something like the following might help make sure people are looking at the right documentation:
-
When you load the RStan package in R, the initial print to screen could include a link to the relevant versions of the documentation for Stan?
-
Online documentation for Stan could include a banner or something explaining which version of RStan/PyStan etc. the documentation is relevant for?