11.4 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 θ. Given an angle θ, a matrix can be defined, the pre-multiplication by which rotates a point by an angle of θ. For angle θ (in two dimensions), the 2×2 rotation matrix is defined by Rθ=[cosθ−sinθsinθcosθ]. Given a two-dimensional vector x, Rθx is the rotation of x (around the origin) by θ degrees.
Angles from unit vectors
Angles can be calculated from unit vectors. For example, a random
variable theta
representing an angle in (−π,π) radians
can be declared as a two-dimensional unit vector then transformed to
an angle.
parameters {
unit_vector[2] xy;
}transformed parameters {
real<lower=-pi(), upper=pi()> theta = atan2(xy[2], xy[1]);
}
If the distribution of (x,y) is uniform over a circle, then the distribution of arctanyx is uniform over (−π,π).
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 −π and π are
at −∞ and ∞ on the unconstrained scale, which can
produce multimodal posterior distributions when the true distribution
on the circle is unimodal.
With a little additional work on the trigonometric front, the same conversion back to angles may be accomplished in more dimensions.