0% found this document useful (0 votes)
64 views3 pages

Correlation Analysis Program

This Java program calculates the correlation between two sets of data. It prompts the user to enter the number of data points and then the x and y values. It then calculates the mean of each data set, subtracts each value from the mean, multiplies the differences and sums them. It also calculates variances and divides the sum of multiplied differences by the square root of the product of the variances to determine the correlation coefficient. The program then analyzes the coefficient to determine if there is a positive, negative, or no correlation between the data sets.

Uploaded by

chirusagar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Correlation Analysis Program

This Java program calculates the correlation between two sets of data. It prompts the user to enter the number of data points and then the x and y values. It then calculates the mean of each data set, subtracts each value from the mean, multiplies the differences and sums them. It also calculates variances and divides the sum of multiplied differences by the square root of the product of the variances to determine the correlation coefficient. The program then analyzes the coefficient to determine if there is a positive, negative, or no correlation between the data sets.

Uploaded by

chirusagar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CORRELATION ANALYSIS PROGRAM /* and open the template in the editor. */ package corelation; import java.util.

Scanner; /** * * @author Admin */ public class Correlation { /** * @param args the command line arguments */ int x[], y[], n; double xmean, ymean, result; Scanner scanner = new Scanner(System.in); public void readData() { System.out.println("enter the number of dataitems"); n = Integer.parseInt(scanner.next()); x = new int[n]; y = new int[n]; System.out.println("enter the X values"); for (int i = 0; i < n; i++) { x[i] = Integer.parseInt(scanner.next()); } System.out.println("enter the Y values"); for (int i = 0; i < n; i++) { y[i] = Integer.parseInt(scanner.next()); }

} public void corelation() { int xtemp = 0; int ytemp = 0; double temp = 0.0; double xdenotemp = 0.0;

double ydenotemp = 0.0; for (int i = 0; i < n; i++) { xtemp = xtemp + x[i]; ytemp = ytemp + y[i]; } xmean = xtemp / n; ymean = ytemp / n; for (int i = 0; i < n; i++) { temp = temp + ((x[i] - xmean) * (y[i] - ymean)); xdenotemp = xdenotemp + Math.pow((x[i] - xmean), 2); ydenotemp = ydenotemp + Math.pow((y[i] - ymean), 2); } result = temp / (Math.sqrt(xdenotemp * ydenotemp)); if (result > 0 && result <= 1) { System.out.println(result + " there exist positive correlation"); } else if (result < 0 && result >= -1) { System.out.println(result + " there exist negative correlation"); } else if (result == 0) { System.out.println("no correlation exits"); } else { System.out.println("error"); } } public static void main(String[] args) { // TODO code application logic here Correlation correlation = new Correlation(); correlation.readData(); correlation.corelation(); } }

OUTPUT run: enter the number of dataitems 3 enter the X values 2 4 6 enter the Y values 1 3 5 1.0 there exist positive correlation BUILD SUCCESSFUL (total time: 19 seconds)

run: enter the number of dataitems 4 enter the X values 2 4 6 8 enter the Y values 9 7 3 1 -0.9899494936611665 there exist negative correlation BUILD SUCCESSFUL (total time: 20 seconds)

You might also like