Java stddev stdev(final double... values)

Here you can find the source of stdev(final double... values)

Description

Calculates and returns the standard deviation of an array of double.

License

Open Source License

Parameter

Parameter Description
values an array of doubles to calculate the standard deviation of.

Return

the standard deviation of the provided array of doubles.

Declaration

public static double stdev(final double... values) 

Method Source Code

//package com.java2s;
/*/*www  . j a v  a2s .c  om*/
 * Copyright 2007-2010 Tom Castle & Lawrence Beadle
 * Licensed under GNU General Public License
 * 
 * This file is part of EpochX: genetic programming software for research
 * 
 * EpochX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * EpochX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with EpochX. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The latest version is available from: http:/www.epochx.org
 */

public class Main {
    /**
     * Calculates and returns the standard deviation of an array of
     * <code>double</code>. The provided argument must not be null nor an empty
     * array.
     * 
     * @param values an array of doubles to calculate the standard deviation of.
     * @return the standard deviation of the provided array of doubles.
     */
    public static double stdev(final double... values) {
        return stdev(values, ave(values));
    }

    /**
     * Calculates and returns the standard deviation of an array of
     * <code>double</code>. The provided <code>values</code>argument must not be
     * null nor an empty array. The <code>ave</code> argument supplies the mean
     * average of the values if it is known to save calculating it again.
     * 
     * @param values an array of doubles to calculate the standard deviation of.
     * @param ave the mean average of the values.
     * @return the standard deviation of the provided array of doubles.
     */
    public static double stdev(final double[] values, final double ave) {
        if ((values != null) && (values.length != 0)) {
            // Sum the squared differences.
            double sqDiff = 0;
            for (int i = 0; i < values.length; i++) {
                sqDiff += Math.pow(values[i] - ave, 2);
            }

            // Take the square root of the average.
            return Math.sqrt(sqDiff / values.length);
        } else {
            throw new IllegalArgumentException(
                    "cannot calculate standard deviation of null or empty array of values");
        }
    }

    /**
     * Calculates and returns the standard deviation of an array of
     * <code>int</code>. The provided argument must not be null nor an empty
     * array.
     * 
     * @param values an array of int to calculate the standard deviation of.
     * @return the standard deviation of the provided array of ints.
     */
    public static double stdev(final int... values) {
        return stdev(values, ave(values));
    }

    /**
     * Calculates and returns the standard deviation of an array of
     * <code>int</code>. The provided <code>values</code>argument must not be
     * null nor an empty array. The <code>ave</code> argument supplies the mean
     * average of the values if it is known to save calculating it again.
     * 
     * @param values an array of int to calculate the standard deviation of.
     * @param ave the mean average of the values.
     * @return the standard deviation of the provided array of ints.
     */
    public static double stdev(final int[] values, final double ave) {
        if ((values != null) && (values.length != 0)) {
            // Sum the squared differences.
            double sqDiff = 0;
            for (int i = 0; i < values.length; i++) {
                sqDiff += Math.pow(values[i] - ave, 2);
            }

            // Take the square root of the average.
            return Math.sqrt(sqDiff / values.length);
        } else {
            throw new IllegalArgumentException("cannot calculate average of null or empty array of values");
        }
    }

    /**
     * Calculates and returns the mean average of an array of
     * <code>double</code>. The provided argument must not be null nor an empty
     * array.
     * 
     * @param values an array of doubles to calculate the mean average of.
     * @return the mean average of the provided array of doubles.
     */
    public static double ave(final double... values) {
        if ((values != null) && (values.length != 0)) {
            double sum = 0;
            for (int i = 0; i < values.length; i++) {
                sum += values[i];
            }
            return sum / values.length;
        } else {
            throw new IllegalArgumentException("cannot calculate average of " + "null or empty array of values");
        }
    }

    /**
     * Calculates and returns the mean average of an array of <code>int</code>.
     * The provided argument must not be null nor an empty array.
     * 
     * @param values an array of int to calculate the mean average of.
     * @return the mean average of the provided array of int.
     */
    public static double ave(final int... values) {
        if ((values != null) && (values.length != 0)) {
            int sum = 0;
            for (int i = 0; i < values.length; i++) {
                sum += values[i];
            }
            return ((double) sum) / values.length;
        } else {
            throw new IllegalArgumentException("cannot calculate average of null or empty array of values");
        }
    }
}

Related

  1. stdDeviation(int[] data)
  2. stdev(Collection values)
  3. stdev(Collection values)
  4. stdev(double... array)
  5. stdev(double[] array)
  6. stdev(long[] vals, long average)