Java List Variance variance(List observations)

Here you can find the source of variance(List observations)

Description

variance

License

Open Source License

Declaration

public static double variance(List<Double> observations) 

Method Source Code


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

import java.util.*;

public class Main {
    public static double variance(List<Double> observations) {
        double mean = average(observations);
        double varSum = 0.0;
        for (double d : observations) {
            varSum += (d - mean) * (d - mean);
        }// w  ww.  j a v  a2s.  c  o  m
        return varSum / observations.size();
    }

    public static double variance(double[] observations) {
        double mean = average(observations);
        double varSum = 0.0;
        for (double d : observations) {
            varSum += (d - mean) * (d - mean);
        }
        // use bessel's correction
        return varSum / (observations.length - 1);
    }

    public static double average(List<Double> observations) {
        double total = 0.0;
        for (double d : observations) {
            total += d;
        }
        return total / observations.size();
    }

    public static double average(double[] data) {
        double total = 0.0;
        for (double d : data) {
            total += d;
        }
        return total / data.length;
    }
}

Related

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