Here you can find the source of covarianceOfDoubleArrays(double[] x, double[] y)
Parameter | Description |
---|---|
x | the one array |
y | the other array |
public static double covarianceOfDoubleArrays(double[] x, double[] y)
//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; } }