# Variable type for S topology

**URL:** <https://discourse.mc-stan.org/t/variable-type-for-s-topology/2098>\
**Category:** Modeling\
**Created:** [October 3, 2017, 9:46am UTC](https://discourse.mc-stan.org/t/variable-type-for-s-topology/2098 "2017-10-03T09:46:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maedoc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/maedoc/32/205_2.png) [@maedoc](https://discourse.mc-stan.org/u/maedoc)\
**Post date:** [October 3, 2017, 9:46am UTC](https://discourse.mc-stan.org/t/variable-type-for-s-topology/2098/1 "2017-10-03T09:46:40Z")

</div>

I wanted to write a model of phase synchronization but realized that the typical simulation approach

```stan
for (t in 1:T) {
  if (theta > (2*pi))
    theta = theta - 2*pi
  theta = theta + dt * (omega + ...)
}

```

wouldn’t work well under AD because of the jump (is that correct?)

The difficulty is that `theta` is properly in S, not R. I noticed the `unit_vector` type but that would require rewriting the model in R^2 coordinates, which is awkward. Is that nevertheless the best approach 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:** [October 3, 2017, 2:30pm UTC](https://discourse.mc-stan.org/t/variable-type-for-s-topology/2098/2 "2017-10-03T14:30:35Z")

</div>

Stuff on a circle ought to be continuous but the periodicity is a problem. You can constrain a parameter `theta` to be between 0 and 2pi, in which case you don’t need the `if` statement, but in the unconstrained space 0 is negative infinity and 2pi is positive infinity, so it is hard to make a small clockwise move from slightly above 0. But you should be able to declare a `unit_vector` in the parameters block, map to `theta` in transformed parameters, and write your likelihood in terms of `theta`. Be sure that if you put your prior on `theta` that you make a Jacobian adjustment.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 3, 2017, 10:29pm UTC](https://discourse.mc-stan.org/t/variable-type-for-s-topology/2098/3 "2017-10-03T22:29:34Z")

</div>

I really think we should rename `unit_vector` to `sphere`. The only problem is that we’d want to use `K - 1` instead of `K` and then everyone who doesn’t know their spherical geometry is going to get confused.
