The ErrorNum class (basics)

A Japanese version is here.

Please follow this instruction to get my software on your computer.

Proper treatment of the errors is essential in data analysis. Sometimes it is troublesome to deal with error for every value you compute in your code. This Matlab class helps your data and error analysis a little easy. Take a look at the following example. (Empty lines are omitted.)

% you know two values with an error
>> en1 = ErrorNum(5, 0.3)
en1 = 
ErrorNum:
5.0 ± 0.3
>> en2 = ErrorNum(2, 0.2)
en2 = 
ErrorNum:
2.0 ± 0.2
% You can do simple arithmetics on these values.
>> en1 + en2
ans = 
ErrorNum:
7.0 ± 0.4
>> en1 * en2
ans = 
ErrorNum:
10.0 ± 1.2
>> en1 / en2
ans = 
ErrorNum:
2.5 ± 0.3

These calculations obey basic error propagation rules. The results are shown using proper significant figures that are inferred from the error, which is the leading order of the error except when the leading order is 1.

% You should not worry about values much smaller than the error.
>> ErrorNum(1.2531, 0.2)
ans = 
ErrorNum:
1.3 ± 0.2
>> ErrorNum(1.2531, 0.02)
ans = 
ErrorNum:
1.25 ± 0.02
>> ErrorNum(1.2531, 0.002)
ans = 
ErrorNum:
1.253 ± 0.002

This will avoid a typical mistake of a science beginner who unnecessarily reports a 10-digit number for every result.

The ErrorNum can also be a vector or matrix. Currently, it only does the element-wise calculation.

>> m1 = ErrorNum([5, 4; 3, 2], 0.3 * ones(2, 2))
ans = 
ErrorNum:
5.0 ± 0.3,  4.0 ± 0.3
3.0 ± 0.3,  2.0 ± 0.3
>> m1 * en1
ans = 
ErrorNum:
25 ± 2,  20.0 ± 1.9
15.0 ± 1.7,  10.0 ± 1.6

You can also run basic functions like sum or mean on the ErrorNum matrix.

>> mean(m1)
ans = 
ErrorNum:
4.0 ± 0.2,  3.0 ± 0.2
>> sum(m1, 2) % summing direction also works!
ans = 
ErrorNum:
9.0 ± 0.4
5.0 ± 0.4

If you have some data, you can create ErrorNum out of it. It calculates the mean and the standard error of the mean (SEM) automatically.

% Note that you have to specify the direction to calculate.
>> data1 = [5.3, 5.2, 5.1, 5.8, 5.5]; ErrorNum.create(data1, 2)
ans = 
ErrorNum:
5.38 ± 0.12
>> data2 = ErrorNum.create(randn(30, 5) + 5, 1)
data2 = 
ErrorNum:
4.76 ± 0.15,  4.60 ± 0.18,  4.9 ± 0.2,  4.99 ± 0.17,  5.14 ± 0.19

In the next post, I will explain the advanced usage of the ErrorNum class.

Author: Shinya

I'm a Scientist at Allen Institute. I'm developing a biophysically realistic model of the primary visual cortex of the mouse. Formerly, I was a postdoc at University of California, Santa Cruz. I received Ph.D. in Physics at Indiana University, Bloomington. This blog is my personal activity and does not represent opinions of the institution where I belong to.