Java examples for java.lang:Math Array Function
Computes the correlation between two arrays of the same length, p and q.
/** A collection of mathematical utility functions. * <p>/*w ww . j a v a2 s .co m*/ * Copyright (c) 2008 Eric Eaton * <p> * 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. * <p> * 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. * <p> * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * @author Eric Eaton (EricEaton@umbc.edu) <br> * University of Maryland Baltimore County * * @version 0.1 * */ //package com.java2s; public class Main { /** Computes the correlation between two arrays of the same length, p and q. * Computes the correlation between p and q as * r = (|p| * \sum_i(p[i]*q[i]) - \sum_i(p[i]) * \sum_i(q[i]))/ * sqrt((|p| * \sum_i((p[i])^2) - (\sum_i(p[i]))^2) * * (|p| * \sum_i((p[i])^2) - (\sum_i(p[i]))^2)) * This correlation can be tested for statistical significance via t-tests. * See e.g.: http://www.socialresearchmethods.net/kb/statcorr.htm * @return The correlation between the elements of the two arrays. */ public static double correlation(int[] p, int[] q) { if (p == null || q == null) { throw new IllegalArgumentException("p and q cannot be null"); } if (p.length != q.length) { throw new IllegalArgumentException( "p and q must be the same length"); } // compute the sums and squared sums int sumP = 0; int sumQ = 0; int sumPSquared = 0; int sumQSquared = 0; int sumPQ = 0; for (int i = 0; i < p.length; i++) { sumP += p[i]; sumQ += q[i]; sumPSquared += p[i] * p[i]; sumQSquared += q[i] * q[i]; sumPQ += p[i] * q[i]; } // compute the correlation double r = ((double) (p.length * sumPQ - sumP * sumQ)) / Math.sqrt(((long) (p.length * sumPSquared - sumP * sumP)) * ((long) (p.length * sumQSquared - sumQ * sumQ))); return r; } /** Computes the correlation between two arrays of the same length, p and q. * Computes the correlation between p and q as * r = (|p| * \sum_i(p[i]*q[i]) - \sum_i(p[i]) * \sum_i(q[i]))/ * sqrt((|p| * \sum_i((p[i])^2) - (\sum_i(p[i]))^2) * * (|p| * \sum_i((p[i])^2) - (\sum_i(p[i]))^2)) * This correlation can be tested for statistical significance via t-tests. * See e.g.: http://www.socialresearchmethods.net/kb/statcorr.htm * @return The correlation between the elements of the two arrays. */ public static double correlation(double[] p, double[] q) { if (p == null || q == null) { throw new IllegalArgumentException("p and q cannot be null"); } if (p.length != q.length) { throw new IllegalArgumentException( "p and q must be the same length"); } // compute the sums and squared sums double sumP = 0; double sumQ = 0; double sumPSquared = 0; double sumQSquared = 0; double sumPQ = 0; for (int i = 0; i < p.length; i++) { sumP += p[i]; sumQ += q[i]; sumPSquared += p[i] * p[i]; sumQSquared += q[i] * q[i]; sumPQ += p[i] * q[i]; } // compute the correlation double r = (p.length * sumPQ - sumP * sumQ) / Math.sqrt((p.length * sumPSquared - sumP * sumP) * (p.length * sumQSquared - sumQ * sumQ)); return r; } }