Java Array Covariance covarianceTwoColumns(double[][] data, int col1, int col2)

Here you can find the source of covarianceTwoColumns(double[][] data, int col1, int col2)

Description

<p>Returns the covariance between two columns of data in a multivariate array.</p> <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a> </p>

License

Apache License

Parameter

Parameter Description
data multivariate array of data; first index is time, second is variable number
col1 variable number 1 to compute the covariance to
col2 variable number 2 to compute the covariance to

Return

the covariance

Declaration

public static double covarianceTwoColumns(double[][] data, int col1, int col2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Karlsruhe Institute of Technology. This Work has been partially supported by the EIT ICT Labs funded research project Towards a Mobile Cloud (activity CLD 12206).
 * /*  ww  w . jav  a2s . c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    /**
     * <p>Returns the covariance between two columns of data in
     *  a multivariate array.</p>
     * <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a>
     * </p>
     * 
     * @param data multivariate array of data; first index is time, second is 
     *    variable number
     * @param col1 variable number 1 to compute the covariance to
     * @param col2 variable number 2 to compute the covariance to
     * @return the covariance
     */
    public static double covarianceTwoColumns(double[][] data, int col1, int col2) {
        double mean1 = mean(data, col1);
        double mean2 = mean(data, col2);
        return covarianceTwoColumns(data, col1, col2, mean1, mean2);
    }

    /**
     * <p>Returns the covariance between two columns of data in
     *  a multivariate array.</p>
     * <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a>
     * </p>
     * 
     * @param data multivariate array of data; first index is time, second is 
     *    variable number
     * @param col1 variable number 1 to compute the covariance to
     * @param col2 variable number 2 to compute the covariance to
     * @param mean1 mean of variable 1
     * @param mean2 mean of variable 2
     * @return the covariance
     */
    public static double covarianceTwoColumns(double[][] data, int col1, int col2, double mean1, double mean2) {
        double c = 0;
        for (int t = 0; t < data.length; t++) {
            c += (data[t][col1] - mean1) * (data[t][col2] - mean2);
        }
        return c / data.length;
    }

    /**
     * <p>Returns the covariance between two columns of data in
     *  two multivariate arrays.</p>
     * <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a>
     * </p>
     * 
     * @param data1 first multivariate array of data; first index is time, second is 
     *    variable number
     * @param data2 second multivariate array of data; first index is time, second is 
     *    variable number
     * @param col1 variable number 1 to compute the covariance to
     * @param col2 variable number 2 to compute the covariance to
     * @param mean1 mean of variable 1
     * @param mean2 mean of variable 2
     * @return the covariance
     */
    public static double covarianceTwoColumns(double[][] data1, double[][] data2, int col1, int col2, double mean1,
            double mean2) {
        double c = 0;
        for (int t = 0; t < data1.length; t++) {
            c += (data1[t][col1] - mean1) * (data2[t][col2] - mean2);
        }
        return c / data1.length;
    }

    public static double mean(int[] input) {
        return sum(input) / (double) input.length;
    }

    public static double mean(double[] input) {
        return sum(input) / input.length;
    }

    public static double mean(double[] input, int startIndex, int length) {
        return sum(input, startIndex, length) / length;
    }

    public static double mean(double[][] input) {
        return sum(input) / (input.length * input[0].length);
    }

    /**
     * Compute the mean along the given column 
     * 
     * @param input
     * @param column
     * @return
     */
    public static double mean(double[][] input, int column) {
        return sum(input, column) / input.length;
    }

    public static double sum(double[] input) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i];
        }
        return total;
    }

    public static double sum(double[] input, int startIndex, int length) {
        double total = 0;
        for (int i = startIndex; i < startIndex + length; i++) {
            total += input[i];
        }
        return total;
    }

    public static double sum(double[][] input) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input[i].length; j++) {
                total += input[i][j];
            }
        }
        return total;
    }

    public static double sum(double[][] input, int column) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i][column];
        }
        return total;
    }

    public static int sum(int[] input) {
        int total = 0;
        for (int i = 0; i < input.length; i++) {
            total += input[i];
        }
        return total;
    }

    public static int sum(int[][] input) {
        int total = 0;
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input[i].length; j++) {
                total += input[i][j];
            }
        }
        return total;
    }
}

Related

  1. covariance(final double[] xArray, final double[] yArray)
  2. covariance(final double[][] data)
  3. covariance(int[] v1, int[] v2)
  4. covariance(Number[] x, Number[] y)
  5. covarianceOfDoubleArrays(double[] x, double[] y)