Why do I get non-zero standard deviation in data with no variability?

9 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Steven Lord
Steven Lord el 6 de En. de 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]
x = 1×2
1.0000 1.0000
areTheyTheSame = x(1) == x(2) % false
areTheyTheSame = logical
0
difference = x(2)-x(1)
difference = 2.2204e-16
s = std(x)
s = 2.2204e-16
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.

Más respuestas (2)

the cyclist
the cyclist el 6 de En. de 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.

Susan Leemburg
Susan Leemburg el 11 de En. de 2021
Thank you for the replies, they are super helpful!
Luckily, these very small variations are not a problem for my data, but I'll definitely keep it in mind if I ever end up looking at very small values or very small differences.

Productos


Versión

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by