Calculates the sample standard deviation of a list of list of doubles - Java java.lang

Java examples for java.lang:float

Description

Calculates the sample standard deviation of a list of list of doubles

Demo Code

/*/*from  w w w .j  av a 2 s.c  o  m*/
 * Copyright (c) 2010-2011 Ashlie Benjamin Hocking. All Rights reserved.
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main{
    /**
     * Calculates the sample standard deviation of a list of list of doubles
     * @param dblList List of list of doubles to calculate sample standard deviation for
     * @return Sample standard deviation
     */
    public static double sampleStdDev(final List<List<Double>> dblList) {
        int numSums = 0;
        double totalSum = 0.0;
        double totalSoS = 0.0;
        for (final List<Double> withinTrial : dblList) {
            totalSum += ArrayNumberUtils.sum(withinTrial);
            totalSoS += ArrayNumberUtils.sumOfSquares(withinTrial);
            numSums += withinTrial.size();
        }
        if (numSums > 1 && !Double.isNaN(totalSum)) {
            // Calculate sample standard deviation
            return Math.sqrt((numSums * totalSoS - totalSum * totalSum)
                    / (numSums * (numSums - 1)));
        }
        return Double.NaN;
    }
    /**
     * Adds up all of the values in an array of Strings.
     * @param valArray Array of Strings
     * @return Sum of values in the array
     * @exception NumberFormatException if one of the Strings does not contain a parsable number.
     */
    public static double sum(final String[] valArray) {
        return sum(toDoubleList(valArray));
    }
    /**
     * Adds up all of the values in an array of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberArray Array of Numbers
     * @return Sum of values in the array
     */
    public static <T extends Number> double sum(final T[] numberArray) {
        return sum(Arrays.asList(numberArray));
    }
    /**
     * Adds up all of the values in a List of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberList List of Numbers
     * @return Sum of values in the List
     */
    public static <T extends Number> double sum(final List<T> numberList) {
        double retval = 0;
        if (!numberList.isEmpty()) {
            for (final Integer i : new IntegerRange(numberList.size())) {
                retval += numberList.get(i).doubleValue();
            }
        }
        return retval;
    }
    /**
     * Adds up all of the squares of values in an array of Strings.
     * @param valList Array of Strings
     * @return Sum of squares of values in the array
     * @exception NumberFormatException if one of the Strings does not contain a parsable number.
     */
    public static double sumOfSquares(final String[] valList) {
        return sumOfSquares(toDoubleList(valList));
    }
    /**
     * Adds up all of the squares of values in an array of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberArray Array of Numbers
     * @return Sum of squares of values in the array
     */
    public static <T extends Number> double sumOfSquares(
            final T[] numberArray) {
        return sumOfSquares(Arrays.asList(numberArray));
    }
    /**
     * Adds up all of the squares of values in a List of Numbers.
     * @param <T> Class extending Number (e.g., Double or Integer)
     * @param numberList List of Numbers
     * @return Sum of squares of values in the List
     */
    public static <T extends Number> double sumOfSquares(
            final List<T> numberList) {
        double retval = 0;
        if (!numberList.isEmpty()) {
            for (int i = 0; i < numberList.size(); ++i) {
                final double v = numberList.get(i).doubleValue();
                retval += v * v;
            }
        }
        return retval;
    }
    /**
     * Converts an array of Strings to a List of Doubles
     * @param valList Array of Strings
     * @return List of Doubles corresponding to the the Array of Strings
     * @exception NumberFormatException if one of the Strings does not contain a parsable number.
     */
    public static List<Double> toDoubleList(final String[] valList) {
        final List<Double> retval = new ArrayList<Double>();
        for (final String s : valList) {
            retval.add(Double.valueOf(s));
        }
        return retval;
    }
    /**
     * Adds up two equal sized Lists into a resultant List of the same size
     * @param list1 First list to add
     * @param list2 Second list to add
     * @pre list1.size() == list2.size()
     * @return Addition of two arrays
     */
    public static List<Double> add(final List<Double> list1,
            final List<Double> list2) {
        if (list1.size() != list2.size())
            throw new IllegalArgumentException(
                    "Lists being added together must be of the same size.");
        final List<Double> retval = new ArrayList<Double>(list1.size());
        if (!list1.isEmpty()) {
            for (final Integer i : new IntegerRange(list1.size())) {
                retval.add(list1.get(i) + list2.get(i));
            }
        }
        return retval;
    }
    /**
     * @param list List to add to
     * @param toAdd Amount to add to the list
     * @return List with all values increased by toAdd
     */
    public static List<Double> add(final List<Double> list,
            final double toAdd) {
        final List<Double> retval = new ArrayList<Double>(list.size());
        if (!list.isEmpty()) {
            for (final Integer i : new IntegerRange(list.size())) {
                retval.add(list.get(i) + toAdd);
            }
        }
        return retval;
    }
}

Related Tutorials