possible Bug? or checking if OverFlow happened?
Show older comments
I have a matrix of size (6472908 x 67) all single values. Different columns have different max/min (there are different variables.
So I calculate mean of each column using
avgData=mean(Data);
I am expecting the first value in avgData to be the mean of the first column. However, when I issue
avgData(1) - mean(Data(:,1))
ans =
-100.9785
as you can see the output is not zero. So what is changed? The same thing happened if I convert everything to double.
If I do this for sum() the difference is even more. So, I was wondering if overflow is happening and how should I check if overflow has happened? lastwarn() returns nothing.
I am afraid the X is about 800MB and can't upload it here.
Accepted Answer
More Answers (2)
James Tursa
on 31 Mar 2015
Edited: James Tursa
on 31 Mar 2015
0 votes
How large is the average value? Is 100 close to eps of this average value? E.g., is the average value near 1e17? If so, this could just be rounding differences in the methods used. I.e., maybe the problem is split up differently for multi-threading if there are several columns of a matrix involved vs just one column.
"...The same thing happened if I convert everything to double..."
You get the exact same result? Or you get a similar result (i.e., something not "close" to zero.). It would not surprise me that in the background MATLAB uses double accumulators even if the input is single, which might explain the same result in single vs double.
EDIT:
Probably the better comparison would be eps of the sum, not eps of the average.
EDIT:
You might also consider using this FEX contribution from Jan Simon:
1 Comment
Mohammad Abouali
on 31 Mar 2015
Edited: Mohammad Abouali
on 31 Mar 2015
Image Analyst
on 31 Mar 2015
0 votes
Yes, this is a known issue. We've converted arrays from double to single to save on memory yet when calling mean(), the means may not be correct. Basically it's ignoring the later elements as you add them because the sum is so huge and a tiny value added onto a gigantic value basically does not get added because it's so small. Basically underflow . We contacted the Mathworks to ask them. The Mathworks knows about this and does not consider it a bug , but just a normal precision issue comparable to this issue.
3 Comments
Mohammad Abouali
on 31 Mar 2015
Edited: Mohammad Abouali
on 31 Mar 2015
Roger Stafford
on 1 Apr 2015
"I expect the same results" <-- This is an assumption you should not make. Strictly speaking, even the associative and distributive laws of arithmetic are violated when computation is subject to round-off errors. When a series of numbers is added, the results can depend on how they are grouped together in the addition process. I like to give the following as an example which will usually give differing results even on a decimal calculator:
3/14+(3/14+15/14)
(3/14+3/14)+15/14
In computing mean(Data(:,1)) one cannot assume that the algorithm used is identical to that used for the first column of mean(Data). It all depends on just how the programmers who did the coding decided to handle the two different situations. Perhaps it depends on what they ate for breakfast? The basic assumption is that if different results agree within round-off error with perfect results, then either is acceptable.
Mohammad Abouali
on 1 Apr 2015
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!