# HackerRank Day 8: Least Square Regression Line | 10 Days of Statistics solution

In this Hackerrank Day 8: Least Square Regression Line 10 Days of Statistics problem A group of five students enroll in Statistics immediately after taking a Math aptitude test. Each student's Math aptitude test score, x, and Statistics course grade, y, can be expressed as the following list of (x,y) points:

(95,85)

(85,95)

(80,70)

If a student scored an 80 on the Math aptitude test, what grade would we expect them to achieve in Statistics? Determine the equation of the best-fit line using the least-squares method, then compute and print the value of y when x=80.

## Problem solution in Python programming.

```# Enter your code here. Read input from STDIN. Print output to STDOUT
n = 5
xy = [map(int, input().split()) for _ in range(n)]
sx, sy, sx2, sxy = map(sum, zip(*[(x, y, x**2, x * y) for x, y in xy]))
b = (n * sxy - sx * sy) / (n * sx2 - sx**2)
a = (sy / n) - b * (sx / n)
print('{:.3f}'.format(a + b * 80))```

## 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 = 5;
double[] x = { 95.0, 85.0, 80.0, 70.0, 60.0 };
double[] y = { 85.0, 95.0, 70.0, 65.0, 70.0 };
double xMean = 0, yMean = 0;
for (int i = 0; i < n; i++)
{
xMean += x[i];
yMean += y[i];
}
xMean /= n;
yMean /= n;
double xStdDev = 0, yStdDev = 0;
for (int i = 0; i < n; i++)
{
xStdDev += x[i] * x[i];
yStdDev += y[i] * y[i];
}
xStdDev = Math.sqrt(xStdDev / n - xMean * xMean);
yStdDev = Math.sqrt(yStdDev / n - yMean * yMean);
double corr = 0;
for (int i = 0; i < n; i++)
corr += (x[i] - xMean) * (y[i] - yMean);
corr /= n * xStdDev * yStdDev;
double b = corr * yStdDev / xStdDev;
double a = yMean - b * xMean;
System.out.printf("%.3f\n", b * 80 + a);
}
}```

## Problem solution in C++ programming.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */

float mathMean=0;
float statMean=0;
float xy=0;
float x2=0;
float sumX=0;
float sumY=0;
for(int i=0;i<N;++i)
{
}
mathMean=sumX/N;
statMean=sumY/N;

float b=(N*xy-sumX*sumY)/(N*x2-sumX*sumX);
float a=statMean-b*mathMean;

cout<<setprecision(3)<<fixed<<a+b*80<<endl;

return 0;
}```

## Problem solution in C programming.

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

int main() {
int n = 5;
float x[] = {95, 85, 80, 70, 60};
float y[] = {85, 95, 70, 65, 70};
float ys = 0.0;
float xs = 0.0;
float x2s = 0.0;
float xys = 0.0;
for (int i = 0; i < n; i++)
{
xs += x[i];
ys += y[i];
xys += x[i] * y[i];
x2s += x[i] * x[i];
}
float b = (n * xys - xs * ys) / (n*x2s - xs*xs);
float a = ys / n - b * (xs / n);
printf("%.3f", a + b*80);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}```

## Problem solution in JavaScript programming.

```function calcMean(array) {
return array.reduce((p,c) => p + c)/array.length
}

function calcSt(array, mean){
let sum = array.map(item=>(item-mean)**2).reduce((p,c)=>p+c)
let sd = (sum/array.length)**(0.5)
return sd
}

function parseArray(lines,n) {
let arr = []
for(var i = 0; i < n;i++) {
arr.push(parseFloat(lines[i]))
}
return arr
}

function pearsonFunc(x,meanX, stX, y, meanY, stY) {
let n = x.length
let sum = 0
for(var i = 0; i < n; i++) {
sum += (x[i] - meanX) * (y[i] - meanY)
}
return sum/(n * stX * stY)

}

function processData(input) {
let x = [95,85,80,70,60]
let y = [85,95,70,65,70]
let meanX = calcMean(x)
let meanY = calcMean(y)
let stX = calcSt(x,meanX)
let stY = calcSt(y,meanY)

let pearson = pearsonFunc(x,meanX, stX, y, meanY, stY)
let b = pearson*stY/stX
let a = meanY - b*meanX

let ans = a + b*80
console.log(ans.toFixed(3))
}

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

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