Java Array Covariance covarianceOfDoubleArrays(double[] x, double[] y)

Here you can find the source of covarianceOfDoubleArrays(double[] x, double[] y)

Description

computes the covariance between two arrays

License

Open Source License

Parameter

Parameter Description
x the one array
y the other array

Return

the correlation

Declaration

public static double covarianceOfDoubleArrays(double[] x, double[] y) 

Method Source Code

//package com.java2s;
/*//from w  w  w.j  a v  a 2  s  .  c  o m
 * Copyright (C) 2010-2014  Andreas Maier
 * CONRAD is developed as an Open Source project under the GNU General Public License (GPL).
*/

public class Main {
    /**
     * computes the covariance between two arrays
     *
     * @param x the one array
     * @param y the other array
     * @return the correlation
     */
    public static double covarianceOfDoubleArrays(double[] x, double[] y) {
        double meanX = computeMean(x);
        double meanY = computeMean(y);
        double covariance = 0;
        for (int i = 0; i < x.length; i++) {
            covariance += ((x[i] - meanX) * (y[i] - meanY)) / x.length;
        }
        return covariance / x.length;
    }

    /**
     * computes the mean of the array "values" on the interval [start, end].
     * @param values the array
     * @param start the start index
     * @param end the end index
     * @return the mean value
     */
    public static double computeMean(double[] values, int start, int end) {
        double revan = 0;
        for (int i = start; i <= end; i++) {
            revan += values[i];
        }
        revan /= end - start + 1;
        return revan;
    }

    /**
     * Computes the mean value of a given array
     * @param array the array
     * @return the mean value as double
     */
    public static double computeMean(double[] array) {
        double mean = 0;
        for (int i = 0; i < array.length; i++) {
            mean += array[i];
        }
        return mean / array.length;
    }
}

Related

  1. covariance(double[] x, double[] y, int delay)
  2. covariance(final double[] xArray, final double[] yArray)
  3. covariance(final double[][] data)
  4. covariance(int[] v1, int[] v2)
  5. covariance(Number[] x, Number[] y)
  6. covarianceTwoColumns(double[][] data, int col1, int col2)