Regression Analysis is a method for identifying relationships between variables, especially useful for elucidating linear relationships. Simple Linear Regression is the simplest among them, referring to regression analysis on one dependent (response) variable and one independent (explanatory) variable.
The statement that independent variable xi and dependent variable yi have a linear relationship means that for some a,b, it can be expressed as yi=axi+b. Of course, there will inevitably be errors concerning the actual data, so more precisely, it includes an error term, becoming yi=axi+b+εi. Converting this expression into a form more commonly used in regression analysis
yi=β0+β1xi+εi
When represented as a design matrix,
y1y2⋮yn=11⋮1x1x2⋮xn[β0β1]+ε1ε2⋮εn
and by arranging, we get Y=Xβ+ε.
Optimization
This boils down to finding β=[β0β1] that minimizes ∥ε∥2=∥Y−Xβ∥2 through the method of least squares. Since β represents the intercept and slope of the line, finding β is about finding the line that explains the data in linear terms with the least error. Of course, since we can’t know exactly what relationship the two variables have, we need to find the estimates of regression coefficients, β0^ and β1^. In simple terms, draw the line that looks the most like the data.
These problems are typically solved using tools from numerical linear algebra, but simple regression analysis can also be solved with simple calculus. Rearranging the matrix and representing it again,
ε2=i=1∑n(yi−β0−β1xi)2
is about finding β0=β0^ and β1=β1^ that minimize it. Taking the partial derivative of equation (1) with respect to β0,
∂β0∂ε2=−2i=1∑n(yi−β0−β1xi)
For ε2 to be minimized,
nβ0=i=1∑nyi−β1i=1∑nxi
Thus, ε2 is minimized when it is β0=y−β1x. Taking the partial derivative of equation (1) with respect to β1,
∂β1∂ε2=−2i=1∑nxi(yi−β0−β1xi)
Since ε2 is minimized when it is β0=y−β1x,
i=1∑nxi(yi−y+β1x−β1xi)=0
In other words,
β1i=1∑n(xi2−xxi)=i=1∑nxiyi−i=1∑nxiy
Summarizing,
β1====∑i=1n(xi2−xxi)∑i=1nxiyi−∑i=1nxiy∑i=1n(xi2−x2)∑i=1n(xi−x)(yi−y)Var(X)Cov(X,Y)Cor(X,Y)sxsy
For the actual calculation, one should find β0^ before β1^.