-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpearson_correlation.java
More file actions
70 lines (61 loc) · 1.68 KB
/
pearson_correlation.java
File metadata and controls
70 lines (61 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
double a[]=new double[n];
double b[]=new double[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextDouble();
}
for(int i=0;i<n;i++)
{
b[i]=sc.nextDouble();
}
double mean_a = calc_mean(a);
double mean_b = calc_mean(b);
double sda = calc_sd(a, mean_a);
double sdb = calc_sd(b, mean_b);
double pearson_coff=pearson(a,b,sda,sdb,mean_a,mean_b);
System.out.println(pearson_coff);
/*
System.out.println(mean_a);
System.out.println(mean_b);
System.out.println(sda);
System.out.println(sdb);
*/
}
static double calc_mean(double a[])
{
double sum=0;
for(double i:a)
{
sum=sum+i;
}
return sum/a.length;
}
static double calc_sd(double a[], double mean)
{
double temp=0;
for(double i:a)
{
temp = temp + Math.pow((i-mean),2);
}
return Math.sqrt(temp/a.length);
}
static double pearson(double a[], double b[], double sda, double sdb, double mean_a, double mean_b)
{
double sum=0;
for(int i=0;i<a.length;i++)
{
sum= sum + (a[i]-mean_a) * (b[i]-mean_b);
}
return sum/(sda*sdb*a.length);
}
}