Returns the correlation between the two arrays of data. The arrays are assumed to have the same lengths See - Wikipedia - Java java.lang

Java examples for java.lang:Math Array Function

Description

Returns the correlation between the two arrays of data. The arrays are assumed to have the same lengths See - Wikipedia

Demo Code

/*//from   w  ww .j  a  va2s .  co 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 correlation between the two arrays of data.</p>
     * <p>The arrays are assumed to have the same lengths</p>
     * <p>See - <a href="http://en.wikipedia.org/wiki/Correlation">Wikipedia</a>
     * </p>
     * 
     * @param x
     * @param y
     * @return the correlation
     */
    public static double correlation(double[] x, double[] y) {
        return correlation(x, y, x.length);
    }

    /**
     * <p>Returns the correlation between the two arrays of data.</p>
     * <p>See - <a href="http://en.wikipedia.org/wiki/Correlation">Wikipedia</a>
     * </p>
     * 
     * @param x
     * @param y
     * @param dataLength - number of terms in each vector to consider (we look at the first dataLength terms).
     *    Precondition: dataLength is less than min(x.length, y.length)
     * @return the correlation
     */
    public static double correlation(double[] x, double[] y, int dataLength) {
        // return covariance(x, y) / stdDev(x) / stdDev(y);
        // Save some code time by reusing the code from inside covariance:
        double c = 0;
        double meanX = mean(x, 0, dataLength);
        double meanY = mean(y, 0, dataLength);
        for (int t = 0; t < dataLength; t++) {
            c += (x[t] - meanX) * (y[t] - meanY);
        }
        double covariance = c / (double) (dataLength - 1);
        return covariance / stdDev(x, meanX, dataLength)
                / stdDev(y, meanY, dataLength);
    }

    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 stdDev(double[] array) {
        double mean = 0.0;
        double total = 0.0;
        for (int m = 0; m < array.length; m++) {
            total += array[m];
        }
        mean = total / (double) array.length;

        return stdDev(array, mean);
    }

    public static double stdDev(double[][] matrix, int column) {
        double mean = 0.0;
        double total = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            total += matrix[m][column];
        }
        mean = total / (double) matrix.length;

        return stdDev(matrix, column, mean);
    }

    /**
     * Return the standard deviation of all the elements in array
     * 
     * @param array
     * @param mean
     * @return
     */
    public static double stdDev(double[] array, double mean) {
        return stdDev(array, mean, array.length);
    }

    /**
     * Standard deviation for the first arrayLength terms of array
     * 
     * @param array
     * @param mean
     * @param arrayLength
     * @return
     */
    public static double stdDev(double[] array, double mean, int arrayLength) {
        if (arrayLength == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < arrayLength; m++) {
            sumSqs += (array[m] - mean) * (array[m] - mean);
        }
        double std = sumSqs / (double) (arrayLength - 1);
        std = Math.sqrt(std);
        return std;
    }

    /**
     * Compute the standard deviation along the given column, with the known
     *  given mean.
     * 
     * @param matrix
     * @param column
     * @param mean
     * @return
     */
    public static double stdDev(double[][] matrix, int column, double mean) {
        if (matrix.length == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            sumSqs += (matrix[m][column] - mean)
                    * (matrix[m][column] - mean);
        }
        double std = sumSqs / (double) (matrix.length - 1);
        std = Math.sqrt(std);
        return std;
    }

    /**
     * Compute the standard deviation across all values in the 2D matrix
     * 
     * @param matrix
     * @return
     */
    public static double stdDev(double[][] matrix) {
        double mean = mean(matrix);
        return stdDev(matrix, mean);
    }

    /**
     * Compute the standard deviation across all values in the 2D matrix
     * 
     * @param matrix
     * @param mean
     * @return
     */
    public static double stdDev(double[][] matrix, double mean) {
        if (matrix.length == 0) {
            return 0.0;
        }
        double sumSqs = 0.0;
        for (int m = 0; m < matrix.length; m++) {
            for (int c = 0; c < matrix[m].length; c++) {
                sumSqs += (matrix[m][c] - mean) * (matrix[m][c] - mean);
            }
        }
        double std = sumSqs
                / (double) ((matrix.length * matrix[0].length) - 1);
        std = Math.sqrt(std);
        return std;
    }

    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