Java Standard Deviation Calculate calcStdDeviation(double[] population)

Here you can find the source of calcStdDeviation(double[] population)

Description

calc Std Deviation

License

Apache License

Declaration

public static double calcStdDeviation(double[] population) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static double calcStdDeviation(double[] population) {
        return Math.sqrt(calcVariance(population));
    }/*from w  w  w .ja v a2 s.  c  o m*/

    public static double calcVariance(double[] population) {
        long n = 0;
        double mean = 0;
        double s = 0.0;

        for (double x : population) {
            n++;
            double delta = x - mean;
            mean += delta / n;
            s += delta * (x - mean);
        }
        //if you want to calculate std deviation
        //of a sample change this to (s/(n-1))
        return (s / n);
    }

    public static double calcVariance(double[] values, double mean) {
        double squares = 0D;
        for (double value : values) {
            squares += Math.pow((value - mean), 2);
        }
        return squares / (double) values.length;
    }
}

Related

  1. calcStdDeviation(int sampleCount, long total, long sumOfSquares)
  2. computeStddev(double[] array, double mean)
  3. computeStdDev(double[] results, double mean)
  4. computeStddev(float[] array, float mean)