Java Array Variance variance(double[] d, double mean)

Here you can find the source of variance(double[] d, double mean)

Description

Calculates and returns the variance of the given list of double values.

License

Open Source License

Parameter

Parameter Description
d the list of double values
mean the mean of the double values

Declaration

public static double variance(double[] d, double mean) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    /**// w  w w.  ja  v a2  s .co  m
     * Calculates and returns the variance of the given list of double values
     * 
     * @param d
     *            the list of double values
     * @return
     */
    public static double variance(double[] d) {
        double mean = average(d);
        return variance(d, mean);
    }

    /**
     * Calculates and returns the variance of the given list of double values.
     * This version of the method also takes the precalculated mean of the
     * values as parameter to prevent redundant calculations.
     * 
     * @param d
     *            the list of double values
     * @param mean
     *            the mean of the double values
     * @return
     */
    public static double variance(double[] d, double mean) {
        if (d.length <= 1) {
            return 0.0;
        }
        double sum = 0.0;
        for (int i = 0; i < d.length; i++) {
            sum += Math.pow(d[i] - mean, 2);
        }
        return sum / (d.length - 1);
    }

    /**
     * Mittelwertberechnung. Versuchts erst schneller und nimmt sonst den
     * langsameren, aber sicheren Algorithmus.
     */
    public static double average(double[] d) {
        double average = average1(d);
        if (Double.isNaN(average) || Double.isInfinite(average))
            return average2(d);
        return average;
    }

    /**
     * Spaltenweise mittelwertberechnung. Versuchts erst schneller und nimmt
     * sonst den langsameren, aber sicheren Algorithmus.
     */
    public static double[] average(double[][] d) {
        double[] average = average1(d);
        if (average == null)
            return null; // Koomt vor wenn er alle sequenzen nicht mappen kann
        for (int i = 0; i < average.length; i++)
            if (Double.isNaN(average[i]) || average[i] == Double.POSITIVE_INFINITY
                    || average[i] == Double.NEGATIVE_INFINITY)
                return average2(d);
        return average;
    }

    /**
     * 
     * @param d
     * @return
     */
    public static double average1(double[] d) { // Schneller
        if (d == null || d.length < 1)
            return Double.NaN;
        double retVal = 0;

        int countNonNAN = 0;
        for (int i = 0; i < d.length; i++) {
            if (Double.isNaN(d[i]) || Double.isInfinite(d[i]))
                continue;
            countNonNAN++;
            retVal += d[i];
        }

        if (countNonNAN <= 0)
            return Double.NaN;
        return (retVal / countNonNAN);
    }

    /**
     * 
     * @param d
     * @return
     */
    public static double[] average1(double[][] d) { // Schneller
        if (d.length < 1)
            return new double[0];
        double[] retVal = null;

        int countNonNull = 0;
        for (int i = 0; i < d.length; i++) {
            if (d[i] == null)
                continue; // kommt vor wenn er sequenz i nicht mappen kann
            countNonNull++;
            if (retVal == null)
                retVal = new double[d[i].length];
            for (int j = 0; j < d[i].length; j++)
                retVal[j] += d[i][j];
        }

        if (retVal == null)
            return null; // Koomt vor wenn er alle sequenzen nicht mappen kann
        for (int i = 0; i < retVal.length; i++)
            retVal[i] /= countNonNull;

        return retVal;
    }

    /**
     * 
     * @param d
     * @return
     */
    public static double average2(double[] d) { // Keine to-large-numbers
        if (d.length < 1)
            return Double.NaN;
        double retVal = 0;

        int countNonNAN = 0;
        for (int i = 0; i < d.length; i++) {
            if (Double.isNaN(d[i]) || Double.isInfinite(d[i]))
                continue;
            countNonNAN++;

            // retVal[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1);
            retVal = retVal * (countNonNAN - 1) / (countNonNAN) + d[i] * 1 / (countNonNAN);
        }

        // Wenn irgendwo nur NaNs waren, das auch so wiedergeben
        if (countNonNAN <= 0)
            return Double.NaN;
        return retVal;
    }

    /**
     * 
     * @param d
     * @return
     */
    public static double[] average2(double[][] d) { // Keine to-large-numbers
        if (d.length < 1)
            return new double[0];
        double[] retVal = null;
        ArrayList<Integer> spaltenCounter = new ArrayList<Integer>();
        for (int i = 0; i < d.length; i++) {
            if (d[i] == null)
                continue; // kommt vor wenn er sequenz i nicht mappen kann
            if (retVal == null)
                retVal = new double[d[i].length];
            for (int j = 0; j < d[i].length; j++) {
                if (spaltenCounter.size() <= j)
                    spaltenCounter.add(0);
                if (Double.isNaN(d[i][j]))
                    continue; // Deshalb auch der Spaltencounter: Skip NaN
                              // eintr?ge.
                              // retVal[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1);
                retVal[j] = retVal[j] * spaltenCounter.get(j) / (spaltenCounter.get(j) + 1)
                        + d[i][j] * 1 / (spaltenCounter.get(j) + 1);
                spaltenCounter.set(j, spaltenCounter.get(j) + 1);
            }
        }
        // Wenn irgendwo nur NaNs waren, das auch so wiedergeben
        for (int i = 0; i < spaltenCounter.size(); i++)
            if (spaltenCounter.get(i) == 0)
                retVal[i] = Double.NaN;
        return retVal;
    }
}

Related

  1. variance(double m[], double[][] o)
  2. variance(double[] data, double meanVal)
  3. variance(double[][] x, double[] meanVector, boolean isAlongRows)