Here you can find the source of covariance(double[] a, double[] b)
Parameter | Description |
---|---|
a | a parameter |
b | a parameter |
public static final double covariance(double[] a, double[] b)
//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; } }