2023-01-25 Function errors
Contents
2023-01-25 Function errors#
Last Time#
Intro to Julia
Intro to floating point
Summing series
Today#
Conditioning and well posedness
Relative and absolute errors
Condition numbers
using Plots
default(linewidth=4, legendfontsize=12, xtickfontsize=12, ytickfontsize=12)
┌ Info: Precompiling Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80]
└ @ Base loading.jl:1664
What is floating point arithmetic?#
fuzzy arithmetic
exact arithmetic, correctly rounded
the primary focus of numerical analysis
#(0.1 + 0.2) - 0.3
2^(-53)
1.1102230246251565e-16
plot(x -> (1 + x) - 1, xlims=(-1e-15, 1e-15))
plot!(x -> x)
Machine epsilon#
We approximate real numbers with floating point arithmetic, which can only represent discrete values. In particular, there exists a largest number, which we call \(\epsilon_{\text{machine}}\), such that
The notation \(\oplus, \ominus, \odot, \oslash\) represent the elementary operation carried out in floating point arithmetic.
eps = 1
while 1 - eps != 1
eps = eps / 2
end
eps
5.551115123125783e-17
eps = 1.f0
while 1 - eps != 1
eps = eps / 2
end
eps
2.9802322f-8
Beating exp
#
Suppose we want to compute \(f(x) = e^x - 1\) for small values of \(x\).
f1(x) = exp(x) - 1
y1 = f1(1e-8)
9.99999993922529e-9
f2(x) = x + x^2/2 + x^3/6
y2 = f2(1e-8)
1.000000005e-8
Which answer is more accurate?
@show (y1 - y2) # Absolute difference
@show (y1 - y2) / y2; # Relative difference
y1 - y2 = -1.1077470910720506e-16
(y1 - y2) / y2 = -1.1077470855333152e-8
Conditioning#
We say that a mathematical function \(f(x)\) is well conditioned if small changes in \(x\) produce small changes in \(f(x)\). (What we mean by “small” will be made more precise.)
The function \(f(x)\) may represent a simple expression such as
\(f(x) := 2 x\)
\(f(x) := \sqrt{x}\)
\(f(x) := \log{x}\)
\(f(x) := x - 1\)
Conditioning#
A function may also represent something more complicated, implementable on a computer or by physical experiment.
Find the positive root of the polynomial \(t^2 + (1-x)t - x.\)
Find the eigenvectors of the matrix
\[\begin{split} A(x) = \begin{bmatrix} 1 & 1 \\ 0 & x \end{bmatrix} .\end{split}\]Find much the bridge flexes when the truck of mass \(x\) drives over it.
Find how long it takes to clean up when the toddler knocks the glass off the counter, as a function of the chair location \(x\).
Find the length of the rubber band when it finally snaps, as a function of temperature \(x\) during manufacturing.
Find the time at which the slope avalanches as a function of the wind speed \(x\) during the storm.
Find the probability that the slope avalanches in the next 48 hours as a function of the wind speed \(x\) during the storm.
Find the probability that the hurricane makes landfall as a function of the observations \(x\).
Specification#
Some of these problems are fully-specified
Others involve sophisticated models and ongoing community research problems.
In some cases, the models that are computable may incur greater uncertainty than the underlying system. In such cases, an analog experiment might produce smoother variation of the output as the problem data \(x\) are varied.
In others, the model might be better behaved than what it seeks to model.
Some of these problems may be ill-posed
Well-posedness#
A problem is said to be well-posed if
a solution exists,
the solution is unique, and
the solution depends continuously on the problem specification.
Mathematically, continuous variation in part 3 can be arbitrarily fast, but there may be measurement error, manufacturing tolerances, or incomplete specification in real-world problems, such that we need to quantify part 3. This is the role of conditioning.
Computing \(e^x\)#
function myexp(x)
sum = 1
for k in 1:100
sum += x^k / factorial(big(k))
end
return sum
end
myexp(1) - exp(1)
1.445646891729250136554224997792468345749669676277240766303534163549513721620773e-16
function myexp(x)
sum = 0
term = 1
n = 1
while sum + term != sum
sum += term
term *= x / n
n += 1
end
sum
end
myexp(1) - exp(1)
4.440892098500626e-16
How accurate is it?#
plot(myexp, xlims=(-2, 2))
plot([exp myexp], xlims=(-1e-15, 1e-15))
What’s happening?#
We’re computing \(f(x) = e^x\) for values of \(x\) near zero.
This function is well approximated by \(1 + x\).
Values of \(y\) near 1 cannot represent every value.
After rounding, the error in our computed output \(\tilde f(x)\) is of order \(\epsilon_{\text{machine}}\).
Absolute Error#
Relative Error#
Suppose I want to compute \(e^x - 1\)#
plot([x -> myexp(x) - 1 , x -> x],
xlims=(-1e-15, 1e-15))
What now?#
We’re capable of representing outputs with 16 digits of accuracy
Yet our algorithm
myexp(x) - 1
can’t find themWe can’t recover without modifying our code
Modify the code#
function myexpm1(x)
sum = 0
term = x
n = 2
while sum + term != sum
sum += term
term *= x / n
n += 1
end
sum
end
myexpm1 (generic function with 1 method)
plot(myexpm1, xlims=(-1e-15, 1e-15))
Plot relative error#
function relerror(x, f, f_ref)
fx = f(x)
fx_ref = f_ref(x)
max(abs(fx - fx_ref) / abs(fx_ref), 1e-17)
end
badexpm1(t) = exp(t) - 1
plot(x -> relerror(x, badexpm1, expm1), yscale=:log10, xrange=(-1e-15, 1e-15))
Floating point representation is relative (see float.exposed)#
Let \(\operatorname{fl}\) round to the nearest floating point number.
This also means that the relative error in representing \(x\) is small:
plot(x -> (1 + x) - 1, xlims=(-1e-15, 1e-15))
plot!(x -> x)
Exact arithmetic, correctly rounded#
Take an elementary math operation \(*\) (addition, subtraction, multiplication, division), and the discrete operation that our computers perform, \(\circledast\). Then
with a relative accuracy \(\epsilon_{\text{machine}}\),
Seems easy, how do operations compose?#
Is this true?
f(x; y=1, z=-1) = (x+y)+z # The best arbitrary numbers are 0, 1, and -1
plot(x -> abs(f(x) - x)/abs(x), xlims=(-1e-15, 1e-15))
Which operation caused the error?#
\(\texttt{tmp} = \operatorname{fl}(x + 1)\)
\(\operatorname{fl}(\texttt{tmp} - 1)\)
Use Julia’s BigFloat
@show typeof(big(.1))
@show big(.1) # Or BigFloat(.1); parsed as Float64, then promoted
@show BigFloat(".1"); # Parse directly to BigFloat
typeof(big(0.1)) = BigFloat
big(0.1) = 0.1000000000000000055511151231257827021181583404541015625
BigFloat(".1") = 0.1000000000000000000000000000000000000000000000000000000000000000000000000000002
tmp = 1e-15 + 1
tmp_big = big(1e-15) + 1 # Parse as Float64, then promote
abs(tmp - tmp_big) / abs(tmp_big)
1.102230246251563524952071662733800140614440125894379682676737388538642032894338e-16
r = tmp - 1
r_big = big(tmp) - 1
abs(r - r_big) / abs(r_big)
0.0
This last experiment shows that step 2 is exact (there is no rounding). The only approximation occurs when rounding in step 1, and yet I blame step 2 for the error.
Conditioning#
What sort of functions cause small errors to become big?
Consider a function \(f: X \to Y\) and define the absolute condition number
Floating point offers relative accuracy, so it’s more useful to discuss relative condition number,