Computer Science‎ > ‎

### Stats, ML, Data: Computing the Pearson Product-Moment Correlation Coefficient

 Pearson product-moment correlation coefficientThe Person product-moment correlation coefficient is a measure of linear correlation described well on this Wikipedia page. The formula,  is given by: where x and y denote the two vectors between which the correlation is to be measured.Correlation coefficients are used to identify a mutual relationship and/or interdependence between 2 or more variables. Say, you are given a file with N rows, indicating the scores of candidates in three subjects A, B, C (each on a new line, space separated).You need to calculate the pearson coefficients between A and B, B and C, A and C. C++ Program to compute the pearson correlation coefficient between the pairs of variables `#include ``#include ``#include ``#include ``using namespace std;``int main(){` `int n;scanf("%d",&n);` `vector m(n),p(n),c(n);` `for(int i=0;i 0 return float(sum(x)) / len(x) def pearson_def(x, y): assert len(x) == len(y) n = len(x) assert n > 0 avg_x = average(x) avg_y = average(y) diffprod = 0 xdiff2 = 0 ydiff2 = 0 for idx in range(n): xdiff = x[idx] - avg_x ydiff = y[idx] - avg_y diffprod += xdiff * ydiff xdiff2 += xdiff * xdiff ydiff2 += ydiff * ydiff return diffprod / math.sqrt(xdiff2 * ydiff2) T = int(input()) arr1 = [] arr2 = [] arr3 = [] for i in range(0,T): x,y,z = a = map(int, input().split()) arr1.append(x) arr2.append(y) arr3.append(z) print("%.2f" % pearson_def(arr1,arr2)) print("%.2f" % pearson_def(arr2,arr3)) print("%.2f" % pearson_def(arr3,arr1))```````````` ``````