OpenMS
Loading...
Searching...
No Matches
QuadraticRegression Class Reference

Estimates model parameters for a quadratic equation. More...

#include <OpenMS/ML/REGRESSION/QuadraticRegression.h>

Collaboration diagram for QuadraticRegression:
[legend]

Public Member Functions

 QuadraticRegression ()
 Default constructor.
 
void computeRegression (std::vector< double >::const_iterator x_begin, std::vector< double >::const_iterator x_end, std::vector< double >::const_iterator y_begin)
 Fit y = a + b*x + c*x^2 to unweighted 2D points.
 
void computeRegressionWeighted (std::vector< double >::const_iterator x_begin, std::vector< double >::const_iterator x_end, std::vector< double >::const_iterator y_begin, std::vector< double >::const_iterator w_begin)
 Fit y = a + b*x + c*x^2 to weighted 2D points.
 
double eval (double x) const
 Evaluate the fitted polynomial at x.
 
double getA () const
 Constant term of the fitted polynomial.
 
double getB () const
 Linear coefficient of the fitted polynomial.
 
double getC () const
 Quadratic coefficient of the fitted polynomial.
 
double getChiSquared () const
 Weighted residual sum of squares of the last successful fit.
 

Static Public Member Functions

static double eval (double A, double B, double C, double x)
 Evaluate a quadratic polynomial at x using externally-supplied coefficients.
 

Protected Attributes

double a_
 Constant term of the last successful fit.
 
double b_
 Linear coefficient of the last successful fit.
 
double c_
 Quadratic coefficient of the last successful fit.
 
double chi_squared_
 Weighted residual sum of the last successful fit.
 

Detailed Description

Estimates model parameters for a quadratic equation.

Fits the coefficients of y = a + b*x + c*x^2 via the normal equations of a (weighted) least-squares problem. Both an unweighted (computeRegression) and a weighted (computeRegressionWeighted) overload are provided.

Once a fit is computed the coefficients and the chi-squared sum can be queried via getA / getB / getC / getChiSquared, and the resulting polynomial can be evaluated via eval. A static overload of eval is available for evaluating an externally-supplied (A,B,C) triple.

Before the first successful fit the coefficients and the chi-squared sum read back as 0.

Constructor & Destructor Documentation

◆ QuadraticRegression()

Default constructor.

Leaves all coefficients and the chi-squared sum at 0; the instance produces meaningful results only after the first successful call to computeRegression or computeRegressionWeighted.

Member Function Documentation

◆ computeRegression()

void computeRegression ( std::vector< double >::const_iterator  x_begin,
std::vector< double >::const_iterator  x_end,
std::vector< double >::const_iterator  y_begin 
)

Fit y = a + b*x + c*x^2 to unweighted 2D points.

Equivalent to calling computeRegressionWeighted with a uniform weight of 1 for every point. On success the coefficients and getChiSquared are updated.

Parameters
[in]x_beginIterator to the first x value.
[in]x_endPast-the-end iterator for the x range.
[in]y_beginIterator to the first y value; the y range must contain at least std::distance(x_begin, x_end) elements.
Exceptions
Exception::UnableToFitif the resulting linear system is singular (e.g. fewer than three distinct x values).

◆ computeRegressionWeighted()

void computeRegressionWeighted ( std::vector< double >::const_iterator  x_begin,
std::vector< double >::const_iterator  x_end,
std::vector< double >::const_iterator  y_begin,
std::vector< double >::const_iterator  w_begin 
)

Fit y = a + b*x + c*x^2 to weighted 2D points.

Solves the weighted normal equations for the quadratic model. On success the coefficients and getChiSquared are updated.

Parameters
[in]x_beginIterator to the first x value.
[in]x_endPast-the-end iterator for the x range.
[in]y_beginIterator to the first y value; the y range must contain at least std::distance(x_begin, x_end) elements.
[in]w_beginIterator to the first weight; the weight range must contain at least std::distance(x_begin, x_end) elements.
Exceptions
Exception::UnableToFitif the resulting linear system is singular (e.g. fewer than three distinct x values).

◆ eval() [1/2]

static double eval ( double  A,
double  B,
double  C,
double  x 
)
static

Evaluate a quadratic polynomial at x using externally-supplied coefficients.

Parameters
[in]AConstant term.
[in]BLinear coefficient.
[in]CQuadratic coefficient.
[in]xInput value.
Returns
A + B*x + C*x*x.

◆ eval() [2/2]

double eval ( double  x) const

Evaluate the fitted polynomial at x.

Parameters
[in]xInput value.
Returns
a + b*x + c*x^2 using the currently stored coefficients.

◆ getA()

double getA ( ) const

Constant term of the fitted polynomial.

Returns
a from the last successful fit (or 0 if no fit has been computed yet).

◆ getB()

double getB ( ) const

Linear coefficient of the fitted polynomial.

Returns
b from the last successful fit (or 0 if no fit has been computed yet).

◆ getC()

double getC ( ) const

Quadratic coefficient of the fitted polynomial.

Returns
c from the last successful fit (or 0 if no fit has been computed yet).

◆ getChiSquared()

double getChiSquared ( ) const

Weighted residual sum of squares of the last successful fit.

Returns
sum(weight * (y - a - b*x - c*x*x)^2) over the last fit's input points (or 0 if no fit has been computed yet).

Member Data Documentation

◆ a_

double a_
protected

Constant term of the last successful fit.

◆ b_

double b_
protected

Linear coefficient of the last successful fit.

◆ c_

double c_
protected

Quadratic coefficient of the last successful fit.

◆ chi_squared_

double chi_squared_
protected

Weighted residual sum of the last successful fit.