Understanding the transformation underlying unit_vector[2]

Dear Community,

I’m trying to square section 11.3 and 11.4 in the manual but need some help.

Section 11.4 11.4 Unit vectors and rotations | Stan User’s Guide
tells me to represent the variable as unit_vector[2] and then use atan2 of the components to represent the angle.

Section 11.3 11.3 Transforming to unconstrained parameters | Stan User’s Guide tells me that the transformation in question (from the real line to the unit circle) is using an auxiliary variable approach described in Marsaglia (1972).

Following the link of Marsaglia (1972), he describes how this works for a sphere in 3 and 4 dimension, but I fail to understand how the method works for a unit circle (and the general case).

If somebody please could elaborate on how this transformation works, and point me to the relevant implementation in the source code, I would be very grateful.

Best regards

1 Like

The unit vector constraint source code is here: math/unit_vector_constrain.hpp at develop · stan-dev/math · GitHub

Where the constraint is implemented by dividing each element of the vector by the square-root of the vector norm. In Stan code, this is equivalent to:

vector[2] unconstrained = ...;
vector[2] constrained = unconstrained / sqrt(dot_self(unconstrained));