BDE 4.14.0 Production release
|
Online algorithm for computing the least squares regression line.
This component provides a mechanism, bdlsta::LineFit
, that provides online calculation of the least squares line fit. Online algorithms process the data in one pass, while maintaining accuracy. The online algorithm used is developed in the implementation notes (it is similar to the Welford online algorithm for computing variance). The formulae for line fit are taken from: https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line
Note that the behavior is undefined if there are less than 2 data points, or if all the X's (dependent variable) are the same.
This section illustrates intended use of this component.
This example shows how to accumulate a set of values, and calculate the line fit parameters, variance, and mean.
First, we create example input and instantiate the appropriate mechanism:
Then, we invoke the add
routine to accumulate the data:
Finally, we assert that the alpha, beta, variance, and mean are what we expect: