#概念公式普及 方差公式:
通过此公式可推断出
s^2= E(x^2) -(E(x))^2
其中:E平均 D方差
E(x)平均值,D(x)=E{[x-E(x)]^2}称为方差
所以可以通过一次遍历求数组方差
double variance(double x[], int n) {
double s1 = 0, s2 = 0;
for(int i = 0; i < n; i++) {
s1 += x[i]*x[i];
s2 += x[i];
}
double quare = s1/n - (s2/n) * (s2/n);
return quare;
}
public static void main() {
double x[] = {4,4,5,5};
double var = variance(x, 4);
}