Java Array Covariance covariance(double[] a, double[] b)

Here you can find the source of covariance(double[] a, double[] b)

Description

Calculates the covariance between the two double arrays a and b.

License

Open Source License

Parameter

Parameter Description
a a parameter
b a parameter

Declaration

public static final double covariance(double[] a, double[] b) 

Method Source Code

//package com.java2s;
//  it under the terms of the GNU General Public License as published by

public class Main {
    /**//from ww w . j ava2 s  .com
     * Calculates the covariance between the two double arrays a and b.
     * @param a
     * @param b
     * @return
     */
    public static final double covariance(double[] a, double[] b) {
        if (a.length != b.length) {
            System.err.println("Arrays are not of the same size!");

            return Double.NaN;
        }

        double sumA = 0.0;
        double sumB = 0.0;
        double m_A = 0.0;
        double m_B = 0.0;
        double sum = 0.0;

        for (int i = 0; i < a.length; i++) {
            sumA += a[i];
            sumB += b[i];
        }

        m_A = sumA / (double) a.length;
        m_B = sumB / (double) b.length;

        for (int i = 0; i < a.length; i++) {
            sum += ((a[i] - m_A) * (b[i] - m_B));
        }

        return sum / (double) a.length;
    }
}

Related

  1. covariance(double[] a, double amean, double[] b, double bmean)
  2. covariance(double[] x, double[] y)
  3. covariance(double[] x, double[] y, int delay)
  4. covariance(final double[] xArray, final double[] yArray)
  5. covariance(final double[][] data)