Java stddev stddev(Collection a)

Here you can find the source of stddev(Collection a)

Description

Compute the sample standard deviation for a Collection of numeric types

License

Open Source License

Parameter

Parameter Description
a a collection of numeric objects

Return

the average of a

Declaration

public static double stddev(Collection a) 

Method Source Code

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

import java.util.Collection;

public class Main {
    /**//w ww . j  a v a  2  s  .c  o  m
     * Compute the sample standard deviation for a Collection of numeric types
     * @param a a collection of numeric objects
     * @return the average of a
     */
    public static double stddev(Collection a) {
        return Math.sqrt(variance(a));
    }

    /**
     * Compute the sample variance for a Collection of numeric types
     * @param a a collection of numeric objects
     * @return the average of a
     */
    public static double variance(Collection a) {
        double mean = average(a);
        double squarediffsum = 0;

        for (Object x : a) {
            if (x instanceof Double) {
                squarediffsum += Math.pow((Double) x - mean, 2);
            } else if (x instanceof Long) {
                squarediffsum += Math.pow((Long) x - mean, 2);
            }
        }

        if (a.size() > 1) {
            return squarediffsum / (a.size() - 1);
        } else {
            return 0;
        }
    }

    /**
     * Compute the average for a Collection of numeric types
     * @param a a collection of numeric objects
     * @return the average of a
     */
    public static double average(Collection a) {
        double sum = 0;

        for (Object x : a) {
            if (x instanceof Double) {
                sum += (Double) x;
            } else if (x instanceof Long) {
                sum += (Long) x;
            } else if (x instanceof Integer) {
                sum += (Integer) x;
            } else if (x instanceof Float) {
                sum += (Float) x;
            }
        }

        if (a.size() > 0) {
            return sum / a.size();
        } else {
            return 0;
        }
    }
}

Related

  1. std(final double[] vec)
  2. std(final double[] x, final int begin, final int end)
  3. std(float[][] arr)
  4. std(long[] array)
  5. stdarr(int[] a, double avg)
  6. stdDev(Collection coll)
  7. stddev(double sum, double sqSum, int numberValues)
  8. stddev(double[] a)
  9. stddev(double[] arr)