Why do I get non-zero standard deviation in data with no variability?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Susan Leemburg
am 6 Jan. 2021
Beantwortet: Susan Leemburg
am 11 Jan. 2021
Hello,
I have been calculating means and standard deviations for columns of data, and I am getting some weird results.
I have a 1x4 cell array called Transmats that has a 6x4 matrix in each cell (type double) that contains 4 datapoints (columns) for 6 subjects (rows). Each cell in Transmats has data from a different group.
For testing, I have just used the same input data for each row in Transmats.
So for now, Transmats{1} is the same as Transmats{2}, Transmats{3}, and Transmats{4} and it looks like this:
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
44.4128701072509 0.302502520854340 0.302502520854340 54.9821248510404
I then calculate the group averages and standard deviations for each of my groups like so:
% make group averages and SDs
for g=1:4 %time*treatment
for t =1:4 %transition type
Avmats{g,t}=mean(Transmats{g}(:,t));
Sdmats{g,t}=std(Transmats{g}(:,t));
end
end
Because all my values for each subject are the same, I should get a standard deviation of 0. But this is not what happens. I get the following standard deviations in Sdmats:
7.78360568894479e-15 0 0 7.78360568894479e-15
7.78360568894479e-15 0 0 7.78360568894479e-15
7.78360568894479e-15 0 0 7.78360568894479e-15
7.78360568894479e-15 0 0 7.78360568894479e-15
Why?
The only thing I can think of is that matlab rounds the numbers differently between loop iterations for some reason (that's the only way to get different numbers in Transmats in my script, I think). Is there a way to stop this from happening?
Even weirder, if I run my script on a different PC, I get a different result. Then standard deviations for column 1 and 4 of Sdmats are 0, and columns 2 and 3 have very small numbers in them.
I'm very confused and I'd very much like my standard deviation to be 0 when I have no variability in my data.
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 6 Jan. 2021
So for now, Transmats{1} is the same as Transmats{2}, Transmats{3}, and Transmats{4} and it looks like this:
They may be displayed the same but it's likely the stored values are very slightly different.
x = [1, 1+eps]
areTheyTheSame = x(1) == x(2) % false
difference = x(2)-x(1)
s = std(x)
The two elements of x are displayed the same, but they do not contain the same value. Therefore the standard deviation of x is non-zero.
0 Kommentare
Weitere Antworten (2)
the cyclist
am 6 Jan. 2021
Your standard deviaions are zero -- to within the accuracy possible by calculation in floating point numbers.
The first reference at the bottom of that documentation page is a fairly accesssible introduction to the idea.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!