Day 7: Pearson Correlation Coefficient I | 10 Days Of Statistics | HackerRank Solution

Hello coders, today we are going to solve Day 7: Pearson Correlation Coefficient I HackerRank Solution which is a Part of 10 Days of Statistics Series.

Day 7: Pearson Correlation Coefficient I

Objective

In this challenge, we practice calculating the Pearson correlation coefficient.

Task

Given two n-element data sets, X and Y, calculate the value of the Pearson correlation coefficient.

Input Format

The first line contains an integer, n, denoting the size of data sets X and Y.
The second line contains n space-separated real numbers (scaled to at most one decimal place), defining data set X.
The third line contains n space-separated real numbers (scaled to at most one decimal place), defining data set Y.

Constraints

  • 10 <= n <= 100
  • 1 <= xi <= 500, where xi is the ith value of data set X.
  • 1 <= yi <= 500, where yi is the ith value of data set Y.
  • Data set X contains unique values.
  • Data set Y contains unique values.

Output Format

Print the value of the Pearson correlation coefficient, rounded to a scale of 3 decimal places.

Sample Input

10
10 9.8 8 7.8 7.7 7 6 5 4 2 
200 44 32 24 22 17 15 12 8 4

Sample Output

0.612

Solution – Pearson Correlation Coefficient I

C++

#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;
}

Disclaimer: The above Problem (Pearson Correlation Coefficient I) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *