6 kyu

Linear Regression of Y on X

1,363 of 1,629ahmedbilalkhalid

Description:

Background:

sample linear regression, curtesy of wikipedia

A linear regression line has an equation in the form Y=a+bXY = a + bX, where XX is the explanatory variable and YY is the dependent variable. The parameter bb represents the slope of the line, while aa is called the intercept (the value of yy when x=0x = 0).

For more details visit the related wikipedia page.



Task:

The function that you have to write accepts two list/array, xx and yy, representing the coordinates of the points to regress (so that, for example, the first point has coordinates (x[0], y[0])).

Your function should return a tuple (in Python) or an array (any other language) of two elements: a (intercept) and b (slope) in this order.

You must round your result to the first 4 decimal digits

Formula:

xix_i and yiy_i is xx and yy co-ordinate of ii-th point;
nn is length of input.

a=xi2yixixiyinxi2(xi)2a = \dfrac{\sum x_i^2\cdot \sum y_i - \sum x_i \cdot\sum x_iy_i}{n\sum x_i^2 - (\sum x_i)^2}

b=nxiyixiyinxi2(xi)2b = \dfrac{n\sum x_i y_i - \sum x_i \cdot \sum y_i}{n\sum x^2_i - (\sum x_i)^2}

Examples:

regressionLine([25,30,35,40,45,50], [78,70,65,58,48,42]) == (114.381, -1.4457)

regressionLine([56,42,72,36,63,47,55,49,38,42,68,60], [147,125,160,118,149,128,150,145,115,140,152,155]) == (80.7777, 1.138)
regression_line([25,30,35,40,45,50], [78,70,65,58,48,42]) === [114.381, -1.4457]

regressionLine([56,42,72,36,63,47,55,49,38,42,68,60], [147,125,160,118,149,128,150,145,115,140,152,155]) === [80.7777, 1.138]
regressionLine([25,30,35,40,45,50], [78,70,65,58,48,42]) == [114.381, -1.4457]

regressionLine([56,42,72,36,63,47,55,49,38,42,68,60], [147,125,160,118,149,128,150,145,115,140,152,155]) == [80.7777, 1.138]

Statistics
Geometry
Fundamentals
Data Science

Similar Kata:

Stats:

CreatedMar 27, 2015
PublishedMar 27, 2015
Warriors Trained2922
Total Skips278
Total Code Submissions4039
Total Times Completed1629
Python Completions1363
JavaScript Completions227
Ruby Completions75
Total Stars95
% of votes with a positive feedback rating92% of 181
Total "Very Satisfied" Votes156
Total "Somewhat Satisfied" Votes22
Total "Not Satisfied" Votes3
Ad
Contributors
  • ahmedbilalkhalid Avatar
  • jhoffner Avatar
  • jolaf Avatar
  • GiacomoSorbi Avatar
  • user9644768 Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad