Powers lower than 1

For some reason, Stan outputs NA for powers lower than one.
This seems to be the case for both “^” and the pow() function.
I don’t know if the error happens only on my machine, but seems to persist in both RmdStan and CmdStan.
This happend when I tried to work with utility functions in stan and wrote something like
PERIODU[t] = (C[t]*1000/L[t])^(1-elasmu)-1)/(1-elasmu)-1;
I was confused as to why this was an NA as I have defined C[t], L[t] (vectors of size t) and elasmu (a constant) before. Turns out, elasmu was 1.45, giving a power that could not be processed. I have tested this using simpler expressions, such as 2^(0.45) and the error persists. I don’t know if that is a common experience or maybe a result of my coding errors.
I’d be grateful if anyone could share if that is the case on other machines too.

Hi Lukasz, welcome to the Stan forums! Powers less than 1 should work fine, so perhaps there’s something going on with your particular Stan program or installation. Before asking you to share a reproducible example so we can look at your Stan code and try running it, let’s just check a super simple example. What happens if you run the following Stan program for just a couple of iterations? It tests a fractional power and a negative power:

parameters {
  real x;
model {
  print("x = ", x, ", x^0.5 = ", x^0.5, ", x^-3 = ", x^-3);
  x ~ std_normal();

You should see something like this:

Chain 1 x = 1.40705, x^0.5 = 1.18619, x^-3 = 0.358985 
Chain 1 x = -2.02891, x^0.5 = nan, x^-3 = -0.119732 
Chain 1 x = 1.40705, x^0.5 = 1.18619, x^-3 = 0.358985 

When x is positive there shouldn’t be any nan values, but for negative values of x you should get nan for x^0.5. What happens for you?

Hi Jonah,
Thanks for helping me on this.
It works fine indeed. Turns out, the problem was caused by my misunderstanding of how the sampling functions. Thanks for clarifying that!

1 Like

Great, glad you worked it out.

I also meant to mention in my previous reply that if I had used real<lower=0> x; then there wouldn’t have been any nans at all due to the positive constraint.