Catastrophic cancellation

From HandWiki - Reading time: 7 min

Short description: Loss of precision in numerical analysis

In numerical analysis, catastrophic cancellation[1][2] is the phenomenon that subtracting good approximations to two nearby numbers may yield a very bad approximation to the difference of the original numbers.

For example, if there are two studs, one [math]\displaystyle{ L_1 = 253.5\,\text{cm} }[/math] long and the other [math]\displaystyle{ L_2 = 252.5\,\text{cm} }[/math] long, and they are measured with a ruler that is good only to the centimeter, then the approximations could come out to be [math]\displaystyle{ \tilde L_1 = 254\,\text{cm} }[/math] and [math]\displaystyle{ \tilde L_2 = 252\,\text{cm} }[/math]. These may be good approximations, in relative error, to the true lengths: the approximations are in error by less than 2% of the true lengths, [math]\displaystyle{ |L_1 - \tilde L_1|/|L_1| \lt 2\% }[/math].

However, if the approximate lengths are subtracted, the difference will be [math]\displaystyle{ \tilde L_1 - \tilde L_2 = 254\,\text{cm} - 252\,\text{cm} = 2\,\text{cm} }[/math], even though the true difference between the lengths is [math]\displaystyle{ L_1 - L_2 = 253.5\,\text{cm} - 252.5\,\text{cm} = 1\,\text{cm} }[/math]. The difference of the approximations, [math]\displaystyle{ 2\,\text{cm} }[/math], is in error by 100% of the magnitude of the difference of the true values, [math]\displaystyle{ 1\,\text{cm} }[/math].

Catastrophic cancellation isn't affected by how large the inputs are—it applies just as much to large and small inputs. It depends only on how large the difference is, and on the error of the inputs. Exactly the same error would arise by subtracting [math]\displaystyle{ 52\,\text{cm} }[/math] from [math]\displaystyle{ 54\,\text{cm} }[/math] as approximations to [math]\displaystyle{ 52.5\,\text{cm} }[/math] and [math]\displaystyle{ 53.5\,\text{cm} }[/math], or by subtracting [math]\displaystyle{ 2.00052\,\text{km} }[/math] from [math]\displaystyle{ 2.00054\,\text{km} }[/math] as approximations to [math]\displaystyle{ 2.000525\,\text{km} }[/math] and [math]\displaystyle{ 2.000535\,\text{km} }[/math].

Catastrophic cancellation may happen even if the difference is computed exactly, as in the example above—it is not a property of any particular kind of arithmetic like floating-point arithmetic; rather, it is inherent to subtraction, when the inputs are approximations themselves. Indeed, in floating-point arithmetic, when the inputs are close enough, the floating-point difference is computed exactly, by the Sterbenz lemma—there is no rounding error introduced by the floating-point subtraction operation.

Formal analysis

Formally, catastrophic cancellation happens because subtraction is ill-conditioned at nearby inputs: even if approximations [math]\displaystyle{ \tilde x = x (1 + \delta_x) }[/math] and [math]\displaystyle{ \tilde y = y (1 + \delta_y) }[/math] have small relative errors [math]\displaystyle{ |\delta_x| = |x - \tilde x|/|x| }[/math] and [math]\displaystyle{ |\delta_y| = |y - \tilde y|/|y| }[/math] from true values [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math], respectively, the relative error of the difference [math]\displaystyle{ \tilde x - \tilde y }[/math] of the approximations from the difference [math]\displaystyle{ x - y }[/math] of the true values is inversely proportional to the difference of the true values:

[math]\displaystyle{ \begin{align} \tilde x - \tilde y &= x (1 + \delta_x) - y (1 + \delta_y) = x - y + x \delta_x - y \delta_y \\ &= x - y + (x - y) \frac{x \delta_x - y \delta_y}{x - y} \\ &= (x - y) \biggr(1 + \frac{x \delta_x - y \delta_y}{x - y}\biggr). \end{align} }[/math]

Thus, the relative error of the exact difference [math]\displaystyle{ \tilde x - \tilde y }[/math] of the approximations from the difference [math]\displaystyle{ x - y }[/math] of the true values is

[math]\displaystyle{ \left|\frac{x \delta_x - y \delta_y}{x - y}\right|. }[/math]

which can be arbitrarily large if the true values [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] are close.

In numerical algorithms

Subtracting nearby numbers in floating-point arithmetic does not always cause catastrophic cancellation, or even any error—by the Sterbenz lemma, if the numbers are close enough the floating-point difference is exact. But cancellation may amplify errors in the inputs that arose from rounding in other floating-point arithmetic.

Example: Difference of squares

Given numbers [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math], the naive attempt to compute the mathematical function [math]\displaystyle{ x^2 - y^2 }[/math] by the floating-point arithmetic [math]\displaystyle{ \operatorname{fl}(\operatorname{fl}(x^2) - \operatorname{fl}(y^2)) }[/math] is subject to catastrophic cancellation when [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] are close in magnitude, because the subtraction can expose the rounding errors in the squaring. The alternative factoring [math]\displaystyle{ (x + y) (x - y) }[/math], evaluated by the floating-point arithmetic [math]\displaystyle{ \operatorname{fl}(\operatorname{fl}(x + y) \cdot \operatorname{fl}(x - y)) }[/math], avoids catastrophic cancellation because it avoids introducing rounding error leading into the subtraction.[2]

For example, if [math]\displaystyle{ x = 1 + 2^{-29} \approx 1.0000000018626451 }[/math] and [math]\displaystyle{ y = 1 + 2^{-30} \approx 1.0000000009313226 }[/math], then the true value of the difference [math]\displaystyle{ x^2 - y^2 }[/math] is [math]\displaystyle{ 2^{-29} \cdot (1 + 2^{-30} + 2^{-31}) \approx 1.8626451518330422 \times 10^{-9} }[/math]. In IEEE 754 binary64 arithmetic, evaluating the alternative factoring [math]\displaystyle{ (x + y) (x - y) }[/math] gives the correct result exactly (with no rounding), but evaluating the naive expression [math]\displaystyle{ x^2 - y^2 }[/math] gives the floating-point number [math]\displaystyle{ 2^{-29} = 1.8626451\underline{4923095703125} \times 10^{-9} }[/math], of which less than half the digits are correct and the other (underlined) digits reflect the missing terms [math]\displaystyle{ 2^{-59} + 2^{-60} }[/math], lost due to rounding when calculating the intermediate squared values.

Example: Complex arcsine

When computing the complex arcsine function, one may be tempted to use the logarithmic formula directly:

[math]\displaystyle{ \arcsin(z) = i \log \bigl(\sqrt{1 - z^2} - i z\bigr). }[/math]

However, suppose [math]\displaystyle{ z = i y }[/math] for [math]\displaystyle{ y \ll 0 }[/math]. Then [math]\displaystyle{ \sqrt{1 - z^2} \approx -y }[/math] and [math]\displaystyle{ i z = -y }[/math]; call the difference between them [math]\displaystyle{ \varepsilon }[/math]—a very small difference, nearly zero. If [math]\displaystyle{ \sqrt{1 - z^2} }[/math] is evaluated in floating-point arithmetic giving

[math]\displaystyle{ \operatorname{fl}\Bigl(\sqrt{\operatorname{fl}(1 - \operatorname{fl}(z^2))}\Bigr) = \sqrt{1 - z^2}(1 + \delta) }[/math]

with any error [math]\displaystyle{ \delta \ne 0 }[/math], where [math]\displaystyle{ \operatorname{fl}(\cdots) }[/math] denotes floating-point rounding, then computing the difference

[math]\displaystyle{ \sqrt{1 - z^2}(1 + \delta) - i z }[/math]

of two nearby numbers, both very close to [math]\displaystyle{ -y }[/math], may amplify the error [math]\displaystyle{ \delta }[/math] in one input by a factor of [math]\displaystyle{ 1/\varepsilon }[/math]—a very large factor because [math]\displaystyle{ \varepsilon }[/math] was nearly zero. For instance, if [math]\displaystyle{ z = -1234567 i }[/math], the true value of [math]\displaystyle{ \arcsin(z) }[/math] is approximately [math]\displaystyle{ -14.71937803983977i }[/math], but using the naive logarithmic formula in IEEE 754 binary64 arithmetic may give [math]\displaystyle{ -14.719\underline{644263563968}i }[/math], with only five out of sixteen digits correct and the remainder (underlined) all garbage.

In the case of [math]\displaystyle{ z = i y }[/math] for [math]\displaystyle{ y \lt 0 }[/math], using the identity [math]\displaystyle{ \arcsin(z) = -\arcsin(-z) }[/math] avoids cancellation because [math]\displaystyle{ \sqrt{1 - (-z)^2} = \sqrt{1 - z^2} \approx -y }[/math] but [math]\displaystyle{ i (-z) = -i z = y }[/math], so the subtraction is effectively addition with the same sign which does not cancel.

Example: Radix conversion

Numerical constants in software programs are often written in decimal, such as in the C fragment double x = 1.000000000000001; to declare and initialize an IEEE 754 binary64 variable named x. However, [math]\displaystyle{ 1.000000000000001 }[/math] is not a binary64 floating-point number; the nearest one, which x will be initialized to in this fragment, is [math]\displaystyle{ 1.0000000000000011102230246251565404236316680908203125 = 1 + 5\cdot 2^{-52} }[/math]. Although the radix conversion from decimal floating-point to binary floating-point only incurs a small relative error, catastrophic cancellation may amplify it into a much larger one:

double x = 1.000000000000001;  // rounded to 1 + 5*2^{-52}
double y = 1.000000000000002;  // rounded to 1 + 9*2^{-52}
double z = y - x;              // difference is exactly 4*2^{-52}

The difference [math]\displaystyle{ 1.000000000000002 - 1.000000000000001 }[/math] is [math]\displaystyle{ 0.000000000000001 = 1.0\times 10^{-15} }[/math]. The relative errors of x from [math]\displaystyle{ 1.000000000000001 }[/math] and of y from [math]\displaystyle{ 1.000000000000002 }[/math] are both below [math]\displaystyle{ 10^{-15} = 0.0000000000001\% }[/math], and the floating-point subtraction y - x is computed exactly by the Sterbenz lemma.

But even though the inputs are good approximations, and even though the subtraction is computed exactly, the difference of the approximations [math]\displaystyle{ \tilde y - \tilde x = (1 + 9\cdot 2^{-52}) - (1 + 5\cdot 2^{-52}) = 4\cdot 2^{-52} \approx 8.88\times 10^{-16} }[/math] has a relative error of over [math]\displaystyle{ 11\% }[/math] from the difference [math]\displaystyle{ 1.0\times 10^{-15} }[/math] of the original values as written in decimal: catastrophic cancellation amplified a tiny error in radix conversion into a large error in the output.

Benign cancellation

Cancellation is sometimes useful and desirable in numerical algorithms. For example, the 2Sum and Fast2Sum algorithms both rely on such cancellation after a rounding error in order to exactly compute what the error was in a floating-point addition operation as a floating-point number itself.

The function [math]\displaystyle{ \log(1 + x) }[/math], if evaluated naively at points [math]\displaystyle{ 0 \lt x \lll 1 }[/math], will lose most of the digits of [math]\displaystyle{ x }[/math] in rounding [math]\displaystyle{ \operatorname{fl}(1 + x) }[/math]. However, the function [math]\displaystyle{ \log(1 + x) }[/math] itself is well-conditioned at inputs near [math]\displaystyle{ 0 }[/math]. Rewriting it as [math]\displaystyle{ \log(1 + x) = x \frac{\log(1 + x)}{(1 + x) - 1} }[/math] exploits cancellation in [math]\displaystyle{ \hat x := \operatorname{fl}(1 + x) - 1 }[/math] to avoid the error from [math]\displaystyle{ \log(1 + x) }[/math] evaluated directly.[2] This works because the cancellation in the numerator [math]\displaystyle{ \log(\operatorname{fl}(1 + x)) = \hat x + O(\hat x^2) }[/math] and the cancellation in the denominator [math]\displaystyle{ \hat x = \operatorname{fl}(1 + x) - 1 }[/math] counteract each other; the function [math]\displaystyle{ \mu(\xi) = \log(1 + \xi)/\xi }[/math] is well-enough conditioned near zero that [math]\displaystyle{ \mu(\hat x) }[/math] gives a good approximation to [math]\displaystyle{ \mu(x) }[/math], and thus [math]\displaystyle{ x\cdot \mu(\hat x) }[/math] gives a good approximation to [math]\displaystyle{ x\cdot \mu(x) = \log(1 + x) }[/math].

References

  1. Muller, Jean-Michel; Brunie, Nicolas; de Dinechin, Florent; Jeannerod, Claude-Pierre; Joldes, Mioara; Lefèvre, Vincent; Melquiond, Guillaume; Revol, Nathalie et al. (2018). Handbook of Floating-Point Arithmetic (2nd ed.). Gewerbestrasse 11, 6330 Cham, Switzerland: Birkhäuser. p. 102. doi:10.1007/978-3-319-76526-6. ISBN 978-3-319-76525-9. https://doi.org/10.1007/978-3-319-76526-6. 
  2. 2.0 2.1 2.2 Goldberg, David (March 1991). "What every computer scientist should know about floating-point arithmetic". ACM Computing Surveys (New York, NY, United States: Association for Computing Machinery) 23 (1): 5–48. doi:10.1145/103162.103163. ISSN 0360-0300. https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html. Retrieved 2020-09-17. 




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Catastrophic_cancellation
9 views | Status: cached on August 17 2024 15:57:47
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF