MATLAB: Another simple question about standard deviation!

programmingstandard deviationstatistics

Could I ask yet another question on the theme of standard deviation? – and yes I have read the documentation and it doesn't answer this question. Say if you have: x = [1,2,3,4,5,6] and w = [5,7,10,8,12,3] and I want to find the weighted std for a population, how do I write the command for a POPULATION? I understand for a sample it is:
StdSamp = std(x,w) If you put 1 as the 3rd parameter, it does not interpret it as pop.

Best Answer

std automatically assumes you are doing this for a complete population, NOT as a sample. When you are not sure about something, the best way is to test it! So, how can we test my claim? How might you have done so, and gotten an answer 6 hours earlier?
X = rand(1,5);
First, what does std do, with no weights employed?
std(X)
ans =
0.32851
std(X,1)
ans =
0.29383
So as one should expect, the two are different, by a ratio of
sqrt(5)/2
ans =
1.118
std(X)/std(X,1)
ans =
1.118
That is as expected. std(X) divides by sqrt(n-1) in the formula, but std(X,1) divides by sqrt(n).
Now, lets see what happens when we use weights. A very simple weight vector is sufficient here.
W = ones(1,5);
std(X,W)
ans =
0.29383
This is the population standard deviation, as produced by std(X,1).
std(X,1)
ans =
0.29383
The point is, it makes no sense at all to talk about a sample standard deviation when you have weights. Well, relatively little sense. Given a set of weights, we can only interpret this as the entire population.
I will concede that the documentation (both doc and help) for std should have made this fact explicitly clear, even though it seems clear to me regardless, since the alternative makes no sense. If you have weights, the points are treated as a complete population.
std(X,W,0)
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in var (line 109)
n = size(x,dim);
Error in std (line 51)
y = sqrt(var(varargin{:}));
std(X,0,W)
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in var (line 109)
n = size(x,dim);
Error in std (line 51)
y = sqrt(var(varargin{:}));
Yep. std agrees with me.