Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

10) Conditioning

Today:

  1. Recap from last time

  2. Condition numbers

1. Recap from last time

Last time we saw:

Exact arithmetic, correctly rounded

Take an elementary math operation ∗* (addition, subtraction, multiplication, division), and the discrete operation that our computers perform, ⊛\circledast. Then

x⊛y:=fl⁡(x∗y)x \circledast y := \operatorname{fl}(x * y)

with a relative accuracy ϵmachine\epsilon_{\text{machine}},

∣(x⊛y)−(x∗y)∣∣x∗y∣≤ϵmachine.\frac{|(x \circledast y) - (x * y)|}{|x * y|} \le \epsilon_{\text{machine}} .

How do operations compose?

Is this true?

∣((x⊛y)⊛z)−((x∗y)∗z)∣∣(x∗y)∗z∣≤?ϵmachine\frac{\Big\lvert \big((x \circledast y) \circledast z\big) - \big((x * y) * z\big) \Big\rvert}{|(x * y) * z|} \le^? \epsilon_{\text{machine}}
Loading...

Activity

  • Use Jupyter and Julia

  • Look at how fast series converge when taking only finitely many terms

  • Explore instability, as is occuring for large negative x above, but not for the standard library expm1

(6.1030424788918156e-6, 9.357622968840175e-14)
11-element Vector{Int64}: 1 2 4 8 16 32 64 128 256 512 1024
Loading...
Loading...

What happened? Let’s look at the terms for positive and negative xx

sum(expterms(x)) - exp(x) = 1.2092180894291739e-12
(sum(expterms(x)) - exp(x)) / exp(x) = 2.6634800885273225e-8
51-element Vector{Float64}: 1.0 -10.0 50.0 -166.66666666666669 416.66666666666674 -833.3333333333335 1388.8888888888891 -1984.1269841269846 2480.1587301587306 -2755.7319223985896 2755.7319223985896 -2505.2108385441725 2087.6756987868107 ⋮ -4.902469756513546e-8 1.2256174391283866e-8 -2.9893108271424062e-9 7.117406731291443e-10 -1.655210867742196e-10 3.761842881232264e-11 -8.359650847182808e-12 1.81731540156148e-12 -3.866628513960596e-13 8.055476070751242e-14 -1.64397470831658e-14 3.28794941663316e-15
exp(x) = 4.5399929762484854e-5
Loading...

Condition number

What sort of functions cause small errors to become big?

Consider a function f:X→Yf: X \to Y and define the absolute condition number

κ^=lim⁡δ→0max⁡∣δx∣<δ∣f(x+δx)−f(x)∣∣δx∣=max⁡δx∣δf∣∣δx∣.\hat\kappa = \lim_{\delta \to 0} \max_{|\delta x| < \delta} \frac{|f(x + \delta x) - f(x)|}{|\delta x|} = \max_{\delta x} \frac{|\delta f|}{|\delta x|}.

If ff is differentiable, then κ^=∣f′(x)∣\hat\kappa = |f'(x)|.

Floating point offers relative accuracy, so it’s more useful to discuss relative condition number,

κ=max⁡δx∣δf∣/∣f∣∣δx∣/∣x∣=max⁡δx[∣δf∣/∣δx∣∣f∣/∣x∣]\kappa = \max_{\delta x} \frac{|\delta f|/|f|}{|\delta x|/|x|} = \max_{\delta x} \Big[ \frac{|\delta f|/|\delta x|}{|f| / |x|} \Big]

or, if ff is differentiable,

κ=∣f′(x)∣∣x∣∣f∣.\kappa = |f'(x)| \frac{|x|}{|f|} .
Loading...

Example:

Back to f(x)=ex−1f(x) = e^x - 1

Loading...

What does it mean?

  • The function f(x)=ex−1f(x) = e^x - 1 is well-conditioned

  • The function f1(x)=exf_1(x) = e^x is well-conditioned

  • The function f2(x)=x−1f_2(x) = x - 1 is ill-conditioned for x≈1x \approx 1

The algorithm is unstable

  • f(x) = exp(x) - 1 is unstable

  • Algorithms are made from elementary operations

  • Unstable algorithms do something ill-conditioned

A stable algorithm

We used the series expansion previously.

  • accurate for small xx

  • less accurate for negative xx (see activity)

  • we could use symmetry to fix

  • inefficient because we have to sum lots of terms

Standard math libraries define a more efficient stable variant, expm1(x)=ex−1\texttt{expm1}(x) = e^x - 1.

0.0
4.248354255291589e-18
Loading...

Another example:

log⁡(1+x)\log(1 + x)

What is the condition number of f(x)=log⁡(1+x)f(x) = \log(1 + x) for x≈0x \approx 0?

Loading...
Loading...

Reliable = well-conditioned and stable algorithm

Mathematical functions f(x)f(x) can be ill-conditioned (big κ\kappa)

  • Modeling is how we turn an abstract question into a mathematical function

  • We want well-conditioned models (small κ\kappa)

  • Some systems are intrinsically sensitive (chaotic): fracture, chaotic systems, combustion

Algorithms f(x) can be unstable

  • Unreliable, though sometimes practical

  • Unstable algorithms are constructed from ill-conditioned parts

Exercise 10.1:

  • Find a function f(x)f(x) that models something you’re interested in

  • Plot its condition number κ\kappa as a function of xx

  • Plot the relative error (using single or double precision; compare using Julia’s big)

  • Is the relative error ever much bigger than κϵmachine\kappa \epsilon_{\text{machine}}?

  • If so, can you find what caused the instability in your algorithm?

Further reading: FNC Introduction

Exercise 10.2:

How big are these condition numbers?

  • f(x)=x+cf(x) = x+c

  • f(x)=cxf(x) = cx

  • f(x)=xpf(x) = x^p

  • f(x)=exf(x) = e^x

  • f(x)=log⁡xf(x) = \log x

  • f(x)=sin⁡xf(x) = \sin x