HackerRank Day 7: Pearson Correlation Coefficient I | 10 Days of Statistics solution

In this Hackerrank Day 7: Pearson Correlation Coefficient I  10 Days of Statistics problem You have given two n-element data sets, X and Y, to calculate the value of the Pearson correlation coefficient.

HackerRank Day 7: Pearson Correlation Coefficient I | 10 Days of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
N = int(input())
X = list(map(float,input().strip().split()))
Y = list(map(float,input().strip().split()))

mu_x = sum(X) / N
mu_y = sum(Y) / N

stdv_x = (sum([(i - mu_x)**2 for i in X]) / N)**0.5
stdv_y = (sum([(i - mu_y)**2 for i in Y]) / N)**0.5


covariance = sum([(X[i] - mu_x) * (Y[i] -mu_y) for i in range(N)])

correlation_coefficient = covariance / (N * stdv_x * stdv_y)

print(round(correlation_coefficient,3))



Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        double[] xValues = readDoubles(in, n);
        double[] yValues = readDoubles(in, n);
        double pearsonCorrelation = calculatePearsonCorrelation(xValues, yValues);
        System.out.println(String.format("%.3f", pearsonCorrelation));
    }
    
    private static double[] readDoubles(Scanner in, int size) {
        double[] array = new double[size];
        for(int i = 0; i < size; i++) array[i] = in.nextDouble();
        return array;
    }
    
    private static double calculatePearsonCorrelation(double[] xValues, double[] yValues) {
        double xMean = calculateMean(xValues);
        double yMean = calculateMean(yValues);
        double xStdDev = calculateStdDev(xValues, xMean);
        double yStdDev = calculateStdDev(yValues, yMean);
        double upperSum = 0;
        for(int i = 0; i < xValues.length; i++) {
            upperSum += (xValues[i] - xMean) * (yValues[i] - yMean);
        }
        return upperSum / (xValues.length * xStdDev * yStdDev);
    }
    
    private static double calculateMean(double[] values) {
        double sum = 0;
        for(double value : values) sum += value;
        return sum / values.length;
    }
    
    private static double calculateStdDev(double[] values, double mean) {
        double upperSum = 0;
        for(double value : values) {
            upperSum += (value - mean) * (value - mean);
        }
        return Math.sqrt(upperSum / values.length);
    }
}


Problem solution in C++ programming.

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

double mean(double x[], int n)
{
    double res = 0.0;
    for(int i = 0; i < n; i++)
        res += x[i];
    return res / n;
}

double sdev(double x[], int n, double m)
{
    double res = 0.0;
    for(int i = 0; i < n; i++)
        res += (x[i] - m)*(x[i] - m);
    return sqrt(res / n);
}

double corCoef(double x[], double y[], int n)
{
    double mx = mean(x, n);
    double my = mean(y, n);
    double sdx = sdev(x, n, mx);
    double sdy = sdev(y, n, my);
    double res = 0.0;
    for(int i = 0; i < n; i++)
        res += (x[i] - mx)*(y[i] - my);
    return res / (n*sdx*sdy);
}


int main(void)
{
    int n;
    cin >> n;
    double x[n];
    double y[n];
    for(int i = 0; i < n; i++)
        cin >> x[i];
    for(int i = 0; i < n; i++)
        cin >> y[i];
    cout << fixed << setprecision(3);
    cout << corCoef(x, y, n) << endl;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n;
    scanf("%d", &n);
    
    double *x, sumx=0.0;
    x = malloc(n * sizeof (double));
	for (int i = 0; i < n; i++) {
		scanf("%lf", x + i);
        sumx += *(x+i);
	} 
    
    double *y, sumy=0;
    y = malloc(n * sizeof (double));
	for (int i = 0; i < n; i++) {
		scanf("%lf", y + i);
        sumy += *(y+i);
	} 
    
    double mux = sumx/n;
    double muy = sumy/n;
    
    double ssx=0, ssy=0, devy=0, devx=0, devxy;
    for (int i = 0; i < n; i++) {
		devx = *(x+i) - mux;
        ssx+= pow(devx,2);
        devy = *(y+i) - muy;
        ssy+= pow(devy,2);
        devxy += devx*devy;
	}
    
    double sdx = pow(ssx/n,.5);
    double sdy = pow(ssy/n,.5);
    double corr = devxy/(sdx*sdy*n);
    
   
    printf("%.3lf\n", corr);
    
    return 0;
}


Problem solution in JavaScript programming.

const mean = X => X.reduce((a, c) => a + c) / X.length;
const sumOfSqDistFromMean = X => X.map(x => (x - mean(X)) ** 2).reduce((a, c) => a + c);
const standardDev = X => Math.sqrt(sumOfSqDistFromMean(X) / (X.length));

function standardDeviation(x, n) {
    let v = 0,
        m = mean(x, n);

    for (let i = n; i-- > 0;)
        v += Math.pow(x[i] - m, 2);

    v /= n - 1;

    return Math.sqrt(v);
}

const print = x => console.log(x.toFixed(3));

function processData(input) {
    const [[n], X, Y] = input.split`\n`.map(l => l.trim().split` `.map(Number));
    console.error(n, X, Y)
    
    const meanX = mean(X);
    const meanY = mean(Y);
    // console.error(meanX, meanY);
    
    const sdX = standardDev(X);
    const sdY = standardDev(Y);
    // console.error(sdX, sdY);
    
    console.error(meanX, sdX);
    console.error(meanY, sdY);
    console.error(standardDeviation(X, n))
    console.error(standardDeviation(Y, n))
    
    let sum = 0;
    for (let i = 0; i < n; i++) {
        const temp = (X[i] - meanX) * (Y[i] - meanY);
        // console.error(i, temp)
        sum += temp;
    }
    // console.error(sum)
    const covXY = sum / n;
    // console.error(covXY);
    
    print(covXY / (sdX * sdY));

} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments