Stats with Python: Simple Linear Regression
This post summarizes the basics of simple linear regression --method of least squares and coefficient of determination.
2021-03-223 min readEN
Table of contents
We’ve seen several aspects of the correlation coefficient in the previous posts. The correlation coefficient treats two variables equally; they are symmetrical. When two variables are not symmetrical, that is, when you want to explain y by x, correlation analysis alone is not sufficient. Instead, you might want to conduct a regression analysis.
The simplest approach, simple linear regression, considers a single explanatory variable (independent variable) x for explaining the objective variable (dependent variable) y.
y=β0+β1x
Least Square Estimates
How to determine the parameters β0 and β1 in the above equation? Given the paired data {(xi,yi)}i=1n, they are determined by the method of least squares. That is, they are chosen to minimize the sum of the squared error between the predicted y^i=b0+b1xi and the actual yi:
L(b0,b1):=i=1∑n{yi−(b0+b1xi)}2.
Therefore, the least squares estimates are:
β^1=Sx2Sxy,β^0=yˉ−β1xˉ.
, where
Sxy:=n1i=1∑n(xi−x)(yi−y),Sx2:=n1i=1∑n(xi−x)2,Sy2:=n1i=1∑n(yi−y)2.
Proof
L(b0,b1)=i=1∑n{(yi−yˉ)+(yˉ−b0−b1xˉ)−b1(xi−xˉ)}2
Considering ∑i=1n(xi−xˉ)=0 and ∑i=1n(yi−yˉ)=0,
L(b0,b1)=i=1∑n(yi−yˉ)2+n(yˉ−b0−b1xˉ)2+b12i=1∑n(xi−xˉ)2−2b1i=1∑n(xi−xˉ)(yi−yˉ)=nSy2+n(yˉ−b0−b1xˉ)2+nb12Sx2−2nb1Sxy=nSx2(b1−Sx2Sxy)2+n(yˉ−b0−b1xˉ)2+n(Sy2−Sx2Sxy2)
From the above calculation, L(b0,b1) takes its minimum value when b1=Sxy/Sx2 and b0=yˉ−b1xˉ.
Coefficient of Determination
Now we have the predicted values {y^i}i=1n. How good are these predictions? To evaluate the goodness, coefficient of determination R2 is frequently used.
R2:=TSSESS=∑i=1n(yi−yˉ)2∑i=1n(yi^−yˉ)2.
The coefficient of determination is the ratio of ESS (explained sum of squares) to TSS (total sum of squares). As you may imagine from its notation, the coefficient of determination R2 is the square of the Pearson correlation coefficient r.
Proof
Using the equation: y^i−yˉ=β1(xi−xˉ),
R2=∑i=1n(yi−yˉ)2∑i=1n(β1(xi−xˉ))2=nSy2nβ12Sx2=Sx4Sxy2Sy2Sx2=(SxSySxy)2=r2.
Experiment
Lastly, let’s confirm that R2=r2, introducing how to use linear regression with Python. As done in the previous post, I generated 100 pairs of correlated random samples (x and y).
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("darkgrid")
n = 100
x = np.random.rand(n)
y = x + 0.5*np.random.rand(n)

Scikit-learn implements linear regression as LinearRegression and coefficient of determination as r2_score. After fitting the model, we can plot the regression line like below:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
model = LinearRegression()
model.fit(x.reshape(-1, 1), y.reshape(-1, 1))
sns.scatterplot(x, y)
plt.plot(x, model.predict(x.reshape(-1, 1)), color="k")

As expected, R2=r2 is confirmed.
r2_score(y, model.predict(x.reshape(-1, 1)))
# >> 0.7922606713476185
np.corrcoef(x, y)[0,1]**2
# >> 0.7922606713476184
References
[1] 倉田 博史, 星野 崇宏. “入門統計解析”(第3章). 新世社. 2009.
Shion Honda
Software engineer based in Rome since February 2026. I work on bringing AI agents into products, and I write about evaluation, context engineering, and the practical edges of LLM systems.