cisst-saw
Loading...
Searching...
No Matches
nmrLinearRegressionSolver< _elementType > Class Template Reference

#include <nmrLinearRegression.h>

Inheritance diagram for nmrLinearRegressionSolver< _elementType >:
nmrLinearRegressionWindowSolver< _elementType > nmrLinearRegressionWindowRecursiveSolver< _elementType >

Public Types

typedef _elementType ElementType
typedef cmnTypeTraits< _elementType >::VaArgPromotion SummationType

Public Member Functions

 nmrLinearRegressionSolver (_elementType tol=cmnTypeTraits< _elementType >::DefaultTolerance)
virtual ~nmrLinearRegressionSolver ()
size_t NumPoints () const
_elementType GetTolerance () const
void SetTolerance (_elementType tol)
virtual void Clear ()
virtual bool Sample (const _elementType &x, const _elementType &y)
virtual bool Sample (const vctFixedSizeVector< _elementType, 2 > &in)
virtual bool Sample (const vctFixedSizeConstVectorRef< _elementType, 2, 1 > &in)
virtual bool Sample (const vctDynamicVector< _elementType > &x, const vctDynamicVector< _elementType > &y)
virtual bool Sample (const vctDynamicConstVectorRef< _elementType > &x, const vctDynamicConstVectorRef< _elementType > &y)
virtual bool Sample (const std::vector< _elementType > &x, const std::vector< _elementType > &y)
virtual bool EstimateAsFractions (SummationType &slope_num, SummationType &yint_num, SummationType &denom, SummationType *tse_num=0)
virtual bool Estimate (_elementType &slope, _elementType &yint, _elementType *mse=0)

Protected Member Functions

virtual bool ComputeSums (const _elementType &x, const _elementType &y)
template<class _vectorOwnerType>
bool SampleVector (const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &x, const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &y)

Protected Attributes

size_t numpts
SummationType Sx
SummationType Sy
SummationType Sxx
SummationType Sxy
SummationType Syy
SummationType tolerance

Detailed Description

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) {
// Get and sample new data (x,y)
lr.Sample(x,y);
}
lr.Estimate(slope, yint, &mse);
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:

  1. 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) \).
  2. 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

Member Typedef Documentation

◆ ElementType

template<class _elementType>
typedef _elementType nmrLinearRegressionSolver< _elementType >::ElementType

◆ SummationType

template<class _elementType>
typedef cmnTypeTraits<_elementType>::VaArgPromotion nmrLinearRegressionSolver< _elementType >::SummationType

Constructor & Destructor Documentation

◆ nmrLinearRegressionSolver()

template<class _elementType>
nmrLinearRegressionSolver< _elementType >::nmrLinearRegressionSolver ( _elementType tol = cmnTypeTraits<_elementType>::DefaultTolerance)
inline

Constructor

Parameters
tolTolerance to use when checking for infinite slope (default from cmnTypeTraits)

◆ ~nmrLinearRegressionSolver()

template<class _elementType>
virtual nmrLinearRegressionSolver< _elementType >::~nmrLinearRegressionSolver ( )
inlinevirtual

Member Function Documentation

◆ Clear()

template<class _elementType>
virtual void nmrLinearRegressionSolver< _elementType >::Clear ( )
inlinevirtual

Initialize the accumulators (i.e., clear all sample points)

◆ ComputeSums()

template<class _elementType>
bool nmrLinearRegressionSolver< _elementType >::ComputeSums ( const _elementType & x,
const _elementType & y )
protectedvirtual

Computes the summations used for the linear regression.

◆ Estimate()

template<class _elementType>
bool nmrLinearRegressionSolver< _elementType >::Estimate ( _elementType & slope,
_elementType & yint,
_elementType * mse = 0 )
virtual

Estimate the slope and y intercept and (optionally) return the mean square error (MSE). RMS error is the square root of MSE.

◆ EstimateAsFractions()

template<class _elementType>
bool nmrLinearRegressionSolver< _elementType >::EstimateAsFractions ( SummationType & slope_num,
SummationType & yint_num,
SummationType & denom,
SummationType * tse_num = 0 )
virtual

Estimate the slope and y intercept as fractions: slope = slope_num/denom yint = yint_num/denom tse = tse_num/denom (total square error, divide by numpts to get mse)

Reimplemented in nmrLinearRegressionWindowSolver< _elementType >.

◆ GetTolerance()

template<class _elementType>
_elementType nmrLinearRegressionSolver< _elementType >::GetTolerance ( ) const
inline

Returns the tolerance used when checking for infinite slope.

◆ NumPoints()

template<class _elementType>
size_t nmrLinearRegressionSolver< _elementType >::NumPoints ( ) const
inline

Returns number of sampled points

◆ Sample() [1/6]

template<class _elementType>
bool nmrLinearRegressionSolver< _elementType >::Sample ( const _elementType & x,
const _elementType & y )
virtual

Sample the specified x,y pair. Always returns true, but derived classes could return false (e.g., if checking for overflow)

Reimplemented in nmrLinearRegressionWindowRecursiveSolver< _elementType >, and nmrLinearRegressionWindowSolver< _elementType >.

◆ Sample() [2/6]

template<class _elementType>
virtual bool nmrLinearRegressionSolver< _elementType >::Sample ( const std::vector< _elementType > & x,
const std::vector< _elementType > & y )
inlinevirtual

Sample multiple x and y values, provided as two std::vector objects. Returns true unless vector sizes are not equal.

◆ Sample() [3/6]

template<class _elementType>
virtual bool nmrLinearRegressionSolver< _elementType >::Sample ( const vctDynamicConstVectorRef< _elementType > & x,
const vctDynamicConstVectorRef< _elementType > & y )
inlinevirtual

◆ Sample() [4/6]

template<class _elementType>
virtual bool nmrLinearRegressionSolver< _elementType >::Sample ( const vctDynamicVector< _elementType > & x,
const vctDynamicVector< _elementType > & y )
inlinevirtual

Sample multiple x and y values, provided as two vctDynamicVectors or two vctDynamicVectorRefs. Returns true unless vector sizes are not equal.

◆ Sample() [5/6]

template<class _elementType>
virtual bool nmrLinearRegressionSolver< _elementType >::Sample ( const vctFixedSizeConstVectorRef< _elementType, 2, 1 > & in)
inlinevirtual

Sample the specified x,y pair, provided as a vector ref.

◆ Sample() [6/6]

template<class _elementType>
virtual bool nmrLinearRegressionSolver< _elementType >::Sample ( const vctFixedSizeVector< _elementType, 2 > & in)
inlinevirtual

Sample the specified x,y pair, provided as a vector.

◆ SampleVector()

template<class _elementType>
template<class _vectorOwnerType>
bool nmrLinearRegressionSolver< _elementType >::SampleVector ( const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > & x,
const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > & y )
inlineprotected

Common code to sample multiple x,y pairs, provided as two dynamic vectors. Returns true unless vector sizes are not equal.

◆ SetTolerance()

template<class _elementType>
void nmrLinearRegressionSolver< _elementType >::SetTolerance ( _elementType tol)
inline

Set the tolerance used when checking for infinite slope.

Member Data Documentation

◆ numpts

template<class _elementType>
size_t nmrLinearRegressionSolver< _elementType >::numpts
protected

◆ Sx

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::Sx
protected

◆ Sxx

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::Sxx
protected

◆ Sxy

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::Sxy
protected

◆ Sy

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::Sy
protected

◆ Syy

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::Syy
protected

◆ tolerance

template<class _elementType>
SummationType nmrLinearRegressionSolver< _elementType >::tolerance
protected

The documentation for this class was generated from the following file: