Previous Up Next

9.2.4  Linear regression: linear_regression linear_regression_plot

Given a set of points (x0,y0),…,(xn−1,yn−1), linear regression finds the line y=mx+b that comes closest to passing through all of the points; i.e., that makes

(y0 − (m x0 + b))2 + … + (yn−1 − (m xn−1 + b))2

as small as possible. The linear_regression command finds the linear regression of a set of points.


Example.
Input:

linear_regression([[0,0],[1,1],[2,4],[3,9],[4,16]])

or:

linear_regression([0,1,2,3,4],[0,1,4,9,16])

Output:

4,−2

which means that the line y = 4x − 2 is the best fit line.


The linear_regression_plot command draws the best fit line.


Example.
Input:

linear_regression_plot([0,1,2,3,4],[0,1,4,9,16])

Output:


Previous Up Next