6_A
- hrafnulf13
- Oct 14, 2020
- 1 min read
Updated: Oct 20, 2020
C#
Link
In this application, I tested 2 cases: arithmetic mean and summation. In both cases the number of values "N" is equal to 100000. For the arithmetic mean, I used values: PI and E, that are defined in Math library in C#. While for summation, 0.1 and 0.3 values are used.
For the arithmetic mean, I compared naive, Knuth's [2] and Kahan's [1, 2] approaches to show that naive algorithm is not stable and losses significance. The same comparison was done in summation test, but only for naive and Kahan's algorithms.
Arithmetic mean:
Note that in this test, Kahan is used for summation and then the result is divided by "N".
PI = 3.1415926535897931
E = 2.7182818284590451
Naive approach = { 3.1415926535859, 2.71828182845473}
Error = {0.0000000000038931, 0000000000043151}
Knuth = { 3.14159265358979, 2.71828182845905}
Error = {0.0000000000000031, 0000000000000049}
Kahan = { 3.14159265358979, 2.71828182845905}
Error = {0.0000000000000031, 0000000000000049}
Sum:
value_1 = 0.1
value_2 = 0.3
Naive approach = { 10000.0000000188, 29999.9999999506}
Error = {0.0000000188, 0.0000000494}
Kahan = { 10000, 30000}
Error = {0, 0}
We can clearly see the errors that occur when naive approach is used. Whereas both Kahan and Knuth provide more accurate results.

References
https://mlblogblog.wordpress.com/2017/11/22/r2-the-best-algorithm-to-compute-the-online-mean/
Comments