简体   繁体   English

计算方差/标准差 - JAVA

[英]Computing Variance/Standard deviation - JAVA

There is a table with purchase details.有一张桌子,上面有购买细节。 Let say, an item "chocolate" is sold 1500 in total for past one week (one week from yesterday).比如说,一件“巧克力”在过去一周(从昨天开始的一周)总共售出 1500 件。 Yesterday it was sold 230 in total.昨天一共卖出了230个。 I have counts for per day and per week.我每天和每周都有计数。

is it possible to get variance/standard deviation by taking an average on count past week and compare with yesterday count.是否可以通过对上周的计数取平均值并与昨天的计数进行比较来获得方差/标准偏差。 basically variance / sd on avg(1500) and 230. Please suggest if its correct way to and advice on how to do it in java.基本上是 avg(1500) 和 230 上的方差/标准差。请在 java 中提出正确的方法和建议。

Thanks in advance.提前致谢。

Please take a look at this answer:请看一下这个答案:

https://stackoverflow.com/a/36186227/8310211 https://stackoverflow.com/a/36186227/8310211

Maybe you want to modify it to use a double[] array as input:也许您想修改它以使用 double[] 数组作为输入:

  public static double stdDev(double[] inputArray) {
    double sum = 0;
    double sq_sum = 0;
    for (int i = 0; i < inputArray.length; ++i) {
      double ai = inputArray[i];
      sum += ai;
      sq_sum += ai * ai;
    }
    double mean = sum / inputArray.length;
    double variance = sq_sum / inputArray.length - mean * mean;
    return Math.sqrt(variance);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM