我的阵列真实值,其具有平均和标准偏差。如果将数组元素替换为另一个元素,则新的均值将为
这种方法的优点是,无论的值如何,都需要恒定的计算量。是否有任何的方法来计算σ Ñ Ë 瓦特使用σ ö 升d等的计算μ Ñ Ë 瓦特使用μ ö 升d?
我的阵列真实值,其具有平均和标准偏差。如果将数组元素替换为另一个元素,则新的均值将为
这种方法的优点是,无论的值如何,都需要恒定的计算量。是否有任何的方法来计算σ Ñ Ë 瓦特使用σ ö 升d等的计算μ Ñ Ë 瓦特使用μ ö 升d?
Answers:
一个维基百科的文章中的“算法计算方差”部分展示了如何计算方差,如果元素被添加到您的意见。(回想一下,标准偏差是方差的平方根。)假设您将附加到数组中,然后
编辑:上面的公式似乎是错误的,请参阅注释。
现在,替换一个元素意味着添加一个观测值并删除另一个观测值。两者都可以用上面的公式计算。但是,请记住,可能会出现数值稳定性问题。引用的文章还提出了数值稳定的变体。
到自己推导式中,计算使用样本方差和替代的定义μ Ñ Ë 瓦特式你给适当的时候。这给你σ 2 Ñ ë 瓦特 - σ 2 ö 升d到底,从而为一个公式σ Ñ Ë 瓦特给出σ ö 升d和。在我的符号,我想你更换元素 X ñ通过 X ' ñ:
, but you'll have to work the equation a little bit more to derive a neat result. This should give you the general idea.
Based on what i think i'm reading on the linked Wikipedia article you can maintain a "running" standard deviation:
real sum = 0;
int count = 0;
real S = 0;
real variance = 0;
real GetRunningStandardDeviation(ref sum, ref count, ref S, x)
{
real oldMean;
if (count >= 1)
{
real oldMean = sum / count;
sum = sum + x;
count = count + 1;
real newMean = sum / count;
S = S + (x-oldMean)*(x-newMean)
}
else
{
sum = x;
count = 1;
S = 0;
}
//estimated Variance = (S / (k-1) )
//estimated Standard Deviation = sqrt(variance)
if (count > 1)
return sqrt(S / (count-1) );
else
return 0;
}
Although in the article they don't maintain a separate running sum
and count
, but instead have the single mean
. Since in thing i'm doing today i keep a count
(for statistical purposes), it is more useful to calculate the means each time.
Given original , , and , as well as the change of a given element to , I believe your new standard deviation will be the square root of
Maybe there is a snazzier way of writing it?
I checked this against a small test case and it seemed to work.