Compute the variance of double array. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Compute the variance of double array.

Demo Code


//package com.java2s;

public class Main {


    /**//from   ww w.ja va 2s  . c  om
     * Compute the variance of the timeseries.
     * 
     * @param series The input timeseries.
     * @return the variance of values.
     */
    public static double var(double[] series) {
        double res = 0D;
        double mean = mean(series);
        for (int i = 0; i < series.length; i++) {
            res += (series[i] - mean) * (series[i] - mean);
        }
        return res / ((Integer) (series.length - 1)).doubleValue();
    }

    /**
     * Compute the mean of the timeseries.
     * 
     * @param series The input timeseries.
     * @return the mean of values.
     */
    public static double mean(double[] series) {
        double res = 0D;
        for (int i = 0; i < series.length; i++) {
            res += series[i];
        }
        return res / ((Integer) series.length).doubleValue();
    }
}

Related Tutorials