stan-dev / stan-dev/rstan

Element-wise (vectorised) power operations do not work

Open
#913 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Stan program through rstanc() and rstudio_stanc(), focusing on each commented .^ assignment in the transformed parameters block. Trace the parser behavior for these expressions and verify that the valid element-wise power forms parse without the reported dimension or statement errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.