0

Consider the following example, executed in Matlab:

x =  linspace(-pi, pi, 10^4);
x_R = chop(x, 3);
y = sin(x);
y_R = sin(x_R);

s = sum(y);
s_R = sum(y_R);
abs_err_sum = abs(s_R - s);
rel_err_sum = abs_err_sum/abs(s);

m = mean(y);
m_R = mean(y_R);
abs_err_mean = abs(m_R - m);
rel_err_mean = abs_err_mean/abs(m);

The relative error for the sums ended up being minutely bigger than that of the means (not by much: a difference of 1.1102e-16), but I do not understand why, particularly since computing the mean also involves computing the same sum (unless Matlab mean() does not itself use the same sum() function itself?).

To give more context, this is part of an assignment that we are creating for students, based on an assignment that I myself had when I was a student. The original question in that assignment was "In this case, is the sum operator more robust to digit cancellation or the mean operator? Why?" I am beginning to think that the question was not phrased in the best way and perhaps both are equally robust.

astros
  • 1
  • 2
  • 4
    If you compared the sum (or mean) of y to the sum (or mean) of yR, then you are getting some estimate of an error that includes both errors in computing the sum (or mean) and the errors in altered sine values. In other words, y does not contain the same values as yR, so of course the sum computed for it will differ from the sum computed for yR, even if there is zero error in computing those sums. So it is not clear at all what you did in this experiment. You should show a [mre] and/or explain in further detail, including clearly stating which steps used 3-digit arithmetic and which did not. – Eric Postpischil Jan 18 '21 at 18:20
  • Mean and sum are of course related by mean = sum / N. So, no, mean is not more stable, since an algorithm to compute one can trivially be used to compute the other. Like Eric said, details are important. For instance, whether the sum or mean is accumulated in double precision or using [Kahan summation](https://en.wikipedia.org/wiki/Kahan_summation_algorithm). – Pascal Getreuer Jan 18 '21 at 23:07
  • Dear both, thank you very much for your answers. I shall update my post accordingly. – astros Jan 19 '21 at 08:32
  • I hope this edit clarifies the situation. I apologize for my previous lack of clarity, or still current lack thereof. I believe this is the first time I've posted a question on Stackoverflow. – astros Jan 19 '21 at 08:51
  • What does `chop(x, 3)` do? [The MathWorks’ documentation site](https://www.mathworks.com/matlabcentral/fileexchange/70651-chop?s_tid=srchtitle) suggests `chop` is some third-party function for reducing precision but that `3` is not a proper argument for it. – Eric Postpischil Jan 19 '21 at 13:19
  • A relative error of 1.1102e-16 is less than 1 ULP in the IEEE-754 binary64 format. It may be less than ½ ULP, depending on where a result falls within the significand interval. Such an error can result from a single operation, such as the final division of the sum to produce the mean. This error is tiny and indicates the computation of the sums was essentially just as accurate as the computation of the means. The difference may be entirely due to the division. – Eric Postpischil Jan 19 '21 at 13:25
  • I apologize for my late reply here. Thank you very much for your responses! They clarified the situation. – astros Apr 13 '21 at 13:40

0 Answers0