简体   繁体   中英

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). Yesterday it was sold 230 in total. 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.

Thanks in advance.

Please take a look at this answer:

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

Maybe you want to modify it to use a double[] array as input:

  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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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