Java List std stdDev(List nums)

Here you can find the source of stdDev(List nums)

Description

Get the standard deviation.

License

Apache License

Declaration

public static double stdDev(List<? extends Number> nums) 

Method Source Code


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

import java.util.List;

public class Main {
    /**/*  www  .j av  a 2 s .c o m*/
     * Get the standard deviation.
     */
    public static double stdDev(List<? extends Number> nums) {
        if (nums == null || nums.size() == 0) {
            throw new IllegalArgumentException();
        }

        return stdDev(nums, 0, nums.size());
    }

    /**
     * Get the standard deviation, but if you already know the mean
     *  it can go faster.
     */
    public static double stdDev(List<? extends Number> nums, double mean) {
        if (nums == null || nums.size() == 0) {
            throw new IllegalArgumentException();
        }

        return stdDev(nums, 0, nums.size(), mean);
    }

    /**
     * Get the standard deviation of a range.
     */
    public static double stdDev(List<? extends Number> nums, int start, int size) {
        if (nums == null || nums.size() == 0 || start < 0 || start >= nums.size() || size <= 0
                || start + size > nums.size()) {
            throw new IllegalArgumentException();
        }

        double stdDev = 0;
        double mean = mean(nums, start, size);

        for (int i = start; i < start + size; i++) {
            stdDev += Math.pow(nums.get(i).doubleValue() - mean, 2);
        }

        return Math.sqrt(stdDev / size);
    }

    /**
     * Get the standard deviation or a range, but if you already know the mean
     *  it can go faster.
     */
    public static double stdDev(List<? extends Number> nums, int start, int size, double mean) {
        if (nums == null || nums.size() == 0 || start < 0 || start >= nums.size() || size <= 0
                || start + size > nums.size()) {
            throw new IllegalArgumentException();
        }

        double stdDev = 0;

        for (int i = start; i < start + size; i++) {
            stdDev += Math.pow(nums.get(i).doubleValue() - mean, 2);
        }

        return Math.sqrt(stdDev / size);
    }

    /**
     * Calculate the mean of a range in list of numbers.
     *
     * @param nums The numbers to get the mean of.
     * @param start The index to start taking the mean at
     * @param size The number of elements to include in the mean.
     *
     * @throws IllegalArgumentException If anything is weird (null or zero list), zero size.
     *
     * @return The mean
     */
    public static double mean(List<? extends Number> nums, int start, int size) {
        if (nums == null || nums.size() == 0 || start < 0 || start >= nums.size() || size <= 0
                || start + size > nums.size()) {
            throw new IllegalArgumentException();
        }

        double mean = 0;

        for (int i = start; i < start + size; i++) {
            mean += nums.get(i).doubleValue();
        }

        return (mean / size);
    }

    /**
     * Calculate the mean of a list of numbers.
     *
     * @param nums The numbers to get the mean of.
     *
     * @throws IllegalArgumentException If anything is weird (null or zero list).
     *
     * @return The mean.
     */
    public static double mean(List<? extends Number> nums) {
        if (nums == null || nums.size() == 0) {
            throw new IllegalArgumentException();
        }

        return mean(nums, 0, nums.size());
    }
}

Related

  1. standardDeviation(List list)
  2. standardDeviation(List values, double xbar)
  3. standardDeviationDouble(List list, boolean populationStandardDeviation)
  4. std(List iNumbers)
  5. std(List vector, Double avg)
  6. stddev(List observations)
  7. stDev(final List list)
  8. stdev(List values)
  9. stdInt(List elements)