Java stddev stdDev(double[] data)

Here you can find the source of stdDev(double[] data)

Description

Compute the standard deviation of the given data

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static double stdDev(double[] data) 

Method Source Code

//package com.java2s;
/**/*from   ww w .ja va  2 s .co  m*/
 * Copyright 2000-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

public class Main {
    /**
     * Compute the standard deviation of the given data
     * @param data
     * @return
     */
    public static double stdDev(double[] data) {
        // Pseudocode from wikipedia, which cites Knuth:
        // n = 0
        // mean = 0
        // S = 0
        // foreach x in data:
        //   n = n + 1
        //   delta = x - mean
        //   mean = mean + delta/n
        //   S = S + delta*(x - mean)      // This expression uses the new value of mean
        // end for
        // variance = S/(n - 1)
        double mean = 0;
        double S = 0;
        for (int i = 0; i < data.length; i++) {
            double delta = data[i] - mean;
            mean += delta / (i + 1);
            S += delta * (data[i] - mean);
        }
        return Math.sqrt(S / data.length);
    }

    /**
     * Compute the standard deviation of the given data
     * @param data
     * @return
     */
    public static double stdDev(float[] data) {
        // Pseudocode from wikipedia, which cites Knuth:
        // n = 0
        // mean = 0
        // S = 0
        // foreach x in data:
        //   n = n + 1
        //   delta = x - mean
        //   mean = mean + delta/n
        //   S = S + delta*(x - mean)      // This expression uses the new value of mean
        // end for
        // variance = S/(n - 1)
        double mean = 0;
        double S = 0;
        for (int i = 0; i < data.length; i++) {
            double delta = data[i] - mean;
            mean += delta / (i + 1);
            S += delta * (data[i] - mean);
        }
        return Math.sqrt(S / data.length);
    }
}

Related

  1. stdDev(Collection coll)
  2. stddev(double sum, double sqSum, int numberValues)
  3. stddev(double[] a)
  4. stddev(double[] arr)
  5. stdDev(double[] array)
  6. stddev(double[] observations)
  7. stddev(final double[] in, final int start, final int length)
  8. stddev(final int[] scores)
  9. stddev(float[] data)