Java Array Covariance covariance(double[] x, double[] y, int delay)

Here you can find the source of covariance(double[] x, double[] y, int delay)

Description

<p>Returns the covariance between the two arrays of data, with a given lag between the first and second.</p> <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a> </p>

License

Open Source License

Parameter

Parameter Description
x time series 1
y time series 2
delay delay >= 0 to compute the covariance across (from first to second time series)

Return

the covariance

Declaration

public static double covariance(double[] x, double[] y, int delay) 

Method Source Code

//package com.java2s;
/*//www. ja v a 2s. 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/>.
 */

public class Main {
    /**
     * <p>Returns the covariance between the two arrays of data.</p>
     * <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a>
     * </p>
     * 
     * @param x
     * @param y
     * @return the covariance
     */
    public static double covariance(double[] x, double[] y) {
        double c = 0;
        double meanX = mean(x);
        double meanY = mean(y);
        for (int t = 0; t < x.length; t++) {
            c += (x[t] - meanX) * (y[t] - meanY);
        }
        return c / (double) (x.length - 1); // -1 for sample covariance
    }

    /**
     * <p>Returns the covariance between the two arrays of data, with
     * a given lag between the first and second.</p>
     * <p>See - <a href="http://mathworld.wolfram.com/Covariance.html">Mathworld</a>
     * </p>
     * 
     * @param x time series 1
     * @param y time series 2
     * @param delay delay >= 0 to compute the covariance across (from first to second time series)
     * @return the covariance
     */
    public static double covariance(double[] x, double[] y, int delay) {
        double meanX = 0, meanY = 0;
        // No error checking if y is same length as x
        for (int n = 0; n < x.length - delay; n++) {
            meanX += x[n];
            meanY += y[n + delay];
        }
        meanX /= (double) (x.length - delay);
        meanY /= (double) (x.length - delay);
        double c = 0;
        for (int t = 0; t < x.length - delay; t++) {
            c += (x[t] - meanX) * (y[t + delay] - meanY);
        }
        return c / (double) (x.length - delay - 1); // -1 for sample covariance
    }

    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 double sum(double[][][] input) {
        double total = 0;
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input[i].length; j++) {
                for (int k = 0; k < input[i][j].length; k++) {
                    total += input[i][j][k];
                }
            }
        }
        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;
    }

    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++) {
                for (int k = 0; k < input[i][j].length; k++) {
                    total += input[i][j][k];
                }
            }
        }
        return total;
    }
}

Related

  1. covariance(double[] a, double amean, double[] b, double bmean)
  2. covariance(double[] a, double[] b)
  3. covariance(double[] x, double[] y)
  4. covariance(final double[] xArray, final double[] yArray)
  5. covariance(final double[][] data)
  6. covariance(int[] v1, int[] v2)
  7. covariance(Number[] x, Number[] y)