Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Program to calculate Karl Pearson's correlation coefficient.
- */
- import java.util.*;
- public class KPearson_Correlation
- {
- static int[] calculateXY(int[] x, int[] y) //Method to get XY.
- {
- int num = x.length;
- int i, prod;
- int[] xy = new int[num];
- for(i=0;i<num;i++)
- {
- prod = x[i]*y[i];
- xy[i] = prod;
- prod = 0;
- }
- return xy;
- }
- static int[] calculateX2(int[] x) //Method to get X2.
- {
- int num = x.length;
- int i, xsquare;
- int[] x2 = new int[num];
- for(i=0;i<num;i++)
- {
- xsquare = x[i]*x[i];
- x2[i] = xsquare;
- xsquare = 0;
- }
- return x2;
- }
- static int[] calculateY2(int[] y) //Method to get Y2.
- {
- int num = y.length;
- int i, ysquare;
- int[] y2 = new int[num];
- for(i=0;i<num;i++)
- {
- ysquare = y[i]*y[i];
- y2[i] = ysquare;
- ysquare = 0;
- }
- return y2;
- }
- static long getSum(int[] num) //Method to get the sum of any list.
- {
- int sum = 0;
- int i;
- for(i=0;i<num.length;i++)
- {
- sum += num[i];
- }
- return sum;
- }
- static void display(int[] x, int[] y ,int[] xy, int[] x2, int[] y2) //Method to neatly display all the lists along with their sums.
- {
- System.out.println();
- System.out.println("X\tY\tXY\tX^2\tY^2"); //Displaying the headings.
- int num = x.length;
- int i;
- for(i=0;i<num;i++)
- {
- System.out.println(x[i]+"\t"+y[i]+"\t"+xy[i]+"\t"+x2[i]+"\t"+y2[i]); //Displaying the values.
- }
- System.out.println("---\t---\t---\t---\t---");
- System.out.println(getSum(x)+"\t"+getSum(y)+"\t"+getSum(xy)+"\t"+getSum(x2)+"\t"+getSum(y2)); //Displaying the sums.
- System.out.println("---\t---\t---\t---\t---");
- }
- static double getAnswer(int[] x, int[] y ,int[] xy, int[] x2, int[] y2) //Method to get the correlation coefficient [r].
- {
- long xsum, ysum, xysum, x2sum, y2sum;
- int num = x.length;
- xsum = getSum(x);
- ysum = getSum(y);
- xysum = getSum(xy);
- x2sum = getSum(x2);
- y2sum = getSum(y2);
- double r;
- r = ( xysum - ((xsum*ysum)/num) ) / ( Math.sqrt(x2sum - ((xsum * xsum) / num)) * Math.sqrt(y2sum - ((ysum * ysum) / num)) );
- return r;
- }
- public static void main(String[] args)
- {
- //Getting the user's input.
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter the number of items to be placed in both columns.");
- int num = sc.nextInt();
- int[] x = new int[num];
- int[] y = new int[num];
- System.out.println("Please enter the values in the first column. [X]");
- int i;
- for(i=0;i<x.length;i++)
- {
- x[i] = sc.nextInt();
- }
- System.out.println("Please enter the values in the second column. [Y]");
- for(i=0;i<x.length;i++)
- {
- y[i] = sc.nextInt();
- }
- //Getting the other lists required.
- int[] xy = calculateXY(x,y);
- int[] x2 = calculateX2(x);
- int[] y2 = calculateY2(y);
- //Displaying everything.
- display(x,y,xy,x2,y2);
- //Calculating the correlation coefficient [r].
- double r = getAnswer(x,y,xy,x2,y2);
- System.out.println("Correlation = "+r);
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment