Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlsta_linefit
[Package bdlsta]

Online algorithm for computing the least squares regression line. More...

Namespaces

namespace  bdlsta

Detailed Description

Outline
Purpose:
Online algorithm for computing the least squares regression line.
Classes:
bdlsta::LineFit online calculation of least squares regression line
Description:
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.
Usage:
This section illustrates intended use of this component.
Example 1: Calculating line fit, variance, and mean:
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:
  double inputX[] = { 1.0, 2.0, 4.0, 5.0 };
  double inputY[] = { 1.0, 2.0, 4.0, 4.5 };
  bdlsta::LineFit lineFit;
Then, we invoke the add routine to accumulate the data:
  for(int i = 0; i < 4; ++i) {
      lineFit.add(inputX[i], inputY[i]);
  }
Finally, we assert that the alpha, beta, variance, and mean are what we expect:
  double alpha, beta;
  ASSERT(4    == lineFit.count());
  ASSERT(3.0  == lineFit.xMean());
  ASSERT(1e-3 >  fabs(2.875    - lineFit.yMean()));
  ASSERT(1e-3 >  fabs(3.33333  - lineFit.variance()));
  ASSERT(0    == lineFit.fitIfValid(&alpha, &beta));
  ASSERT(1e-3 >  fabs(0.175 - alpha));
  ASSERT(1e-3 >  fabs(0.9   - beta ));