• Tidak ada hasil yang ditemukan

Approximations and error analysis

Dalam dokumen Annotated Algorithms in Python (Halaman 146-150)

Acknowledgments

4.2 Approximations and error analysis

Consider a physical quantity, for example, the length of a nail. Given one nail, we can measure its length by choosing a measuring instrument.

Whatever instrument we choose, we will be able to measure the length of the nail within the resolution of the instrument. For example, with a tape measure with a resolution of1mm, we will only be able to determine the

length of the nail within1mm of resolution. Repeated measurements per- formed at different times, by different people, using different instruments may bring different results. We can choose a more precise instrument, but it would not change the fact that different measures will bring different values compatible with the resolution of the instrument. Eventually one will have to face the fact that there may not be such a thing as the length of a nail. For example, the length varies with the temperature and the details of how the measurement is performed. In fact, a nail (as every- thing else) is made out of atoms, which are made of protons, neutrons, and electrons, which determine an electromagnetic cloud that fluctuates in space and time and depends on the surrounding objects and interacts with the instrument of measure. The length of the nail is the result of a measure.

For each measure there is a result, but the results of multiple measure- ments are not identical. The results of many measurements performed with the same resolution can be summarized in a distribution of results.

This distribution will have a mean ¯xand a standard deviationδx, which we call uncertainty. From now on, unless otherwise specified, we assume that the distribution of results is Gaussian so that ¯xcan be interpreted as the mean andδx as the standard deviation.

Now let us consider a system that, given an inputx, produces the output y; x and y are physical quantities that we can measure, although only with a finite resolution. We can model the system with a function f such thaty= f(x)and, in general, f is not known.

We have to make various approximations:

• We can replace the “true” value for the input with our best estimate, ¯x, and its associated uncertainty,δx.

• We can replace the “true” value for the output with our best estimate,

¯

y, and its associated uncertainty,δy.

• Even if we know there is a “true” function f describing the system, our implementation for the function is always an approximation, ¯f. In fact, we may not have a single approximation but a series of approxi-

mations of increasing precision, fn, which become more and more ac- curate (usually) asn increases. If we are lucky, up to precision errors, asnincreases, our approximations will become closer and closer to f, but this will take an infinite amount of time. We have to stop at some finiten.

With the preceding definition, we can define the following types of errors:

Data error: the difference betweenxand ¯x.

Computational error: the difference between ¯f(x¯) and y. Computa- tional error includes two parts systematic error and statistical error.

Statistical error: due to the fact that, often, the computation of ¯f(x) = limn→fn(x)is too computationally expensive and we must approxi- mate ¯f(x)with fn(x). This error can be estimated and controlled.

Systematic error: due to the fact that ¯f(x) = limn→fn(x) 6= f(x). This is for two reasons: modeling errors (we do not know f(x)) and rounding errors (we do not implement f(x) with arbitrary precision arithmetics).

Total error: defined as the computational error + the propagated data error and in a formula:

δy=|f(x¯)−fn(x¯)|+|fn0(x¯)|δx (4.3) The first term is the computational error (we use fn instead of the true f), and the second term is the propagated data error (δx, the uncer- tainty inx, propagates through fn).

4.2.1 Error propagation

When a variable x has a finite Gaussian uncertainty δx, how does the uncertainty propagate through a function f? Assuming the uncertainty is small, we can always expand using a Taylor series:

y+δy= f(x+δx) = f(x) + f0(x)δx+O(δx2) (4.4)

And because we interpretδyas the width of the distributiony, it should be positive:

δy=|f0(x)|δx (4.5) We have used this formula before for the propagated data error. For functions of two variablesz= f(x,y)and assuming the uncertainties inx andyare independent,

δz= s

f(x,y)

x

2

δx2+

f(x,y)

y

2

δy2 (4.6) which for simple arithmetic operations reduces to

z=x+y δz=pδx2+δy2 z=x−y δz=pδx2+δy2

z=x∗y δz=|x∗y|p(δx/x)2+ (δy/y)2 z=x/y δz=|x/y|p(δx/x)2+ (δy/y)2

Notice that whenz=x−yapproaches zero, the uncertainty inzis larger than the uncertainty inxandyand can overwhelm the result. Also notice that ifz=x/yandyis small compared tox, then the uncertainty inzcan be large. Bottom line: try to avoid differences between numbers that are in proximity of each other and try to avoid dividing by small numbers.

4.2.2 buckingham

Buckingham is a Python library that implements error propagation and unit conversion. It defines a single class calledNumber, and a number ob- ject has value, an uncertainty, and a dimensionality (e.g., length, volume, mass).

Here is an example:

1 >>> from buckingham import *

2 >>> globals().update(allunits())

3 >>> L = (4 + pm(0.5)) * meter

4 >>> v = 5 * meter/second

5 >>> t = L/v

6 >>> printt)

7 (8.00 +/- 1.00)/10

8 >>> print t.units()

9 second

10 >>> print t.convert('hour')

11 (2.222 +/- 0.278)/10^4

Notice how adding an uncertainty to a numeric value with+

pm(...) or adding units to a numeric value (integer or floating point) transforms the float number into a Number object. A Number object be- haves like a floating point but propagates its uncertainty and its units.

Internally, all units are converted to the International System, unless an explicit conversion is specified.

Dalam dokumen Annotated Algorithms in Python (Halaman 146-150)