11.1 Unit Vectors
The length of a vector \(x \in \mathbb{R}^K\) is given by \[ \Vert x \Vert \ = \ \sqrt{x^{\top}\,x} \ = \ \sqrt{x_1^2 + x_2^2 + \cdots + x_K^2}. \] Unit vectors are defined to be vectors of unit length (i.e., length one).
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 \(K\) has only
\(K - 1\) degrees of freedom. But there is no way to map those \(K - 1\)
degrees of freedom continuously to \(\mathbb{R}^N\)—for example, the
circle can’t be mapped continuously to a line so the limits work out,
nor can a sphere be mapped to a plane. A workaround is needed
instead. Stan’s unit vector transform uses \(K\) unconstrained
variables, then projects down to the unit hypersphere. Even though
the hypersphere is compact, the result would be an improper
distribution. To ensure the unit vector distribution is proper, each
unconstrained variable is given a “Jacobian” adjustment equal to an
independent standard normal distribution. Effectively, each dimension is
drawn standard normal, then they are together projected down to the
hypersphere to produce a unit vector. The result is a proper uniform
distribution over the hypersphere.