7

I'm trying to think through the sensitivity of atan2 to errors in its inputs and I've run into a disconnect that I don't quite understand.

I know that you can compute the relative condition number of a differential function $f$ as:

$$\kappa(\vec{x}) = \frac{\Vert\mathbb{J}_f(\vec{x})\Vert}{\Vert f(\vec{x})\Vert/\Vert \vec{x}\Vert}$$

Where the norm on the Jacobian is the induced norm.

For $atan(x)$ you end up with simply the derivative $\frac{1}{1+x^2}$ so it works out to: $$\kappa(x) = \frac{x}{(1+x^2)atan(x)}$$

Which is very nicely behaved:

If I look at it on the unit circle and compute K(y/x), it looks well behaved too:

However, looking at atan2(y,x) = atan(y/x), you end up with:

$$\mathbb{J}(y,x) = \{\frac{-y}{x^2+y^2}, \frac{x}{x^2+y^2}\}$$

It turns out the induced $L_2$ norm for this is 1, and in my particular case, my x and y values are indeed a sine and cosine pair so $\Vert <x,y>\Vert = \sqrt{x^2+y^2} = 1$

This leaves you with $\kappa(y,x) = \frac{1}{\vert atan2(y,x)\vert}$ which is less well behaved for positive x and y near zero:

I believe the math here but I'm struggling to interpret the results in a consistent way, since atan2 is fundamentally just atan with logic to interpret the sign correctly.

Why does atan2 seem to get very sensitive near zero and atan doesn't?

Edit: Made atan2 and related equations consistently (y,x) order.

gct
  • 623
  • The reason to be concerned about relative condition number is the idea that computing $f(x)=0.002$ when the correct value is $f(x)=0.001$ is much, much worse than computing $f(x)=99.002$ when the correct value is $f(x)=99.001$. In your application, is it really any worse to say $\operatorname{atan2}(y,x)=0.001$ instead of $\operatorname{atan2}(y,x)=0$ than it is to say $\operatorname{atan2}(y,x)=\frac\pi2+0.001$ instead of $\operatorname{atan2}(y,x)=\frac\pi2$? – David K Jun 09 '22 at 02:25
  • $atan(y) = atan2(y,1)$, so $atan()$ stays away from from $(0,0)$, which is where $atan2()$ gets into trouble? – JonathanZ Jun 09 '22 at 04:19
  • 1
    I think what it all comes down to is that as $y\to 0,$ $|y|\to 0$ (for atan) whereas $|(x,y)|=1$ (for atan$2$). So atan to gets a pass because at the point where its output has a huge percentage change, its input is perceived as having an equally huge percentage change. There are implicit assumptions in there about how the numbers are to be used. – David K Jun 09 '22 at 13:27

2 Answers2

5

$$\operatorname{atan2}(y, x) = \arctan(\frac{y}{x}) + (\textit{quadrant fixup if } x < 0)$$

You're dividing by x. This is an ill-conditioned operation if x is near 0.

To formalize things a bit, take the division $q = \frac{y}{x}$ and perturb it to $\tilde{q} = \frac{y + \Delta y}{x + \Delta x}$.

$$\tilde{q} = \frac{y + \Delta y}{x + \Delta x} \frac{x - \Delta x}{x - \Delta x}$$ $$= \frac{xy - y\Delta{x} + x\Delta y - \Delta x \Delta y}{x^2 - (\Delta x)^2}$$

Let's assume that the deltas are small enough that for all practical purposes, $(\Delta x)^2 = 0$ and $\Delta x \Delta y = 0$. Then,

$$\tilde{q} \approx \frac{xy - y\Delta{x} + x\Delta y}{x^2}$$ $$= q - \frac{x\Delta y - y\Delta{x}}{x^2}$$ $$\text{error} = |q - \tilde{q}| \le \frac{1}{|x|}|\Delta y| + \frac{|y|}{x^2} |\Delta x|$$ $$\lim_{x \rightarrow 0} |q - \tilde{q}| = \infty$$

Dan
  • 19,542
  • Looking at the plot, the ill conditioning seems to be where x > 0 and y ~= 0 though, e.g. (1, 0) has a very poor condition number, and y/x is 0 at that point. From your error calculation the error should be ~dy there. I understand I'm dividing by zero so I have a pole where y = 0, but I don't understand why its different from the places where tan(x) goes to zero. – gct Jun 09 '22 at 02:02
  • Isn't your plot using relative rather than absolute error, though? – Dan Jun 09 '22 at 02:40
  • It's just a plot of the condition number itself for each – gct Jun 09 '22 at 04:15
  • Further confused since division supposedly has a condition number of 1, which also doesn't make sense to me wiki – gct Jun 09 '22 at 14:37
1

Condition number shows how relative perturbation of the argument(s) result in relative change in function value.

You show that atan is good conditioned, so any perturbation of $x$ cannot result in bigger relative perturbation of $\operatorname{atan} x$. I was suprised that for nonlinear functions $\kappa$ may be less than 1, for linear ones that is impossible.

Let's study how atan2 is sensitive to perturbation of $x$ and $y$ separately: $$ \kappa_x(y, x) = \frac{|\operatorname{atan2}(y, x+\delta x)-\operatorname{atan2}(y, x)| / |\delta x|}{|\operatorname{atan2}(y, x)| / \|(x, y)\|} = \frac{|y|}{|\operatorname{atan2}(y, x)|} $$ Mathematica defines atan2 with branch cut along $(-\infty, 0]$, so atan2 near $y = 0$, $x = 1$ behaves like $\operatorname{atan2}(y, x) \approx y$, so $\kappa_x \approx 1$.

Similarly $$ \kappa_y(y, x) = \frac{|x|}{|\operatorname{atan2}(y, x)|}. $$ Near $x = 1, y = 0$ we have $\kappa_y = |x/y| \to \infty$. There's no magic in this result. The relative perturbation of function is like $\delta y / y$, while relative perturbation of the argument is $\delta y/1$ (thanks to $x$). Sure thing $\delta y / y$ may be big, while $\delta y$ remain quite small.

The difference in $\operatorname{atan} \frac{y}{x}$ and $\operatorname{atan2}(y, x)$ the condition numbers essentially contain different $\|x\|$ in their definition, the former has $|y/x|$, while the latter has $\|(x, y)\| = 1$.

uranix
  • 7,813
  • 1
  • 23
  • 58