我正在尝试计算(在 VBA Excel 中)具有超过 65536 个元素的数组的平均值和标准差。像这样的东西:
Mitja = worksheetfunction.Average(array()) DesvTip = worksheetfunction.StDev(array())
虽然数组的维度小于 65536 没有问题,但是当它更大时它会给我一个错误!
我知道这个 VBA 函数不能处理超过 65536 个数据,所以如何在 VBA 中获取这个参数?
欣赏您的评论。非常感谢!:))
您可以计算平均值和标准偏差,而无需存储所有值。只需保持总和、平方和和点数的总和。您可以拥有尽可能多的点,因为整数点将允许这种方式。
这是我在Java中的做法。随意婴儿床。
package statistics;
/**
* Statistics
* @author Michael
* @link http://stackoverflow.com/questions/11978667/online-algorithm-for-calculating-standrd-deviation/11978689#11978689
* @link http://mathworld.wolfram.com/Variance.html
* @since 8/15/12 7:34 PM
*/
public class Statistics {
private int n;
private double sum;
private double sumsq;
public void reset() {
this.n = 0;
this.sum = 0.0;
this.sumsq = 0.0;
}
public synchronized void addValue(double x) {
++this.n;
this.sum += x;
this.sumsq += x*x;
}
public synchronized double calculateMean() {
double mean = 0.0;
if (this.n > 0) {
mean = this.sum/this.n;
}
return mean;
}
public synchronized double calculateVariance() {
double variance = 0.0;
if (this.n > 0) {
variance = Math.sqrt(this.sumsq-this.sum*this.sum/this.n)/this.n;
}
return variance;
}
public synchronized double calculateStandardDeviation() {
double deviation = 0.0;
if (this.n > 1) {
deviation = Math.sqrt((this.sumsq-this.sum*this.sum/this.n)/(this.n-1));
}
return deviation;
}
}
感谢您的评论。最后我们做了类似的事情。我希望它对有同样问题的人有用。我们的代码:
sum = 0
sumq = 0
For i = 0 To ((2 * N) - 1)
sum = sum + h_normal(i)
Next i
media = sum / (2 * N)
For j = 0 To ((2 * N) - 1)
sumsq = sumsq + (h_normal(j) - media) ^ 2
Next j
desviaci(h - 1) = Math.Sqr(sumsq / ((2 * N) - 1))
如果数据存储在数组中,请使用以下算法x(1 to N, 1 to 1)
,其中N
是数据点的数量
sum = 0# : sumsq = 0#
for i=1 to N
sum = sum + x(i,1)
sumsq = sumsq + x(i,1)^2
next i
average = sum/N
stddev = Sqr( sumsq/N^2 - sum^2/N^3 )
:注意: 要填充数组,请使用符号
Dim r as Range, x() as Variant
Set r = Range("A1").Resize(N,1)
x = r.Value