-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspearman_corelation.java
More file actions
97 lines (84 loc) · 2.31 KB
/
spearman_corelation.java
File metadata and controls
97 lines (84 loc) · 2.31 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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];
int flag= eqaulity_checker(a);
int flag2= eqaulity_checker(b);
if(flag+flag2>0)
{
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);
}
else
double r= spearman(a, b);
System.out.println(r);
}
static int eqaulity_checker(Double a[])
{
int flag=0;
Arrays.sort(a);
for(int i=1;i<n;i++)
{
for (int j=i-1; j<n-1 ;j++ )
{
if(a[i]==a[j])
{
flag=1;
break;
}
}
}
return flag;
}
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);
}
static double spearman(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
}
}
static double covariance(double a, double b, double sda, double sdb, double mean_a, double mean_b)
{
return (a*mean_a) + (b*mean_b)/(sda*sdb);
}
}