Simple Linear Regression — Theory, Math, And Implementation In JavaScript
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…