#### Background: 
 

A linear regression line has an equation in the form `$Y = a + bX$`, where `$X$` is the explanatory variable and `$Y$` is the dependent variable. The parameter `$b$` represents the *slope* of the line, while `$a$` is called the *intercept* (the value of `$y$` when `$x = 0$`).

For more details visit the related [wikipedia page](http://en.wikipedia.org/wiki/Simple_linear_regression).

---
---
#### Task:
The function that you have to write accepts two list/array, `$x$` and `$y$`,  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:

   

`$x_i$` and `$y_i$` is `$x$` and `$y$` co-ordinate of `$i$`-th point;  
`$n$` is length of input.  

`$a =  \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 =  \dfrac{n\sum x_i y_i - \sum x_i \cdot \sum y_i}{n\sum x^2_i - (\sum x_i)^2}$`
#### Examples:

```python
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)
```
----

Difficulty: EASY
Tags: Geometry

Fundamentals

Mathematics

Starter code:
def regressionLine(x, y):
	

Solutions:
import numpy as np

def regressionLine(x, y):
	(a, b) = np.polyfit(x, y, 1)
	return (round(b, 4), round(a, 4))


def regressionLine(x, y):
	x2 = [float(i ** 2) for i in x]
	y2 = [float(i ** 2) for i in y]
	xy = [x[i] * y[i] for i in range(0, len(x))]
	a = (sum(x2) * sum(y) - sum(x) * sum(xy)) / (len(x) * sum(x2) - sum(x) ** 2)
	b = (len(x) * sum(xy) - sum(x) * sum(y)) / (len(x) * sum(x2) - sum(x) ** 2)
	return (round(a, 4), round(b, 4))


def regressionLine(x, y):
	n = float(len(x))
	sum_y = sum(y)
	sum_x = sum(x)
	sum_x_squared = sum([x_i * x_i for x_i in x])
	sum_x_y = sum([x_i * y_i for (x_i, y_i) in zip(x, y)])
	divisor = n * sum_x_squared - sum_x * sum_x
	intercept = (sum_x_squared * sum_y - sum_x * sum_x_y) / divisor
	slope = (n * sum_x_y - sum_x * sum_y) / divisor
	return (round(intercept, 4), round(slope, 4))


def regressionLine(xs, ys):
	(sx, sy, sxy) = (sum(xs), sum(ys), sum((x * y for (x, y) in zip(xs, ys))))
	(n, sx2, s2x) = (len(xs), sum((x * x for x in xs)), sx * sx)
	return tuple((round(d / (n * sx2 - s2x), 4) for d in (sx2 * sy - sx * sxy, n * sxy - sx * sy)))


def regressionLine(x, y):
	(n, sx, sy) = (len(x), sum(x), sum(y))
	sxy = sum((i * j for (i, j) in zip(x, y)))
	(sx_2, s_x2) = (sx ** 2, sum((i * i for i in x)))
	a = (s_x2 * sy - sx * sxy) / (n * s_x2 - sx_2)
	b = (n * sxy - sx * sy) / (n * s_x2 - sx_2)
	return (round(a, 4), round(b, 4))


def regressionLine(x, y):
	Ex2 = sum((i ** 2 for i in x))
	Exy = sum((i * j for (i, j) in zip(x, y)))
	Ex = sum(x)
	Ey = sum(y)
	n = len(x)
	return (round((Ex2 * Ey - Ex * Exy) / (n * Ex2 - Ex ** 2), 4), round((n * Exy - Ex * Ey) / (n * Ex2 - Ex ** 2), 4))


def regressionLine(x, y):
	sumXSquare = sum([n * n for n in x])
	sumYsquare = sum([n * n for n in y])
	sumXY = sum([X * Y for (X, Y) in zip(x, y)])
	a = (sumXSquare * sum(y) - sum(x) * sumXY) / float(len(x) * sumXSquare - sum(x) ** 2)
	b = (len(x) * sumXY - sum(x) * sum(y)) / float(len(x) * sumXSquare - sum(x) ** 2)
	a = round(a, 4)
	b = round(b, 4)
	return (a, b)


def regressionLine(x, y):
	sq = lambda p: sum([i * i for i in p])
	xy = sum(map(lambda p, q: p * q, x, y))
	return (round((sq(x) * sum(y) - sum(x) * xy) / (len(x) * sq(x) - sum(x) * sum(x)), 4), round((len(x) * xy - sum(x) * sum(y)) / (len(x) * sq(x) - sum(x) * sum(x)), 4))