-
Notifications
You must be signed in to change notification settings - Fork 12
/
Day-7_PearsonCorrelationCoefficient-I.cpp
61 lines (46 loc) · 1.25 KB
/
Day-7_PearsonCorrelationCoefficient-I.cpp
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
double pearson(double x[], double y[], int n, double mean1, double std1, double mean2, double std2)
{
double num = 0;
for(auto i=0; i<n; i++)
{
num += ((x[i]-mean1)*(y[i]-mean2));
}
double den = (n*std1*std2);
double res = num/den;
return res;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n; cin>>n; double x[n+4], y[n+4]; double sum1=0, sum2=0, sum3=0, sum4=0;
for(auto i=0; i<n; i++)
{
cin>>x[i];
sum1 += x[i];
}
double mean1 = (double)(sum1/n);
for(auto i=0; i<n; i++)
{
sum2 += (((double)x[i]-mean1)*((double)x[i]-mean1));
}
double std1 = (double)sqrt(sum2/n);
for(auto i=0; i<n; i++)
{
cin>>y[i];
sum3 += y[i];
}
double mean2 = (double)(sum3/n);
for(auto i=0; i<n; i++)
{
sum4 += (((double)y[i]-mean2)*((double)y[i]-mean2));
}
double std2 = (double)sqrt(sum4/n);
double pearson_coeff = pearson(x, y, n, mean1, std1, mean2, std2);
printf("%0.3f", pearson_coeff);
return 0;
}