Convert real to int

how can I convert a real to an int. I want to round

int x;
x = a * b;
where a and b and reals

You can’t. Also, you don’t want to if a and b are unknown as it would mess up NUTS.

Are you trying to do some kind of interpolation problem? That’s the only example we’ve seen where this would be useful and not violate continuous differentiability for the posterior. But we’ve never gotten around to building in an interpolation.

1 Like

Actually I wanted x to be the size of a new variable

real xx[x];

Could you use a work around like this:

Just keep in mind that the posterior should be continuously differentiable. You can write an inefficient linear function to truncate or a more efficient logarithmetic one with binary search, but however you do it, it’s going to break differentiability w.r.t. the integer.

You can also allocate things that are larger and only use some of them if it’s just a placeholder. But no way to get it the right size without hacking.

What about if you wanted to do it in the generated quantities. I was trying to calculate the saturated likelihood for a Poisson model. I was just trying to do something like.

real saturated_ll = poisson_lpmf(y| y);

but I get the error,

No matches for: 
      poisson_lpmf(int[ ], int[ ])

any quick suggestions for this calculation?

The second command is the mean of the Poisson. Stan expects its to be real (or vector) not int.

This is the signature from the doc: real poisson_lpmf(ints n | reals lambda).

If you want the posterior predictive, put the mean in the second parameter.

Sorry I should have been clearer. I was more just giving an example that I had come across where I wanted to convert an int[ ] to a real[ ] that wasn’t related to the differentiation.