Simple Linear Regression — Theory, Math, And Implementation In JavaScript

Oliver Jumpertz
8 min readJan 30, 2022

The simple linear regression is a predictive algorithm that provides a linear relationship between one input (x) and a predicted result (y).

We’re taking a look at how you can do it by hand and then implement a function in JavaScript that does exactly this for us.

The simple linear regression

Imagine a two-dimensional coordinate system with 2 points. You can connect both points with a straight line and also calculate the formula for that line. And that formula has the form of y = mx + b.

b is the intercept. It's the point where the straight line crosses the y-axis.

m is the slope of the line.

x is the input.

With only two points, calculating y = mx + b is straight-forward and doesn't take that much time. But now imagine that you have a few more points. Which points should the line actually connect? What would its slope and its intercept be?

Simple linear regression solves this problem by finding a line that goes through the cloud of points while minimizing the distance from each point to the line overall as far as possible.

Or in other words: Find the best possible solution while, most likely, never hitting the exact result. But that result is near enough so that we can work with it. You basically rotate the straight line until all points together have the minimum possible distance to the line.

Oliver Jumpertz

Software Engineer - Content Creator