Could you tell me about algebra_solver


Hello, Pioneers of STAN!
I’m STAN rookie.

I posted about “closed-form equation” to this forum some days ago.

As I was taught that “you should use algebra_solver function” in this post, I rewrote my STAN code.
As my model is complicated, I tried simple model like this.

This model is supposed Gamma distribution with parameters (α, β).
As you know, mean of this distribution is α/β, so I re-parametrizationed from (α, β) to (μ, β).
And then, I introduced prior distribution of μ and β.

We succeeded if we simply executed α = μβ.
However, since I want to use algebra_solver, it looks like the following code.

  //solve function
  vector algebra_system(vector y,//y menas alpha
                      vector theta,//theta means mu and beta
                      real[] x_r,
                      int[] x_i){
    vector[1] result;
    result[1] = y[1]-theta[1]*theta[2];
    return result;

  int n;
  real Data[n];

transformed data{
  vector[1] y_guess;
  y_guess[1] = 1.0;
  real x_r[0];
  int x_i[0];

  real mu;
  real beta;

transformed parameters{
  vector[2] theta;
  theta[1] = mu;
  theta[2] = vector;
  real alpha = algebra_solver(algebra_system, y_guess, theta, x_r, x_i);
  //In fact alpha=mu*beta is enough, and in the case, compile succeeded.

  for(i in 1:n) {
    Data[i] ~ gamma(alpha, beta);
  mu ~ uniform(0, 1);
  beta ~ uniform(0, 4);

And I got the following error.


variable "real" does not exist.
  error in 'model_tutorial2' at line 22, column 7
    20:   y_guess[1] = 1.0;
    22:   real x_r[0];
    23:   int x_i[0];

Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model 'tutorial2' due to the above error.

I can’t understand the meaning of this, variable "real" does not exist.

Would you tell me something solution?
In addition, I would like you to forgive my poor English.

Thank you.


There’s probably a problem with how you’re instantiating

  real x_r[0];
  int x_i[0];

Are you trying to input an empty array? I don’t think this is the right syntax.

But also, you don’t need an algebra solver for a simple variable transformation. You know what the relationship between \alpha and \mu,\beta is so just plug that in directly. No solving necessary.


Thank you your reply!!

“you don’t need an algebra solver”
Yes, although it is right, a model I really want to run needs algebra_solver.
As I would like to check on behavior of algebra_solver, I used it in this code.

By the way, is these syntax really wrong?

real x_r[0];
int x_i[0];

These syntax are written in chapter(?) of algebra_solver of the Stan User Manual and I copied and pasted this into the code.
Nevertheless an error occurred, so I was surprised and I am being confused.

Especially about the error message variable "real" does not exist, “real” ought to be primitive data type, so I can not determine the cause of the error.


All the variable declarations must be at the top of any block. My guess is that you’ve put a statement before that definition.

It’s a lot easier to help when the full program is included.


I just saw the full program above.

Change these lines:

  vector[1] y_guess;
  y_guess[1] = 1.0;


  vector[1] y_guess = [1.0];


Yeah, what @syclik said. Except you may need

vector[1] y_guess = [1.0]'; 

with the transpose, if I recall correctly.


Yup. @jonah is absolutely right.


Oh! I get it!
I didn’t know how to use [ ]!
Surely, I know only how to use { } and I was concerned about that using { } cause an error.

Is this right?: { } makes an array, and [ ] makes a vector.

Now I am at home so I will review the manual and re-program at the laboratory tomorrow.
I really appreciate @shoshievass, @syclik, and @jonah.


Yeah, although the latter makes a row_vector unless transposed.


Today, I try this modification and compile have done!!!

…However, when I execute sampling() I got “Rejecting initial value”.
Probably, this is not syntax issue but my model issue.

I would like to identify the cause of the error by myself, but may also post to this forum.

Anyway, thank you for your kindness!

Can't I put literal of vector to function argument?