This is an old version, view current version.

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 \(\theta\)). Given an angle \(\theta\), a matrix can be defined, the pre-multiplication by which rotates a point by an angle of \(\theta\). For angle \(\theta\) (in two dimensions), the \(2 \times 2\) rotation matrix is defined by \[ R_{\theta} = \begin{bmatrix} \cos \theta & - \sin \theta \\ \sin \theta & \cos \theta \end{bmatrix}. \] Given a two-dimensional vector \(x\), \(R_{\theta} \, x\) is the rotation of \(x\) (around the origin) by \(\theta\) degrees.

Unit vector type

In Stan, unit vectors in \(K\) dimensions are declared as

unit_vector[K] alpha;

A unit vector has length one (meaning the sum of squared values is one, not that its number of elements is one).

Angles from unit vectors

Angles can be calculated from unit vectors. For example, a random variable theta representing an angle in \((-\pi, \pi)\) 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 \(\arctan \frac{y}{x}\) is uniform over \((-\pi, \pi]\).

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 \(-\pi\) and \(\pi\) are at \(-\infty\) and \(\infty\) 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.