Java List Variance variance(final List list)

Here you can find the source of variance(final List list)

Description

Variance.

License

GNU General Public License

Parameter

Parameter Description
list the list

Return

the double

Declaration

public static double variance(final List<Double> list) 

Method Source Code

//package com.java2s;
/*/* w  ww  .  ja  v a2s. c  om*/
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2012, The University of Melbourne, Australia
 */

import java.util.List;

public class Main {
    /**
     * Variance.
     * 
     * @param list the list
     * @return the double
     */
    public static double variance(final List<Double> list) {
        long n = 0;
        double mean = mean(list);
        double s = 0.0;

        for (double x : list) {
            n++;
            double delta = x - mean;
            mean += delta / n;
            s += delta * (x - mean);
        }
        // if you want to calculate std deviation
        // of a sample change this to (s/(n-1))
        return s / (n - 1);
    }

    /**
     * Gets the average.
     * 
     * @param list the list
     * 
     * @return the average
     */
    public static double mean(final List<Double> list) {
        double sum = 0;
        for (Double number : list) {
            sum += number;
        }
        return sum / list.size();
    }
}

Related

  1. variance(ArrayList doubleArrayList)
  2. variance(List data)
  3. variance(List observations)
  4. variance(List values)
  5. variance(List data)