Stan - divide integer by a vector

Hello

This seems like a very basic question but I’ve been trying different solutions and nothing has worked so far.

I have a vector of some parameters:

parameters{
vector<lower=0>[n] pars;
}

In the transformed_parameters block, I am trying to make a vector of reciprocal values of those parameters for use in further computations. Here are a couple of things I tried:

transformed_parameters{
vector<lower=0>[n] rec_pars;
rec_pars = 1/pars;
}

This results in an error: No matches for: Available argument signatures for operator /: Expression is ill formed. Adding a dot . before the / creates the same error.

I’ve also tried:

transformed_parameters{
vector<lower=0>[n] rec_pars;
rec_pars = operator/(1, pars);
}

However, this tells me Variable ‘operator’ does not exist.. Adding a dot before the slash still does the same thing.

Lastly, I’ve tried

transformed_parameters{
vector<lower=0>[n] rec_pars = 1/pars;
}

And this tells me again that there are no available argument signatures. So I don’t really understand how can I perform an operation as simple as this.

I think 1.0 / pars should do it

Unfortunately no, it still tells me that there are no available signatures.

Huh. How about rep_vector(1.0,n) ./ pars ?

3 Likes

Yes this works! So, it’s not possible to divide a single number by a vector, you have to divide vector by a vector.

Alternatively, pars .^ (-1)

1 Like

The simplest approach here is the inv function:

vector<lower=0>[n] rec_pars = inv(pars);
3 Likes