Java stddev stdev(Collection values)

Here you can find the source of stdev(Collection values)

Description

stdev

License

Open Source License

Declaration

public static Double stdev(Collection<Double> values) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    public static Double stdev(Collection<Double> values) {
        List<Double> filteredValues = filterNulls(values);
        if (filteredValues.size() < 2)
            return null;

        Double average = average(filteredValues);
        if (average == null)
            return null;
        double variance = 0;

        for (Double value : filteredValues) {
            variance += (value - average) * (value - average);
        }//w  w  w.  j a  va 2s .  c  om

        return Math.sqrt(variance / filteredValues.size());
    }

    private static List<Double> filterNulls(Collection<Double> values) {
        List<Double> result = new ArrayList<>();
        for (Double value : values) {
            if (value != null)
                result.add(value);
        }

        return result;
    }

    public static Double average(Collection<Double> values) {
        List<Double> filteredValues = filterNulls(values);
        if (filteredValues.size() == 0)
            return null;

        double sum = 0;
        for (Double value : filteredValues) {
            sum += value;
        }

        return sum / filteredValues.size();
    }
}

Related

  1. stdDev(Integer[] values)
  2. stddev(long[] samples)
  3. stddev(Number[] array, boolean isSample)
  4. stdDev(T[] array)
  5. stdDeviation(int[] data)
  6. stdev(Collection values)
  7. stdev(double... array)
  8. stdev(double[] array)
  9. stdev(final double... values)