# How to apply weightings to least squares slope formula

Im using this formula to find the slope of the regression line of given $x,y$ samples using LS calculation :

$a=\frac{(n\sum xy-\sum x\sum y)}{(n\sum x^2-(\sum x)^2)}$

How do i change it to apply weightings?

In other words i want to recieve $x,y,w$ samples such that the importance of $x,y$ distance from the resulting line will be determined by $w$.

In the case where you want to introduce weights for the linear regression $y=ax+b$, you need to minimize $$SSQ=\sum_{i=1}^n w_i(a x_i+b-y_i)^2$$ Computing, as usual, the derivatives with respect to $a$ and $b$ you end with $$\left(\sum_{i=1}^n w_i x_i^2\right)a+\left(\sum_{i=1}^n w_i x_i\right)b=\left(\sum_{i=1}^n w_i x_iy_i\right)$$ $$\left(\sum_{i=1}^n w_i x_i\right)a+\left(\sum_{i=1}^n w_i \right)b=\left(\sum_{i=1}^n w_i y_i\right)$$ Solving the two linear equations for $a,b$ would give the expressions.
Check that, if all $w_i=1$, you get the standard expression you wrote.
• So you are saying $\frac{\sum w \sum wxy - \sum wx \sum wy}{\sum w \sum wx^2 - (\sum wx)^2}$? – Ofek Ron Feb 22 '17 at 13:45
What do you know about the observations in $w$? Are those weights, that is, in $[0,1]$. Then why don't you just multiply your original $x$ by $w, getting$x'$, and then apply your original formula to$y$and$x'\$?