Statistical Modeling: Simple Linear Regression
What the model assumes, what it minimises, and how the best line is found.
The short version
- A statistical model compresses many observations into a few parameters, and lets you infer values you haven’t observed.
- Simple linear regression predicts y from x as an intercept plus a weight times x, plus noise with mean zero and constant variance. More inputs make it a dot product of weights and features.
- A loss function measures the fit, here the average squared error, and fitting means finding the weights that minimise it.
- Linear regression has a closed-form solution, the normal equation. Gradient descent is the general method when no formula exists.
- The last section fits scikit-learn’s implementation to the King County house sales data.
Statistical Modeling
Statistical modeling is the practice of summarizing observation and create inferences from it. observation represented in data points includes lots of details. for example, if we observe house sales information in a certain location, we could collect information on the size of the house, price, location, etc.. for now let’s assume that we are concerned only with price and square footage of the house. we observed 100 house sales. there is going to be a lot of information in this data set that we don’t necessarily want to keep track of. imagine now we have 1000 observations (data points). Statistical modeling enables us to create summaries from data, to describe the general trend or overall shape of the data without keeping all track of all observation and data points. Statisticians sometimes refer to this idea by data compress.
%matplotlib inlineimport matplotlib as mplimport matplotlib.pyplot as pltmpl.rc('axes', labelsize=14)mpl.rc('xtick', labelsize=12)mpl.rc('ytick', labelsize=12)
import numpy as npnp.random.seed(56)X = 2 * np.random.rand(100,1)y = 8 + 5 * X + np.random.randn(100 ,1)
plt.plot(X,y, "b.")plt.xlabel("X", fontsize=12)plt.ylabel("y", rotation=0,fontsize=12)plt.show()
The first task of statistical modeling is to summarize or compress this information through a representative model describing correlations in the data. but it does not stop there. Modeling enables us to enter the realm of inference - if we are trying to extend our knowledge from the data we have already, to other variables we have not to measure or didn’t measure directly - essentially extrapolating from sample data to a larger population is an inference based on modeling.
But more importantly, observations are prone to measurement errors, whether this has to do with mistakes, deliberate lies, possibly incorrect assumptions made at various stages, accidents,etc.. some of these sources for error are systematic where it will distort data in predictable, repeatable ways. but some other sources of errors are statistical it is directionless, do not repeat itself yet the distribution of errors in data is still predictable and stable (probability theory helps us to handle such errors).
Simple Linear Regression Model
The most basic of all statistical models and a foundation of most predictions in the world of machine learning is Simple Linear Regression. The model of two random variables, , and , where we are trying to predict from . and like any good statistical model, it has a few key assumptions:
- distribution is unspecified, possibly even deterministic.
- , where is a noise variable.
- has mean 0, a constant variance , and is uncorrelated with and uncorrelated across observations.
the model assumes a noise normally distributed Gaussian to represent measurement error or fluctuations in , or both. The coefficient represents and intercept or initial state. where the coefficient represent weights added to variable to predict .
We could plot a simple regression model on the above dataset to compress and summarize the description of the data.
X_b = np.c_[np.ones((100, 1)), X]beta_1 = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)X_new_b = np.c_[np.ones((100,1)), X]y_predict = X_new_b.dot(beta_1)
# Plot model predictionsplt.plot(X, y_predict, "r-")plt.plot(X, y, "b.")plt.show()
As you can see from the plot, the regression model line compresses the 100 data points into a simpler linear upward trend that is easy for humans to remember and reason about. even better it helps us predict other data points that obey the same assumptions of the simple linear regression model.
Linear Regression probably the most basic simplest and popular tool in regression modeling. dating back to the late 19th century. its basic assumption a linear relationship between input random variable and target random variable . This linear relationship can is represented by the algebraic formula . In simple terms, can be expressed as a weighted sum of the inputs , with additive noise to the observations. and as we described earlier the assumption is this noise is well behaved following a Gaussian distribution. In our introductory example, we can describe the price of a house given an input of the area of the house using a linear model as .
Where:
- : is bias (sometimes called intercept or offest).
- : are weights determine the influence of the area on the target (house price)
We can generalize this model for more than one input or feature to describe a linear relationship between inputs to a target variable. for example, we can describe the relationship between house prices with respect to house area and the number of rooms.
The goal in designing a linear model in the machine learning world is to build a model from a training dataset to help us predict future target values. for example, predicting house prices given area and number of rooms. So if we have an input observations dataset the goals become to choose weights and a bias such that on average, the predictions made with this model are as close as possible (fit) the true house prices we observed in the data.
The predicted values is referred to in mathematical notation using the hat symbol
In high dimentionality suitations we collect all input features in a vector and all weights into a vactor . then the model can be expressed concisely using dot product.
but before we start finding the best parameters and for our model we will need first to have a quality measure for the given model. that is how do we know that our model was able to predict correctly. and once we can measure model performance we will need a procedure to improve the model’s prediction quality.
Measure of Quality
Measuring the quality of linear model prediction is key to improve model prediction quality. that is we need a measure of fitness before we start fitting the model to data. Loss Function quantify the distance between the real and predicted value of the target variable. The loss will usually be a non-negative number where smaller values are better and perfect predictions incur a loss of .
Sum of Squared Errors
This is the most popular loss function in regression problems. for a data point , and real target and predict value . the squared erros is given by:
To measure the quality of a model on the entire dataset, we simply average (or equivalently, sum) the losses on the training set.
When training the model, we want to find parameters () that minimize the total loss across all training samples:
Linear Regression happens to be one of the models that have an analytical solution to the optimization problem. other more complicated models don’t have a solution that can be solved analytically by a simple formula yielding the global optimum; in which case a numerical solution can be used to optimize the model like Gradient Decent algorithm. I’m not going in the mathematical details for how to optimize Linear Regression analytically, we can subsume the bias into the weight parameter and the final formula for computing optimum w would be:
you can easily implement the above formula using NumPy to compute the weights for linear regression. but don’t get used to this easy life, mathematical analysis is not always easy to compute.
w = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)print(w)Gradient Descent to the Rescue!
For high-dimensionality and nonconvex loss surfaces (surfaces with more than single minima), the Gradient Descent algorithm is an effective model in practice to optimize the model. the key idea behind the algorithm is to iteratively reduce the error by updating parameters in the direction that incrementally lowers the loss function. On convex loss surface (Single Global Minima) it will converge to the minima; the same can’t be said for nonconvex surfaces but at least it will lead towards a local minimum.
Real World Example
Let’s try Linear Regression out on a real dataset. Below I’m going to load in the King County house sales dataset of Seattle housing prices. It has the following features:
id: a notation for a housedate: Date house was soldprice: Price is the prediction targetbedrooms: Number of Bedrooms/Housebathrooms: Number of bathrooms/Housesqft_living: square footage of the homesqft_lot: square footage of the lotfloors: Total floors (levels) in housewaterfront: House which has a view to a waterfrontview: Has been viewedcondition: How good the condition is ( Overall )grade: overall grade given to the housing unit, based on King County grading systemsqft_above: square footage of house apart from the basementsqft_basement: square footage of the basementyr_built: Built Yearyr_renovated: Year when the house was renovatedzipcode: ziplat: Latitude coordinatelong: Longitude coordinatesqft_living15: Living room area in 2015 (implies some renovations). This might or might not have affected the lot size areasqft_lot15: lotSize area in 2015 (implies some renovations)
We minimally process the data to make it amenable to analysis as follows:
- fill in any missing data with the average value for that column
- delete the date and the value
sqft_basement
We don’t have to reinvent the wheel, so we will use the most common implementation of linear regression, found in sklearn library. sklearn is the standard package used for the majority of (non-neural network based) machine learning techniques. It contains nice implementations with a common interface for the majority of commonly encountered techniques.
from sklearn.linear_model import LinearRegression
# kc_house_data# https://www.kaggle.com/shivachandel/kc-house-data# House sales in King County, Washington State
data = pd.read_csv('kc_house_data.csv')data = data.drop(['date','sqft_basement'],axis = 1)data = data.fillna(data.mean())
model = LinearRegression()model.fit(data.drop('price',axis=1),data['price'])print("R squared of full model is: {}".format(model.score(data.drop('price',axis=1),data['price'])))Read More:
- Ruelle, David (1991). Chance and Chaos. Princeton, New Jersey: Princeton University Press.
- Smith, Leonard (2007). Chaos: Avery Short Introduction. Oxford: Oxford University Press.
Related posts

Code review economics
On three busy repositories the wait for a first review grows much more slowly than a PR's size, so a change split into eight PRs reviewed one after another waits three to eight times as long. Opening them side by side helps less than it looks, and only one reviewer taking them together is no worse than one PR. The measurement, the rule that falls out of it, and a tool to run it on your own PRs.
The unwritten rulescraftteamsstatistics
Every latency number answers one question
A checkout page hangs while the dashboard says 23 ms. The mean, a percentile and a share under a threshold each answer a different question, only two of them add up across hosts, and an averaged p99 can be wrong by a factor of three in either direction.
The unwritten rulesobservabilityperformancestatistics
What an agent should remember
Agent memory stores mostly decide what to forget by time: a TTL, or deleting what hasn't been used. In a 90-day scripted scenario, that kept a cancelled booking alive for weeks and stored a restaurant's sentence as the user's preference. Two fields fix both: who wrote the memory, and which event would make it false.
AI & agentic architecturesai-agentsarchitecture
Software craftCode review economics
On three busy repositories the wait for a first review grows much more slowly than a PR's size, so a change split into eight PRs reviewed one after another waits three to eight times as long. Opening them side by side helps less than it looks, and only one reviewer taking them together is no worse than one PR. The measurement, the rule that falls out of it, and a tool to run it on your own PRs.
craftteamsOct 2026
SystemsEvery latency number answers one question
A checkout page hangs while the dashboard says 23 ms. The mean, a percentile and a share under a threshold each answer a different question, only two of them add up across hosts, and an averaged p99 can be wrong by a factor of three in either direction.
observabilityperformanceAug 2026
AIWhat an agent should remember
Agent memory stores mostly decide what to forget by time: a TTL, or deleting what hasn't been used. In a 90-day scripted scenario, that kept a cancelled booking alive for weeks and stored a restaurant's sentence as the user's preference. Two fields fix both: who wrote the memory, and which event would make it false.
ai-agentsarchitectureOct 2026
Comments