遍历数组一次求得数组的平均数、标准差、方差

  • 理论根据:DX^2=EX^2-(EX)^2
  • 实现:
    static Tuple<double,double,double> ComputeVariance2(double[] a)
    {
        double variance = 0;    //方差
        double sum = 0;         //总和
        double sum2 = 0;        //平方和
        int len = a.Length;
        for (int i = 0; i < len; i++)
        {
            sum += a[i];
            sum2 += a[i] * a[i];
        }
        variance = sum2 / len - (sum / len) * (sum / len);
        //                  平均数   标准差               方差
        return Tuple.Create(sum/len, Math.Sqrt(variance), variance);
    }
posted @ 2016-05-26 14:04  夏至千秋  阅读(4875)  评论(0编辑  收藏  举报