标准差、方差

double MyMethod::getStandardDeviation(double* info, int length)
{
  double result = 0;//存放每个值与平均值差的平方和
  double average = MyMethod::getAverage(info, length);//得到平均值
  for (int i = 0; i < length; i++)
  {
    result += (info[i] - average)*(info[i] - average);
  }
  result /= (length - 1);//方差
  result = sqrt(result);//标准差
  return result;
}


double MyMethod::getVariance(double* info, int length)
{
  double result = 0;//存放每个值与平均值差的平方和
  double average = MyMethod::getAverage(info, length);//得到平均值
  for (int i = 0; i < length;i++)
  {
    result += (info[i] - average)*(info[i] - average);
  }
  result /= (length - 1);
  return result;
}

 

用的是样本方差【不是全体方差】,样本数需要-1

posted @ 2018-06-05 17:07  朱小勇  阅读(269)  评论(0编辑  收藏  举报