template<class _elementType>
class nmrLinearRegressionSolver< _elementType >
This class provides a linear regression solver using a least-squares solution. It is primarily designed for ongoing use, where samples arrive at discrete points in time. The basic approach is as follows:
double x,y;
double slope, yint, mse;
while (!done) {
}
nmrLinearRegressionSolver(_elementType tol=cmnTypeTraits< _elementType >::DefaultTolerance)
Definition nmrLinearRegression.h:119
virtual bool Estimate(_elementType &slope, _elementType &yint, _elementType *mse=0)
Definition nmrLinearRegression.h:241
virtual bool Sample(const _elementType &x, const _elementType &y)
Definition nmrLinearRegression.h:218
In addition to the Estimate method, there is also an EstimateAsFractions method that returns slope_num, yint_num, denom, and tse_num (note that the types may be different from the input data types due to type promotion). In other words:
\( y = (slope_num/denom)*x + (yint_num/denom) \)
which can be rewritten as:
\( denom*y = slope_num*x + yint_num \)
tse_num is the numerator for the total square error. To get mean square error:
\( mse = tse_num/(N*denom) \)
where N is the number of points.
There are two good reasons for this method:
-
The results are always defined (i.e., there is no possible division by 0), so the caller can use whatever tolerance is desired. For near vertical lines, it is possible to instead compute the parameters of the line \( x = (denom/slope_num)*y - (yint_num/slope_num) \).
-
When this templated class is instantiated for fixed point numbers, such as
int, the estimated slope and yint are also fixed point numbers. But, by returning them as fractions, the caller can decide whether to compute the final results as floating point numbers, or whether to perform both fixed point multiplication and division.
The Sample method is overloaded so that it can also accept input vectors, either a fixed size vector of size 2 that contains both x and y, or dynamic vectors of multiple x and y values.
All methods return a boolean flag to indicate success or failure. Possible reasons for failure include inconsistent vector sizes and near infinite slopes. Note that the check for near infinite slopes is based on a specified tolerance value. The default tolerances are obtained from cmnTypeTraits. These defaults may be too large for some applications (e.g., the default for float is currently 1e-5 and for double it is 1e-9). It is possible to specify a new tolerance value in the class constructor, or via the SetTolerance method.
This class has virtual methods to enable derivation. One possible derivation would be to create a class that checks for impending overflow on the accumulators (Sx, Sy, Sxx, Sxy, Syy) and returns false in that case. This can be done using cmnTypeTraits<_elementType>::MaxPositiveValue and MinNegativeValue. For example, statements such as the following can be added to Sample:
static CISST_EXPORT Type MaxPositiveValue(void)
static CISST_EXPORT Type MinNegativeValue(void)
SummationType Sx
Definition nmrLinearRegression.h:181
SummationType Sxx
Definition nmrLinearRegression.h:182