Java Array Covariance covariance(Number[] x, Number[] y)

Here you can find the source of covariance(Number[] x, Number[] y)

Description

Computes the covariance between the two data vectors and returns it.

License

Open Source License

Parameter

Parameter Description
x the first data array
y the second data array

Return

the computed correlation

Declaration

public static double covariance(Number[] x, Number[] y) 

Method Source Code

//package com.java2s;
/*/*from   w w w.j  a v a 2  s .  c  o  m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Computes the covariance between the two data vectors and returns it.
     *
     * Cov(X,Y) = Sum((Xi-Xbar)*(Yi-Ybar)) / n
     *
     * with n = length of vectors, Xi the ith element of X, Yi the ith element of Y,
     * Xbar the mean of X, Ybar the mean of Y.
     *
     * @param x      the first data array
     * @param y      the second data array
     * @return      the computed correlation
     */
    public static double covariance(double[] x, double[] y) {
        return covariance(toNumberArray(x), toNumberArray(y));
    }

    /**
     * Computes the covariance between the two data vectors and returns it.
     *
     * Cov(X,Y) = Sum((Xi-Xbar)*(Yi-Ybar)) / n
     *
     * with n = length of vectors, Xi the ith element of X, Yi the ith element of Y,
     * Xbar the mean of X, Ybar the mean of Y.
     *
     * @param x      the first data array
     * @param y      the second data array
     * @return      the computed correlation
     */
    public static double covariance(Number[] x, Number[] y) {
        double result;
        int i;
        double xbar;
        double ybar;

        if (x.length != y.length)
            throw new IllegalArgumentException("Arrays differ in length: " + x.length + " != " + y.length);
        if (x.length == 0)
            throw new IllegalArgumentException("0-length arrays provided!");

        xbar = mean(x);
        ybar = mean(y);
        result = 0;
        for (i = 0; i < x.length; i++)
            result += (x[i].doubleValue() - xbar) * (y[i].doubleValue() - ybar);
        result /= x.length;

        return result;
    }

    /**
     * Turns the byte array into a Byte array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(byte[] array) {
        Byte[] result;
        int i;

        result = new Byte[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Byte(array[i]);

        return result;
    }

    /**
     * Turns the short array into a Short array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(short[] array) {
        Short[] result;
        int i;

        result = new Short[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Short(array[i]);

        return result;
    }

    /**
     * Turns the int array into a Integer array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(int[] array) {
        Integer[] result;
        int i;

        result = new Integer[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Integer(array[i]);

        return result;
    }

    /**
     * Turns the long array into a Long array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(long[] array) {
        Long[] result;
        int i;

        result = new Long[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Long(array[i]);

        return result;
    }

    /**
     * Turns the float array into a Float array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(float[] array) {
        Float[] result;
        int i;

        result = new Float[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Float(array[i]);

        return result;
    }

    /**
     * Turns the double array into a Double array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(double[] array) {
        Double[] result;
        int i;

        result = new Double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Double(array[i]);

        return result;
    }

    /**
     * Returns the mean of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the mean
     */
    public static double mean(Number[] array) {
        double result;
        int i;

        if (array.length == 0)
            return Double.NaN;

        result = 0;

        for (i = 0; i < array.length; i++)
            result += array[i].doubleValue();

        result /= array.length;

        return result;
    }

    /**
     * Returns the mean of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the mean
     */
    public static double mean(int[] array) {
        return mean(toNumberArray(array));
    }

    /**
     * Returns the mean of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the mean
     */
    public static double mean(double[] array) {
        return mean(toNumberArray(array));
    }
}

Related

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