# Signum function

Does stan have a signum function similar to the sign function in R. https://stat.ethz.ch/R-manual/R-devel/library/base/html/sign.html ?

I see an open issue here https://github.com/stan-dev/stan/issues/1556 but couldn’t find the function in stan.

``````rstan::lookup("sign")
 "no matching Stan functions"
``````

You can write you own rather easily with

``````functions {
int signnum(real x) {
return x < 0 ? -1 : x > 0;
}
}
``````

However, if you call this function on something other than what is declared in the data block of your Stan program, NUTS will likely not work well.

Thanks. I have a model where two parameters say ‘a’ and ‘c’ (vectors) need to have same signs. So wanted to incorporate same constraint in transformed parameters statement. The above function just works for real numbers. How can I implement a signum function for a vector ? (another way is to put constraints of on bounds of individual components of vectors but that would be complicated too).
This is how the parameters are generated in R for simulated data.
set.seed(1342)
k=4
c<-c(rnorm(k,sd=3))
c
 0.9713825 1.9780360 -4.5492103 -2.6743690
a <-c(sign( c ) *abs( rnorm(k, sd = 3)))
a
 0.56929856 2.63547291 -0.05253869 -0.71564625

I would do

``````parameters {
vector[K] a;
vector<lower = 0>[K] c_abs;
}
transformed parameters {
vector[K] c = c_abs;
for (k in 1:K) c[k] *= signum(a[k]);
}
``````

but it is still possible that NUTS won’t be able to deal with that well.