1. What is error?
Integers (when used appropriately) are exact, but floating-point numbers aren't. They're more like centerpoints of intervals, and the "true" value of the variable in question is likely to be anywhere in that interval.When you do calculations using floating-point variables, you should keep in mind that the result of the calculation is seful only if you keep in mind that other values in the interval could as easily have been used, giving you a different result. All the different likely values of the different floating-point variables could go into the calculation and give a range of result values -- an interval. Important questions: how big is the result's error interval, and how can we keep it small?
2. Sources of error
-
errors in the input data
-
measurement errors
-
roundoff errors
-
natural variation
-
roundoff errors during computation
-
truncation errors
-
summing a finite number of elements in an infinite series
-
errors from discretization
-
errors from the approximation of functions by polynomial or linear functions
-
simplifications necessary to modeling
3. Examples
3.1. Sometimes errors don't accumulate
import random
sum = 0.0
for i in range(1000):
r = 2.0 * (random.random() - 0.5)
print r,
sum += r
if i % 10 == 0:
print " ", sum
else:
print
3.2. Sometimes they do accumulate
import random import math sum = 0.0 for i in range(1000): r = 2.0 * (random.random() - 0.5) sum += math.sqrt(r**2) print sumNote that even if in the long term errors don't accumulate, in the short term they may be quite important. Always keep in mind the temporal scale, even if you're just observing how your simulation behaves.
3.3. Large relative error with subtraction of similar numbers
Say a = 10 (+/- 1), and b = 11 (+/- 1).-
a + b = 21 (+/- 2), and relative error is 2/21 = 0.95
-
a - b = -1 (+/- 2), and relative error is 2.0. That's a 200% error interval!
3.4. Adding numbers that differ greatly in magnitude
x = 1.0 for i in range(20): print "1.0 +", x, "=", 1.0 + x x /= 10.0A consequence
def test(err): return (1-(1-err))/((1-1)+err) for i in range(30): x = 10**(-i) print "It should be 1, but it's", test(x)
4. Estimating error
-
analytic methods (look them up)
-
Monte Carlo (recommended: it's just a few for-loops!)
5. Tips
-
choosing your project
-
maybe you don't need much numerical precision
-
if you do want numerical precision, you need precise (and accurate) data
-
designing your model
-
choose categories of actors carefully
-
minimize number of intercorrelated variables
-
writing code
-
avoid adding or subtracting numbers of very different magnitude
-
add
-
if necessary, multiply or divide
-
avoid raising numbers to powers
-
avoid subtraction
