# How to Calculate Pearson's between every element of a list

• 02-13-2011, 02:12 AM
jetnor
How to Calculate Pearson's between every element of a list
Hi Guys,

I have got a List of Double Arrays and am trying to do Pearson's correlation between each array. So if there were 4 arrays in my list I would like to do a Parson's correlation calculation for the 1st and 2nd, 1st and 3rd, 1st and 4th, 2nd and 3rd, 2nd and 4th and finally 3rd and 4th. It is kind of like trying to calculate the factorial of a number but I can't seem to get all the for loops needed. To calculate pearsons I use the Apache.Math
Code:

```PearsonsCorrelation correlate=new PearsonsCorrelation(); correlate.correlation(x,y); //x and y are double arrays.```
Please find attached the full relevant code.
Code:

```        public static List<Double> calculate() throws IOException,                         InterruptedException, InvocationTargetException {                                 double[] array1 = { 10.1229, 10.0896, 10.0934, 10.2836, 10.0785,10.1563, 9.75105, 9.67281, 9.73634, 9.98839, 10.0357, 10.171,10.1408 };         double[] array2 = { 12.4988, 12.4364, 12.4627, 10.8281, 10.6717,10.6623, 10.035, 10.0366, 10.0564, 10.7333, 10.4123, 10.7356,10.4534 };     double[] array3 = { 9.38457, 9.31928, 9.2238, 9.43497, 9.2742,9.3654, 9.0936, 9.11032, 9.14497, 9.182, 9.25073, 9.25306, 9.32079 };         double[] array4 = { 11.5103, 11.4897, 11.4066, 11.8333, 11.68,11.6297, 12.8099, 12.7682, 13.0319, 11.5912, 11.6165, 11.612,11.4981 };         double[] array5 = { 9.14701, 9.01309, 8.95865, 9.13745, 9.24751,9.19279, 8.94742, 8.97269, 8.84318, 9.15947, 8.91094, 9.15372,9.16545 };         //add the above double arrays to an ArrayList                List<double[]> testList = new ArrayList<double[]>();             testList.add(array1);                 testList.add(array2);                 testList.add(array3);                 testList.add(array4);                 testList.add(array5);                 //create a new list to store the value of correlation between each array in the testList                 List<Double> CorrList = new ArrayList<Double>();                 //Initialise an instance of Pearsons Correlation from org.apache.commons.math.stat.correlation.                 PearsonsCorrelation correlate = new PearsonsCorrelation();                 //loop the Array List with all the arrays upon which calculations will be carried                 for (int i = 0; i < testList.size(); i++) {                         double[] x = testList.get(i);                         //y should be the next element after x.                         double[] y = testList.get(i++);                         //calculate Pearsons                         double e = correlate.correlation(x, y);                         //add the value of correlation for each iteration to List<Double>CorrList initialised before the for loop.                         CorrList.add(e);                 }                 return CorrList;         }         public static void main(String[] args) throws IOException,                         InterruptedException, InvocationTargetException {                 System.out.println(calculate());         } }```
If I have not managed to be clear enough on the description please post your questions as I will be checking regularly for any help.

Jetnor.
• 02-13-2011, 03:16 AM
pbrockway2
Quote:

I can't seem to get all the for loops needed.

Code:

```public class AllPairs {     public static void main (String[] args) {         int[] data = {1, 42, -1, 666};                     for(int firstNdx = 0; firstNdx < data.length - 1; firstNdx++) {             for(int secondNdx = firstNdx + 1; secondNdx < data.length; secondNdx++) {                 System.out.printf(                         "%d<%d is %s%n",                         data[firstNdx], data[secondNdx], data[firstNdx] < data[secondNdx]);             }         }     } }```
• 02-13-2011, 08:53 PM
jetnor
Quote:

Originally Posted by pbrockway2
Code:

```public class AllPairs {     public static void main (String[] args) {         int[] data = {1, 42, -1, 666};                     for(int firstNdx = 0; firstNdx < data.length - 1; firstNdx++) {             for(int secondNdx = firstNdx + 1; secondNdx < data.length; secondNdx++) {                 System.out.printf(                         "%d<%d is %s%n",                         data[firstNdx], data[secondNdx], data[firstNdx] < data[secondNdx]);             }         }     } }```

Hello Mate,

You are a life saviour. I got it to work.

Thank you,

Jetnori.

Code:

```//create a new list to store the value of correlation between each array in the testList                 List<Double> CorrList = new ArrayList<Double>();                 //Initialise an instance of Pearsons Correlation from org.apache.commons.math.stat.correlation.                 PearsonsCorrelation correlate = new PearsonsCorrelation();                 //loop the Array List with all the arrays upon which calculations will be carried                 for (int firstIndex = 0; firstIndex < testList.size(); firstIndex++) {                         for( int secondIndex=firstIndex+1; secondIndex<testList.size();secondIndex++){                                 double[]x= testList.get(firstIndex);                                 double[]y=testList.get(secondIndex);                                 double e = correlate.correlation(x, y);                                        //add the value of correlation for each iteration to List<Double>CorrList initialised before the for loop.                                 CorrList.add(e);                         }                                                                 }                 return CorrList;         }```