BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlsta_linefit

Detailed Description

Outline

Purpose

Online algorithm for computing the least squares regression line.

Classes

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 };
Definition bdlsta_linefit.h:122

Then, we invoke the add routine to accumulate the data:

for(int i = 0; i < 4; ++i) {
lineFit.add(inputX[i], inputY[i]);
}
void add(double xValue, double yValue)
Add the specified (xValue, yValue) point to the data set.
Definition bdlsta_linefit.h:215

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 ));
double xMean() const
Definition bdlsta_linefit.h:277
double yMean() const
Definition bdlsta_linefit.h:295
double variance() const
Definition bdlsta_linefit.h:259
int count() const
Returns the number of elements in the data set.
Definition bdlsta_linefit.h:229
int fitIfValid(double *alpha, double *beta) const
Definition bdlsta_linefit.h:246