#
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib as mp
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
import laUtilities as ut
import slideUtilities as sl
import demoUtilities as dm
from matplotlib import animation
from importlib import reload
from datetime import datetime
from IPython.display import Image, display_html, display, Math, HTML;
qr_setting = None
mp.rcParams['animation.html'] = 'jshtml';
One of the themes of this course will be shifting between mathematical and computational views of various concepts.
Today we need to talk about why the answers we get from computers can be different from the answers we get mathematically -- for the same question!
The root of the problem has to do with how numbers are manipulated in a computer.
In a computer we assign bit patterns to correspond to certain numbers.
We say the bit pattern is the number's representation.
For example the number '3.14' might have the representation '01000000010010001111010111000011'.
For reasons of efficiency, we use a fixed number of bits for these representations. In most computers nowadays we use 64 bits to represent a number.
Representing a real number in a computer is very complicated, much more so than representing integers (where computer arithmetic is, mostly, correct). For many decades after electronic computers were developed, there was no accepted "best" way to do this!
Eventually (in the 1980s) a widely accepted standard emerged, called IEEE-754. This is what almost all computers now use.
The style of representation used is called floating point.
Conceptually, it is similar to "scientific notation."
Except that it is encoded in binary:
In a double-precision floating point number: the sign, significand, and exponent are all contained within 64 bits (or 8 bytes).
#
display(Image("images/04-floating-point.png", width=600))
By Codekaizen (Own work) [GFDL or CC BY-SA 4.0-3.0-2.5-2.0-1.0], via Wikimedia Commons
There are three kinds of special values defined by IEEE-754:
NaN and Inf behave about as you'd expect. If you get one of these values in a computation you should be able to reason about how it happened. Note that these are values, and can be assigned to variables.
np.sqrt(-1)
<ipython-input-41-597592b72a04>:1: RuntimeWarning: invalid value encountered in sqrt np.sqrt(-1)
nan
var = np.log(0)
var
<ipython-input-42-49e412a30c50>:1: RuntimeWarning: divide by zero encountered in log var = np.log(0)
-inf
1/var
-0.0
As far as we are concerned, there is no difference between positive and negative zero. You can ignore the minus sign in front of a negative zero.
var = np.nan
var + 7
nan
var = np.inf
var + 7
inf
Because only a fixed number of bits are used, most real numbers cannot be represented exactly in a computer.
Another way of saying this is that, usually, a floating point number is an approximation of some particular real number.
Floating point numbers cannot be arbitrarily large or small. Numbers can be as large as and as small as .
There are discrete gaps between floating point numbers. For instance, the interval is represented by discrete subset:
Floats are discrete but not equidistant:
#
display(Image("images/04-scale.png", width=600))
Source: What you never wanted to know about floating point but will be forced to find out
Generally when we try to store a real number in a computer, what we wind up storing is the closest floating point number that the computer can represent.
The way to think about working with floating point (in fact, how the hardware actually does it) is:
What does "nearest" mean? Long story short, it means "round to the nearest representable value."
Let's say we have a particular real number and we represent it as a floating point value .
Then where is the amount that was rounded when represented as .
How big can be? Let's say is a number that is ever so slightly larger than 1 (or more generally, any power of two ). Our computer has two choices: round it to 1 or the next larger number.
Then must be smaller than
So as a relative error,
Numpy
will tell you the epsilon for your computer. It corresponds to the distance between 1 and the next larger number.
print(np.finfo(float).eps)
print(np.finfo(float).eps == 2**-52)
2.220446049250313e-16 True
This value is an important one to remember.
It is approximately the relative error that can be introduced any time a real number is stored in a computer.
Another way of thinking about this is that you only have about 16 digits of accuracy in a floating point number.
One implication is that if you add a large value with a small value, the small one might disappear entirely. This is called an underflow error.
a = float(10**10)
b = float(10**-10)
print(a)
print(b)
print(a - b)
a - b == a
10000000000.0 1e-10 10000000000.0
True
Another implication is that every operation introduces error. If we let denote any mathematical operation (e.g., addition, subtraction, multiplication, or division) and denote its floating point analogue, for some , where . That is, every operation of floating point arithmetic is exact up to a relative error of size at most .
As a result, it's possible to create calculations that give very wrong answers, particularly when repeating an operation many times, since each operation could amplify the error. We saw an example of this a few lectures ago.
def f(x):
if x <= 1/2:
return 2 * x
if x > 1/2:
return 2*x - 1
x = 0.2
v = np.zeros(80)
for i in range(80):
v[i] = x
x = f(x)
print("start:", v[:4])
print("end: ", v[-4:])
start: [0.2 0.4 0.8 0.6] end: [1. 1. 1. 1.]
What does all of this mean for us in practice? Since we cannot represent numbers exactly on a computer, it becomes important to know how small perturbations in the input to a problem impact the output.
Here are 3 principles to keep in mind when computing with floating point numbers.
Two floating point computations that should yield the same result mathematically, may not do so due to rounding error.
However, in general, if two numbers should be equal, the relative error of the difference in the floating point should be small.
So, instead of asking whether two floating numbers are equal, we should ask if they are close -- meaning that the relative error of their difference is small. Let's try an example.
a = 7
b = 1/10
c = 1/7
r1 = a * b * c
r2 = b * c * a
print(r1)
print(r2)
np.abs(r1-r2)/r1
0.1 0.09999999999999999
1.3877787807814457e-16
# The wrong way to check equality
print(r1 == r2)
False
In this case, both 1/70 and 0.1 cannot be stored exactly in a floating point representation.
More importantly, the rounding errors are different for the two numbers. As a result, the process of rounding 1/70 to its closest floating point representation, then multiplying by 7, yields a number whose closest floating point representation is not 0.1
However, that floating point representation is very close to 0.1.
Let's look at the difference: -1.3877787807814457e-17.
This is about .
In other words, -0.0000000000000001
Compared to 0.1, this is a very small number. The relative error abs(-0.0000000000000001 / 0.1) is about
This suggests that when a floating point calculation is not exact, the error (in a relative sense) is usually very small.
Notice also that in our example the size of the relative error is about .
Recall that the significand in IEEE-754 uses 52 bits.
Now, note that .
There's our "sixteen digits of accuracy" principle again.
# A better way to check equality
np.finfo('float')
print(np.abs(r1 - r2)/np.max([r1, r2]) < np.finfo('float').eps)
True
This test is needed often enough that numpy
has a function that implements it:
np.isclose(r1, r2)
True
The matrix version of this function is called allclose
. It applies an isclose
check for each entry in the matrix.
A = np.random.rand(2,2)
B = np.random.rand(2,2)
C = np.random.rand(2,2)
(A @ B) @ C == A @ (B @ C)
array([[False, False], [False, False]])
np.allclose((A @ B) @ C, A @ (B @ C))
True
So another way to state Rule 1 for our purposes is:
… always use np.isclose() or np.allclose() to compare floating point numbers for equality!
Next, we will generalize this idea a bit:
beyond the fact that numbers that should be equal, may not be in practice,
we can also observe that it can be hard to be accurate about the difference between two numbers that are nearly equal. This leads to the next two principles.
An ill-conditioned problem is one in which the outputs depend in a very sensitive manner on the inputs.
That is, a small change in the inputs can yield a very large change in the outputs.
The simplest example is computing . If is close to , then small changes in either make a big difference in the output. This is because 0 doesn't have a multiplicative inverse, so as and get closer then will go to infinity.
print(f'r1 is {r1}')
print(f'r2 is very close to r1')
r3 = r1 + 0.0001
print(f'r3 is 0.1001')
print(f'1/(r1 - r2) = {1/(r1 - r2)}')
print(f'1/(r3 - r2) = {1/(r3 - r2)}')
r1 is 0.1 r2 is very close to r1 r3 is 0.1001 1/(r1 - r2) = 7.205759403792794e+16 1/(r3 - r2) = 9999.999999998327
Because the inputs to your problem may not be exact, if the problem is ill-conditioned, the outputs may be wrong by a large amount.
The notion of ill-conditioning applies to matrix problems too, and in particular comes up when we solve certain problems involving matrices that are "almost uninvertible."
Here is a simple example; we will see later why this issue arises. Given a matrix , let's try to solve the two equations
The two vectors on the right hand sides are similar, so we might expect that the (one and only) solution would be close to . But sometimes this is not the case.
A = np.array([[1, 2.0000000001], [2, 4]])
b = np.array([[1],[2]])
c = np.array([[1],[2.01]])
print("x =")
print(np.linalg.solve(A, b))
print("y =")
print(np.linalg.solve(A, c)) # should be [[100 million], [-50 million]]
x = [[1.] [0.]] y = [[ 99999992.73096144] [-49999995.86298072]]
Two numbers, each with small relative error, can yield a value with large relative error if subtracted. If two numbers have errors in the 10th significant digit, but the first 8 significant digits cancel out, then all of a sudden the relative error is large.
This is easiest to see with an example.
a = 1.23456789
b = 1.2345678
trueDiff = 0.00000009
calcDiff = a - b
print("True difference:", trueDiff)
print("Calculated diff:", calcDiff,"\n")
print("Absolute error:", trueDiff-calcDiff)
print("Relative error:", (trueDiff-calcDiff) / trueDiff)
True difference: 9e-08 Calculated diff: 8.999999989711682e-08 Absolute error: 1.0288317610723888e-16 Relative error: 1.1431464011915431e-09
Here, the relative error in the inputs is on the order of , but the relative error of the output is on the order of – i.e., a million times larger.
A good summary that covers additional issues is at https://docs.python.org/2/tutorial/floatingpoint.html.