Calculate standard deviation given frequency counts rather than sample

7 ビュー (過去 30 日間)
the cyclist
the cyclist 2013 年 8 月 27 日
Given a sample x
x = [1 1 1 1 2 2 2 3 3 3 3 4 5 6 6];
it is trivial to calculate the standard deviation:
s = std(x);
Suppose instead of x, I have the an array with the frequency counts of x:
A = [4 1;
3 2;
4 3;
1 4;
1 5;
2 6];
What's an elegant way to calculate the standard deviation, without needing to reconstruct x along the way?

採用された回答

dpb
dpb 2013 年 8 月 27 日
編集済み: dpb 2013 年 8 月 27 日
One way,
>> m=dot(a(:,1)',a(:,2))/sum(a(:,1))
m =
2.8667
>> s=sqrt(dot([[a(:,2)-m]'].^2,a(:,1))/(sum(a(:,1))-1))
s =
1.7265
>> [mean(x) std(x)]
ans =
2.8667 1.7265
>>
  4 件のコメント
the cyclist
the cyclist 2013 年 8 月 27 日
Patience! Not every pounces on the answers within minutes. :-)
dpb
dpb 2013 年 8 月 27 日
Chuckles...
This implementation is, of course, straightforward and for small sample sizes and well-behaved inputs should be fine. You're at the mercy of data order for computation of course, so isn't as robust as might be (and as I presume the builtin mean/std functions are)

サインインしてコメントする。

その他の回答 (1 件)

Iain
Iain 2013 年 8 月 27 日
編集済み: Iain 2013 年 8 月 27 日
mu = (A(:,1).*A(:,2)) ./ sum(A(:,1));
std =sqrt(sum(A(:,1).*(A(:,2)-mu).^2)) ./(sum(A(:,1))-1));
  2 件のコメント
the cyclist
the cyclist 2013 年 8 月 27 日
This gives vector results for mu and std, so guessing you are missing an operation.
Iain
Iain 2013 年 8 月 27 日
Yup, I'm missing a sum here or there:
mu = sum(A(:,1).*A(:,2)) ./ sum(A(:,1));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by