Computes the standard deviation of a set, given the average value. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Computes the standard deviation of a set, given the average value.

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    /**/* ww  w.j av a2  s  .  c om*/
     * Computes the standard deviation of a set, given the average value.
     * @param c Values set
     * @param mean Average value of the set.
     * @return Return the standard deviation of the set.
     */
    public static double stdDeviation(Collection<Double> c, double mean) {
        double var = variance(c, mean);
        return Math.sqrt(var / (c.size() - 1));
    }

    /**
     * Computes the standard deviation of a set.
     * @param c Values set
     * @return Return the standard deviation of the set.
     */
    public static double stdDeviation(Collection<Double> c) {
        return stdDeviation(c, mean(c));
    }

    public static double variance(Collection<Double> c, double mean) {
        double sum = 0.0;

        for (Double i : c) {
            double cur = i - mean;
            sum += cur * cur;
        }

        return sum;
    }

    public static double variance(Collection<Double> c) {
        return variance(c, mean(c));
    }

    /**
     * Computes the set mean.
     * @param c Values set.
     * @return Return the average value of the set.
     */
    public static double mean(Collection<Double> c) {
        double sum = 0.0;
        for (Double t : c) {
            sum += t;
        }

        return sum / c.size();
    }
}

Related Tutorials