# Signum function

**URL:** https://discourse.mc-stan.org/t/signum-function/9156
**Category:** RStan
**Tags:** rstan
**Created:** [June 7, 2019, 6:13pm UTC](https://discourse.mc-stan.org/t/signum-function/9156 "2019-06-07T18:13:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Kay](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@Kay](https://discourse.mc-stan.org/u/Kay)
#### Post date: [June 7, 2019, 6:13pm UTC](https://discourse.mc-stan.org/t/signum-function/9156/1 "2019-06-07T18:13:25Z")

</div>

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](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](https://github.com/stan-dev/stan/issues/1556) but couldn’t find the function in stan.

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [June 7, 2019, 6:20pm UTC](https://discourse.mc-stan.org/t/signum-function/9156/2 "2019-06-07T18:20:31Z")

</div>

```
rstan::lookup("sign")
[1] "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.

---

<div class="post-metadata">

### Author: ![Kay](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@Kay](https://discourse.mc-stan.org/u/Kay)
#### Post date: [June 7, 2019, 6:59pm UTC](https://discourse.mc-stan.org/t/signum-function/9156/3 "2019-06-07T18:59:38Z")

</div>

> [@bgoodri](#):
>
> functions { int signnum(real x) { return x \< 0 ? -1 : x \> 0; } }

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  
[1] 0.9713825 1.9780360 -4.5492103 -2.6743690  
a \<-c(sign( c ) \*abs( rnorm(k, sd = 3)))  
a  
[1] 0.56929856 2.63547291 -0.05253869 -0.71564625

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [June 7, 2019, 11:38pm UTC](https://discourse.mc-stan.org/t/signum-function/9156/4 "2019-06-07T23:38:26Z")

</div>

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.
