Directions, Rotations, and Hyperspheres
Directional statistics involve data and/or parameters that are constrained to be directions. The set of directions forms a sphere, the geometry of which is not smoothly mappable to that of a Euclidean space because you can move around a sphere and come back to where you started. This is why it is impossible to make a map of the globe on a flat piece of paper where all points that are close to each other on the globe are close to each other on the flat map. The fundamental problem is easy to visualize in two dimensions, because as you move around a circle, you wind up back where you started. In other words, 0 degrees and 360 degrees (equivalently, 0 and
Stan supports directional statistics by providing a unit-vector data type, the values of which determine points on a hypersphere (circle in two dimensions, sphere in three dimensions).
Unit vectors
The length of a vector
With a variable declaration such as
unit_vector[K] x;
the value of x
will be constrained to be a vector of size K
with unit length; the reference manual chapter on constrained parameter transforms provides precise definitions.
Warning: An extra term gets added to the log density to ensure the distribution on unit vectors is proper. This is not a problem in practice, but it may lead to misunderstandings of the target log density output (lp__
in some interfaces). The underlying source of the problem is that a unit vector of size
Circles, spheres, and hyperspheres
An
Even though
Even though
Like a bounded interval
Transforming to unconstrained parameters
Stan (inverse) transforms arbitrary points in
The problem with this mapping is that it’s many to one; any point lying on a vector out of the origin is projected to the same point on the surface of the sphere. Muller (1959) introduced an auxiliary variable interpretation of this mapping that provides the desired properties of uniformity; the reference manual contains the precise definitions used in the chapter on constrained parameter transforms.
Warning: undefined at zero!
The above mapping from
Unit vectors and rotations
Unit vectors correspond directly to angles and thus to rotations. This is easy to see in two dimensions, where a point on a circle determines a compass direction, or equivalently, an angle
Angles from unit vectors
Angles can be calculated from unit vectors. For example, a random variable theta
representing an angle in
parameters {
unit_vector[2] xy;
}transformed parameters {
real<lower=-pi(), upper=pi()> theta = atan2(xy[2], xy[1]);
}
If the distribution of
It might be tempting to try to just declare theta
directly as a parameter with the lower and upper bound constraint as given above. The drawback to this approach is that the values
With a little additional work on the trigonometric front, the same conversion back to angles may be accomplished in more dimensions.
Circular representations of days and years
A 24-hour clock naturally represents the progression of time through the day, moving from midnight to noon and back again in one rotation. A point on a circle divided into 24 hours is thus a natural representation for the time of day. Similarly, years cycle through the seasons and return to the season from which they started.
In human affairs, temporal effects often arise by convention. These can be modeled directly with ad-hoc predictors for holidays and weekends, or with data normalization back to natural scales for daylight savings time.