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

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?

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.

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.