Results 1 to 3 of 3
  1. #1
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default 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
    Java Code:
    PearsonsCorrelation correlate=new PearsonsCorrelation();
    correlate.correlation(x,y);
    //x and y are double arrays.
    Please find attached the full relevant code.
    Java 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.

    Thanks in advance,

    Jetnor.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

    Java 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]);
                }
            }
        }
    }

  3. #3
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Java 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.

    Java 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;
    	}

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  2. Replies: 1
    Last Post: 05-20-2010, 11:17 PM
  3. Replies: 0
    Last Post: 03-03-2009, 09:46 AM
  4. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 06:22 AM
  5. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 05:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •