Returns the covariance between two columns of data in a multivariate array. See - Mathworld - Java java.lang

Java examples for java.lang:Math Array Function

Description

Returns the covariance between two columns of data in a multivariate array. See - Mathworld

Demo Code

/*//from ww  w. j  a v a  2s  .c  o m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  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/>.
 */
//package com.java2s;

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 / (double) (data.length - 1);
    }

    /**
     * <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 / (double) (data1.length - 1);
    }

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

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

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

    public static double mean(double[][] input) {
        return sum(input) / (double) (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) / (double) 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 Tutorials