Last time¶
Conditioning and well posedness
Relative and absolute errors
Today¶
Condition numbers
Stability of algorithms
Forward and backward error
Demo: computing volume of a polygon
using Plots
default(linewidth=4)Conditioning¶
What sort of functions cause small errors to become big?
Consider a function and define the absolute condition number
If is differentiable, then .
Floating point offers relative accuracy, so it’s more useful to discuss relative condition number,
or, if is differentiable,
f(x) = x - 1; fp(x) = 1
plot(x -> abs(fp(x)) * abs(x) / abs(f(x)), xlims=(0, 2))Back to ¶
f(x) = exp(x) - 1
fp(x) = exp(x)
plot(x -> abs(fp(x)) * abs(x) / abs(f(x)))What does it mean?¶
The function is well-conditioned
The function is well-conditioned
The function is ill-conditioned for
The algorithm is unstable¶
f(x) = exp(x) - 1is unstableAlgorithms are made from elementary operations
Unstable algorithms do something ill-conditioned
Another example ¶
What is the condition number of for ?
plot([x -> log(2 + x),
x -> log1p(1 + x)],
xlims = (-1-2e-15, -1+2e-15))cond1(x) = abs(1/(1+x) * x / log1p(x))
cond2(x) = abs(1/x * x / log(x))
plot([cond1 cond2], xlims=(-1, 2), ylims=(0, 100))Reliable = well-conditioned and stable¶
Mathematical functions can be ill-conditioned (big )¶
Modeling is how we turn an abstract question into a mathematical function
We want well-conditioned models (small )
Some systems are intrinsically sensitive: fracture, chaotic systems, combustion
Algorithms f(x) can be unstable¶
Unreliable, though sometimes practical
Unstable algorithms are constructed from ill-conditioned parts
An ill-conditioned problem from Paul Olum¶
From Surely You’re Joking, Mr. Feynman (page 113)
So Paul is walking past the lunch place and these guys are all excited. “Hey, Paul!” they call out. “Feynman’s terrific! We give him a problem that can be stated in ten seconds, and in a minute he gets the answer to 10 percent. Why don’t you give him one?” Without hardly stopping, he says, “The tangent of 10 to the 100th.” I was sunk: you have to divide by pi to 100 decimal places! It was hopeless.
tan(1e100)-0.4116229628832498tan(BigFloat("1e100", precision=400))0.4012319619908143541857543436532949583238702611292440683194415381168718098221194Forward and backward error¶
plot(tan, ylims=(-10, 10), x=LinRange(-10, 10, 1000))Read Stability: Backward error¶
For ill-conditioned functions, the best we can hope for is small backward error.
Feynman could have answered with any real number and it would have a (relative) backward error less than 10-100. All the numbers we saw on the previous slide had tiny backward error because
for some . (All real numbers can be written this way for some small .)
Stability¶
“nearly the right answer to nearly the right question”
for some that is close to
Backward Stability¶
“exactly the right answer to nearly the right question”
for some that is close to
Every backward stable algorithm is stable.
Not every stable algorithm is backward stable.
Demo: Map angle to the unit circle¶
theta = 1 .+ LinRange(0, 8e-15, 100)
scatter(cos.(theta), sin.(theta), legend=:none, aspect_ratio=:equal)GKS: Possible loss of precision in routine SET_WINDOW
theta = 0 .+ LinRange(1., 1+2e-5, 100)
mysin(t) = cos(t - (1 + 1e10)*pi/2)
plot(cos.(theta), sin.(theta), aspect_ratio=:equal)
scatter!(cos.(theta), mysin.(theta))Are we observing A=stability, B=backward stability, or C=neither?¶
The numbers do not lie exactly on the unit circles.
There does not exist a such that
Accuracy of backward stable algorithms (Theorem)¶
A backward stable algorithm for computing has relative accuracy
Backward stability is generally the best we can hope for.
In practice, it is rarely possible for a function to be backward stable when the output space is higher dimensional than the input space.
Go find some functions...¶
Find a function that models something you’re interested in
Plot its condition number as a function of
Plot the relative error (using single or double precision; compare using Julia’s
big)Is the relative error ever much bigger than ?
Can you find what caused the instability?
Share on Zulip
Further reading: FNC Introduction¶
Stability demo: Volume of a polygon¶
X = ([1 0; 2 1; 1 3; 0 1; -1 1.5; -2 -1; .5 -2; 1 0])
R(θ) = [cos(θ) -sin(θ); sin(θ) cos(θ)]
Y = X * R(deg2rad(70))' .+ [2e8 2e8]
plot(Y[:,1], Y[:,2], seriestype=:shape, aspect_ratio=:equal)using LinearAlgebra
function pvolume(X)
n = size(X, 1)
vol = sum(det(X[i:i+1, :]) / 2 for i in 1:n-1)
end
@show pvolume(Y)
[det(Y[i:i+1, :]) for i in 1:size(Y, 1)-1]
A = Y[2:3, :]
sum([A[1,1] * A[2,2], - A[1,2] * A[2,1]])
Xpvolume(Y) = 7.181508719921112
8×2 Matrix{Float64}:
1.0 0.0
2.0 1.0
1.0 3.0
0.0 1.0
-1.0 1.5
-2.0 -1.0
0.5 -2.0
1.0 0.0What happens if this polygon is translated, perhaps far away?
Why don’t we even get the first digit correct? These numbers are only 108 and so shouldn’t we get about 8 digits correct?