Java Collection Mean meanAndVariance(Collection vector)

Here you can find the source of meanAndVariance(Collection vector)

Description

mean And Variance

License

Open Source License

Declaration

public staticdouble[] meanAndVariance(Collection<Number> vector) 

Method Source Code

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

import java.util.Collection;

public class Main {
    public static/* @pure@ */double[] meanAndVariance(Collection<Number> vector) {
        return meanAndVariance(vector.toArray(new Number[vector.size()]), true);
    }/* w ww . j  a v  a2  s. c om*/

    public static/* @pure@ */double[] meanAndVariance(Collection<Number> vector, boolean biased) {
        return meanAndVariance(vector.toArray(new Number[vector.size()]), biased);
    }

    public static/* @pure@ */double[] meanAndVariance(Number[] vector, boolean biased) {

        double sum = 0.0d, ssum = 0.0d;

        if (vector.length == 0) {
            return new double[] { 0.0d, 0.0d };
        }
        for (int i = 0; i < vector.length; i++) {
            sum += vector[i].doubleValue();
            ssum += vector[i].doubleValue() * vector[i].doubleValue();
        }
        double mean = sum / (double) vector.length;
        double variance = (ssum / (double) vector.length) - (mean * mean);
        if (!biased) {
            variance *= vector.length / (double) (vector.length - 1);
        }
        return new double[] { mean, variance };
    }
}

Related

  1. mean(Collection data)
  2. mean(Collection dist)
  3. mean(Collection vector)
  4. mean(final Collection values)
  5. mean(final Collection values)