# Ordered vector uniform in angle

**URL:** <https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932>\
**Category:** Modeling\
**Created:** [December 11, 2018, 6:15pm UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932 "2018-12-11T18:15:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grburgess](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/grburgess/32/2010_2.png) [@grburgess](https://discourse.mc-stan.org/u/grburgess)\
**Post date:** [December 11, 2018, 6:15pm UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932/1 "2018-12-11T18:15:24Z")

</div>

I need to sample angle uniformly which is simple enough:

```stan
parameters {

vector<lower=0, upper=1>[N] angle_raw;

}

transformed parameters {

vector[N] angle;

angle = acos(1-angle_raw); // will be uniformly distributed between 0 and pi/2

}

```

However, if I need the angle to be ordered, I lose the ability to place constraints. So, following the manual:

```stan
parameters {

ordered angle_rawer[N];

}

transformed parameters {

vector<lower=0, upper=1>[N] angle_raw;
vector[N] angle;

angle_raw = inv_logit(angle_rawer);

angle = acos(1-angle_raw); // will be uniformly distributed between 0 and pi/2

}

model {

angle_raw ~ uniform(0,1);

target += log(angle_raw) + log(1 - angle_raw); // jacobian adjustment. 

}

```

Is this the correct way to handle this?

---

<div class="post-metadata">

**Author:** ![germannp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/germannp/32/3795_2.png) [@germannp](https://discourse.mc-stan.org/u/germannp)\
**Post date:** [December 13, 2018, 9:14am UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932/2 "2018-12-13T09:14:22Z")

</div>

Sorry, no idea what the correct way is, just noticed the following line which might be a problem:

> [@grburgess](#):
>
> vector\<lower= **0** , upper= **0** \>[N] angle\_raw;

---

<div class="post-metadata">

**Author:** ![grburgess](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/grburgess/32/2010_2.png) [@grburgess](https://discourse.mc-stan.org/u/grburgess)\
**Post date:** [December 13, 2018, 10:38am UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932/3 "2018-12-13T10:38:40Z")

</div>

Oops. That was just a typo in the example! Thanks!

---

<div class="post-metadata">

**Author:** ![grburgess](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/grburgess/32/2010_2.png) [@grburgess](https://discourse.mc-stan.org/u/grburgess)\
**Post date:** [December 13, 2018, 4:26pm UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932/4 "2018-12-13T16:26:00Z")

</div>

Ok, after hunting a bit (and understanding), I think I might have a solution. While I’m not sure if it works in the bigger model I’m building, I have tested a bit and I think it is the right path.

Starting from here: [https://groups.google.com/forum/#!topic/stan-users/04GSu-ql3vM](https://groups.google.com/forum/#!topic/stan-users/04GSu-ql3vM)

I tested this with a little python code to make sure the general idea worked:

```python
def simplex_sample(n):
    """
    sample from an n-dim unit simplex
    """
    sample = np.random.uniform(0,1,size=n-1)
    sample = np.append(sample,1.)
    sample.sort()
    sample = np.append(0,sample)
    
    diffs = np.diff(sample)
    
    return diffs

def ordered_interval(n, a = 0, b=1.):
    """
    sample an ordered (distinguishable) interval
    """
    
    simplex = simplex_sample(n+1)
    
    return a+ np.cumsum(simplex)[:n]*(b-a)

```

Which yields:

```python

sample = ordered_interval(n=10000, a = 0, b= 1)

len(np.unique(sample)) # it equals 10000 

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/9/93608c41320af909b4a84e186af993f1d1a7bad3.png)

And then I transform this to variable uniform in angle:

```python

ang = np.rad2deg(np.arccos(1-sample))

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/6/6b6a52e54fc434faff2a15a25aa96e322d58e01f.png)

Thus, the method _should_ produce an ordered angle uniform in angle.

The stan code should be:

```stan
parameters {

simplex angle_rawer[N+1];

}

transformed parameters {

postive_ordered[N] angle_raw;
vector[N] angle;

angle_raw = head( cumulative_sum(angle_rawer), N );

angle = acos(1-angle_raw); // will be uniformly distributed between 0 and pi/2 in angle

}

model {

angle_raw ~ uniform(0,1);

}

```

And no jacobian adjustments needed (I believe! )

---

<div class="post-metadata">

**Author:** ![grburgess](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/grburgess/32/2010_2.png) [@grburgess](https://discourse.mc-stan.org/u/grburgess)\
**Post date:** [December 14, 2018, 8:40am UTC](https://discourse.mc-stan.org/t/ordered-vector-uniform-in-angle/6932/5 "2018-12-14T08:40:30Z")

</div>

Update: This works great in the bigger model.
