Linear regression
AvailableFit a line to a set of points and watch how the mean squared error changes.
Description
Qué haceLearns a line ŷ = m·x + b: given a value of x, it uses the slope m and intercept b to predict a numeric value ŷ.
Para qué sirveEstimate a numeric quantity (price, temperature, demand) from a single variable, when the relationship between the two is roughly a straight line.
What the best line means
For each point, the line produces a prediction. The algorithm picks the slope and intercept that make the errors of all those predictions small.
Prediction
ŷᵢ = m·xᵢ + b
m tells you how much the prediction changes when x increases by one unit; b is the prediction when x = 0.
Error or residual
eᵢ = ŷᵢ − yᵢ
It's the vertical distance between the prediction and the actual value. Regression minimizes the average of their squares so positive and negative errors don't cancel out.
Example with units:To predict the price of a taxi ride, x could be the distance in kilometers and y the actual price in dollars. If the learned line is ŷ = 100x + 150, the slope is $100/km and the intercept is a starting cost of $150. For a 4 km ride, it predicts $550; if the actual price was $520, the residual is 550 − 520 = $30.
How m and b are learned
In simple linear regression with squared error, both methods search for exactly the same minimum. The difference is how they get there.
Closed-form solution
m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)²b = ȳ − m·x̄
Computes the result directly. The numerator measures whether x and y vary together (covariance); the denominator measures how much x varies. It’s the natural choice for this small, single-variable problem.
Gradient descent
m ← m − α·(2/n)Σ eᵢxᵢb ← b − α·(2/n)Σ eᵢ
Starts with m = 0 and b = 0. Each iteration computes which direction increases the MSE and moves both parameters the opposite way. α is the learning rate: it controls the size of each step. It’s used when a direct solution doesn’t exist or is too costly, and here it lets you watch the learning happen step by step.
When it stops:The demo stops when the MSE improvement between two steps is smaller than 0.0001 or after 100 iterations. A learning rate that’s too large can jump over the minimum and make the error grow.
Edge case:If every point has the same x, the denominator of the m formula is zero and there’s no single slope. The demo flags this and shows the horizontal line ŷ = ȳ as a reference.
How the result is measured
The two metrics describe the fit from different angles: error in y's units, and improvement over a basic prediction.
MSE: error size
MSE = (1/n) Σ(yᵢ − ŷᵢ)²
It's the average of the squared residuals, and it's the quantity the algorithm minimizes. Zero means every point falls on the line; the lower, the better.
R²: improvement over the mean
R² = 1 − Σ(yᵢ−ŷᵢ)² / Σ(yᵢ−ȳ)²
The numerator is the line’s error (SSres) and the denominator is the error you’d get by always predicting the mean ȳ (SStot). A value of 1 is perfect, 0 doesn’t improve on that mean, and a negative value is worse than using it.
In this demo:MSE and R² are computed over every visible point. They don't yet measure how the line would perform on new data; that requires splitting training and test data.
Playground
Training
- Dataset point
- Outlier
- Gradient descent (solid line)
- Optimal / least squares (dashed)
Current model metrics (gradient descent)
- Slope
- 0.00
- Intercept
- 0.00
- MSE
- 65.09
- R²
- -1.14
- Iteration
- 0 / 100
- Δ slope vs. optimal
- 1.87
- Δ intercept vs. optimal
- 2.14
Absolute difference from the closed-form (least-squares) solution.
Dataset generation
Changing these controls generates a new synthetic dataset and resets gradient descent.
Seed for the deterministic generator.
Standard deviation of the Gaussian noise added to y.
Edit points
| x | y | Actions |
|---|---|---|
Shortcut: Ctrl/Cmd+Z undoes the last confirmed edit (if focus isn't in a text field).
Loss
Gradient descent
Too high can diverge (the loss grows); too low converges slowly.
Status: Ready. The model starts with slope and intercept equal to zero.
Training controls are available. Shortcut: spacebar starts or pauses (if focus isn't in a text field or control).