Pearson correlation coefficient

In statistics, the Pearson product-moment correlation coefficient (sometimes referred to as the PMCC, and typically denoted by r) is a measure of the correlation (linear dependence) between two variables X and Y, giving a value between +1 and −1 inclusive.

    /**
     *   calculate Pearson product-moment correlation coefficient
     * @param point1     first point
     * @param point2     second point
     * @return     Pearson correlation coefficient
     */
    public static double getPMCC(double[] point1, double[] point2) {
        double suma = 0;
        double sumb = 0;
        double sumaSq = 0;
        double sumbSq = 0;
        double pSum = 0;
        int n = point1.length;
        for (int i = 0; i < point1.length; i++) {
            suma = suma + point1[i];
            sumb = sumb + point2[i];
            sumaSq = sumaSq + point1[i] * point1[i];
            sumbSq = sumbSq + point2[i] * point1[i];
            pSum = pSum + point1[i] * point2[i];
        }
        double numerator = pSum - suma * sumb / n;
        double denominator = Math.sqrt((sumaSq - suma * suma / n) * (sumbSq - sumb * sumb / n));
        return numerator / denominator;
    }

Tags: ,

One Response to “Pearson correlation coefficient”

  1. mikkom 07. Jun, 2010 at 8:03 am #

    I think there is a typo in the above formula, note that sumbSq is not actually point2*point2.

Leave a Reply