We often take the arithmetic mean, or average to be quite straightforward concept in mathematics. For example, the average of two numbers, 1 and 3 is (1 + 3)/2 = 2. But what about taking the average of two rotations? The answer is not so straightforward, but I'll explain it in this article. However, before diving into averaging rotations, let's first discuss what a rotation is in the context of this article.
Consider the blue arrow above, which is pointing in a top-right direction. There are many ways to represent the direction of this arrow. For example, it can be represented using the angle rotated in a counter clockwise direction from the horizontal, or using another angle such as in a clockwise direction from the horizontal. In fact, there are infinitely many angles representing the rotation of this arrow. For the purpose of this article, when I refer to a rotation or a heading, then I mean the "arrow direction" (i.e., think "top-right"), and when I refer to an angle, then I mean the number parametrizing the rotation or heading (e.g., ).
Reiterating the point above, the same arrow's rotation or heading can be represented using infinitely many angles (e.g., , where is an integer). However, the heading is often represented using an angle in a Euclidean subspace of length . For example, or .
Let's go back to the first question posed at the beginning of the article: what is the average of two rotations? To get some insight into the problem, we'll start by looking at an example.
Consider the blue arrow pointing to the left in the figure above and assume there are two of them (i.e., one laying on top of the other). Then, the average of the two rotations (i.e., the two blue arrows) should be the same (i.e., an arrow pointing to the left). But, what about the average of the two parametrizations? Let's make this interesting by assuming the two rotations are parametrized using the angles and . The arithmetic mean of these two angles is , which is a wrong answer; it's an arrow pointing to the right, which is shown as the yellow arrow in the figure above.
To further demonstrate the point, consider averaging the same rotations but now using a different angles. Specifically, consider replacing with , which is still the same rotation represented using the angle . The arithmetic mean is then , which is a correct answer. Hmmm... what's going on here?
The examples above demonstrate how the heading representation (i.e., its parameterization) affects the "arithmetic angle average". In this article, we'll dig into the reason behind this behaviour and then a solution is presented that solves the "rotation averaging" problem. The answer uses the mathematics of Lie groups, which are special manifolds that are often used in robotics [1], especially when describing rotations. The subject of Lie groups is somewhat abstract and was difficult to learn when I was first introduced to the subject. My attempt in this article is to motivate the usage of Lie groups through the example of rotation averaging. I hope you enjoy the journey.
The first challenge in addressing the rotation averaging problem is to address how a rotation is parametrized. That is, how can a rotation be represented. One of the parametrizations was discussed earlier in this post, which uses real numbers (i.e., ). An issue with this parametrization is that the such parametrization is not unique. That is, the same rotation can be represented using different angles. Why is the non-uniqueness an issue in this case?
The reason that the non-uniqueness of a parametrization may cause issues is related to the notation of distance. For example, given two angles and , then the distance from to is . However, the two angles represent the same rotation, so the distance should actually .
The notion of distance is important as many areas of mathematics rely on it. For example, the notion of distance always appears in calculus, which in turn is used all over smooth optimization theory, which is used in many engineering applications. As such, having the correct notion of distance is important when using such mathematical tools.
The problem of non-unique parameterization may be dealt with by defining the headings to belong to a continuous subset of length . For example, the subset . Such parametrizations do solve the non-uniqueness problems and are often used in practice.
However, such parametrization is not a vector space, which is a small setback. Specifically, the set is not closed under addition or scaling. That is, given two headings and , then , in general.
The reason that this is a setback is because many algorithms rely on linear algebra, which assume that the variables belong to a vector space. For example, many numerical optimization algorithms (e.g., Newton's method) rely on such assumption.
This setback will not be an issue if there is a way to keep using the mathematical tools developed using linear algebra while still using the bounded number line . One way to do this is to exploit a surjective mapping from the real number line to the bounded set . This option is explored using complex numbers.
A different way to represent heading is by representing a point on a unit circle, which is a more natural way to represent headings. This way, if two points lie on the same location on the unit circle then they have the same heading and parametrization.
The complex unit circle
is an elegant way to represent headings. It addresses the parameterization uniqueness issue previously discussed, where a heading is uniquely represented using a single complex number . The notation is used to denote that it's a one-dimensional sphere [1].
The unit circle is not a vector space, which is the same issue presented when using the set . However, complex numbers tools can be used to remedy this issue. A point on the unit circle can be parameterized by
where is not a unique number. This allows us to use the linear algebra tools on and then map them to the unit circle using the mapping (2). For instance, two headings can be added since they are in a vector space and then mapped to the unit circle using the exponential map (2), which is a smooth map. For example, given two headings parameterized by , respectively. Then their added heading is
The parameter such that is computed using the inverse map, which is the logarithm map . The logarithm map does not have a unique solution because the exponential map (2) is a surjective map, which means that there's a unique mapping from to , but not necessarily the other way around. That is, there may exist two different parameters , that have the same heading . Thus, the logarithm map is instead defined as
For example, , where .
The mappings and will be used often, so explicitly defining functions representing these mappings will simplify the notation in this article.
Define the to be
and to be
where is defined to return the angle in the range .
Another useful operation is the angle-wrapping operator , which is a mapping that takes any valid angle and wraps it to the range . For example, . Mathematically, the angle-wrap function can be defined as
Now that the rotation parametrization using complex numbers is introduced, we can continue the rotation-averaging discussion. But before deriving the rotation averaging algorithm, we will first discuss and derive the arithmetic mean, then expand on the arithmetic mean to apply it on rotations.
In order to derive the rotation averaging algorithm, it helps to dig deeper into the arithmetic mean equation
which is valid for elements in a vector space (e.g., ). Deriving the arithmetic mean will provide the basis for deriving the rotation averaging algorithm.
One way to think about the mean of a set of elements , where , is that it's the number closest to all the numbers in the set, on average. But what does this statement mean mathematically? There are two points in the above statement that will help in formulating the above statement mathematically: first, the notion "closest" implies the minimum distance, so we need to define the distance on the given set; second, the notion "on average" implies reducing the distance on aggregate (i.e., on all elements), and not on a specific element.
Let's go back to the arithmetic mean to make the example more concrete. The notion of distance for the Euclidean space is defined as
Thus, the distance between the mean and the element can be defined as
where
is referred to as the error function because it's measuring the difference between the mean (i.e., the target variable) and the th element .
The mean is then the element that minimizes the total distance given by
which can be written mathematically as
where is read as the "argument of the minimum".
Since (11) is an error function, then (13) is a (linear) least squares problem. Without going into details, squaring the summands in (13) simplifies the problem. As such, the objective function (12) is modified to be
which in turn changes the optimization problem (13) to be
Equation (16) can be written in lifted form (i.e., using matrices)
where
The solution to the least squares problem (16) is [2]
which matches (8).
For further reading into least squares and optimization, [3] is a classic optimization book.
The generalization of the linear least squares problem is the nonlinear least squares, which is used in deriving the rotation-averaging equation in the next section.
The rotation averaging algorithm is an on-manifold nonlinear least squares algorithm implemented, where the manifold in this case is the unit circle . The algorithm is very similar to the arithmetic mean equation derived in the previous section, so we'll follow a similar path to derive the rotation averaging algorithm.
As previously discussed, the rotations will be parametrized using complex numbers (2). And as was done with the arithmetic mean, we need a distance metric to measure the distance between two rotations, which in this case is given by
where is the complex conjugate, and is defined in (6).
Similar to the error function introduced in (10), the distance between the mean and the th rotation is given by
where
is the error function between the mean and the th rotation .
It important to note that the error function (21) is smooth, near the minimum, in both arguments. Without the smoothness of the error function, it's not possible to use the nonlinear least squares algorithm, which is used in the following steps.
The rotation average is then the solution to the optimization problem
The optimization problem (22) looks nice in theory, but unfortunately, it's not very straightforward to solve using complex numbers. As such, we turn to parametrizing the rotations using angles, as was introduced in (2). Specifically, I will use the notation
Then, the error function (21) becomes
The optimization problem (22) becomes
The optimization problem (25) is a non-convex nonlinear least squares problem, which can be solved using the various methods such as the Gauss Newton algorithm. The Gauss Newton method is a gradient-based optimization method, which means that it requires and uses the error function Jacobian to iterate over the current solution, until convergence. As such, the Jacobian of the error function (25) is needed.
The Jacobian of the error function (25) can be computed by expanding the error function using its Taylor series approximation. Let be the operating point to linearize about, and let , where is the perturbation. Then, the error function can be expressed as
As such, the Jacobian is
The error functions can be stacked into a single column vector
and its Jacobian is given by
The Gauss Newton algorithm is an iterative algorithm given by
where is the search direction (also known as the update step), and is given by solving the system of equations
Inserting the error function (28) and the block Jacobian (29) into the system of equations (31) and solving for the search direction gives the solution
The rotation averaging equation is then
Given that the rotation averaging equation (33) is an iterative equation, it needs to be initialized using a starting guess . Due to the nonconvexity of the optimization problem, there may be local minimums that are different from the global minimum. As such, the initial value plays a huge role in determining whether the algorithm converges to a local or a global minimum.
Some examples will be introduced in the next section that shed some light on this issue.
To show the effect of the initial angle on the final result, three examples will be presented.
First, we'll present the example presented at the beginning of the article shown below, where there are two angles being averaged out: and . The angles to be averaged are colored in orange, the starting angle is colored in blue, and the computed mean is shown in yellow. As seen in the plot above, the rotation averaging algorithm returns the true average, .
To further understand how the algorithm converges, the objective function plotted against all possible angles is plotted below.
Notice that in the above plot, there's a single minima, which is at . Don't let the right- and left-sides of the plots confuse you; they refer to the same heading. Since there's a single minima for this problem, the algorithm converges to the correct solution regardless from where it starts. This is because the Gauss Newton algorithm will try to reach the bottom of the valley in the plot.
The next example demonstrates that this is not always the case. Consider the two angles and . Let's explore the first minima by starting the algorithm at , then the algorithm converges to , as shown in the plot below. This may be surprising, since we're expecting the answer to be . So, what happened?
Taking a look at the objective function plot reveals that there are actually two minimas, and the algorithm converged to the local (but not global) minima. Thinking more about the problem reveals that it's actually not that surprising that we get the answer; it's an angle that's the same distance from both and , but it's not the angle with the shortest distance (i.e., it's not the global minima).
Well, if we start the same optimization problem at an angle that is closer to the global optima, say , then the algorithm converges to the global minima as seen below.
Finally, one last example reveals that in fact there may not be a unique solution. Consider the angles , , and as shown below.
Our intuition tells us that there's no "right" answer for the average of these angles. That is, there's no single angle that is equally close to all the angles in the plot. This is better explained by looking at the objective function plot, which reveals that there are multiple global minimas. The rotation averaging algorithm simply converges to the minima closest to the starting condition. But it would have also converged to a different angle if the algorithm was initialized closer to the other angle.
This final example demonstrates that sometimes the solution may depend on the problem structure itself, which give some insight into the algorithm: the fact that the algorithm gives an answer doesn't mean that it's the only, or even the right, answer.
For the reader interested in a numeric example, check the plotting script, which uses the algorithm to compute the mean.
In this post, I used an innocent looking problem to motivate an important field of applied mathematics: optimization on manifold. This field is used in multiple robotics' disciplines such as state estimation, control, and computer vision.
Don't be scared by the word "manifold" because you've just worked with one: the unit circle. This unit circle belongs to special class of manifolds known as Lie groups, which are special manifolds that are also groups. Lie groups appear quite frequently in robotic applications, mainly because they represent rotations elegantly, both in 2D and 3D.
For readers interested in Lie groups for robotic applications, then [1] is a great introduction to the topic and for a more rigorous text, refer to [2].
If you made it this far, then you are a champion! I hope you enjoyed this post and found something useful out of it. Don't hesitate to reach out.
[1] | J. Solà, J. Deray, and D. Atchuthan, “A micro Lie theory for state estimation in robotics,” arXiv:1812.01537 [cs], Dec. 2021, Accessed: Mar. 20, 2022. |
[2] | T. D. Barfoot, State Estimation for Robotics. Cambridge, MA, USA: 774 Cambridge Univ. Press, 2017. |
[3] | J. Nocedal and S. J. Wright, Numerical optimization, 2nd ed. in Springer series in operations research. New York: Springer, 2006. |