tlx
Loading...
Searching...
No Matches
PolynomialRegression< Type, WithStore > Class Template Reference

Calculate the regression polynomial \( a_0+a_1x^1+a_2x^2+\cdots+a_nx^n \) from a list of 2D points. More...

#include <polynomial_regression.hpp>

Classes

struct  Coefficients
 polynomial stored as the coefficients of \( a_0+a_1 x^1+a_2 x^2+\cdots+a_n x^n \) More...
 
struct  Point
 2D point More...
 

Public Member Functions

 PolynomialRegression (size_t order)
 start new polynomial regression calculation
 
size_t size () const
 number of points
 
PolynomialRegressionadd (const Type &x, const Type &y)
 add point. this invalidates cached coefficients until next evaluate()
 
const Pointpoint (size_t i)
 return a point. Only available if WithStore is true.
 
Type r_square ()
 get r^2. Only available if WithStore is true.
 
Type evaluate (const Type &x)
 returns value of y predicted by the polynomial for a given value of x
 
const Coefficientscoefficients ()
 return coefficients vector
 

Protected Member Functions

void fit_coefficients ()
 polynomial regression by inverting a Vandermonde matrix.
 

Protected Attributes

size_t order_
 polynomial order
 
std::vector< Pointpoints_
 list of stored points if WithStore, else empty.
 
size_t size_
 number of points added
 
std::vector< Type > X_
 X_ = vector that stores values of sigma(x_i^2n)
 
std::vector< Type > Y_
 Y_ = vector to store values of sigma(x_i^order * y_i)
 
Coefficients coefficients_
 cached coefficients
 

Detailed Description

template<typename Type = double, bool WithStore = false>
class tlx::PolynomialRegression< Type, WithStore >

Calculate the regression polynomial \( a_0+a_1x^1+a_2x^2+\cdots+a_nx^n \) from a list of 2D points.

See also https://en.wikipedia.org/wiki/Polynomial_regression

If WithStore is false, then the sums are aggregated directly such that the class retains O(1) size independent of the number of points. if WithStore is true then the points are stored in a vector and can be retrieved.

Definition at line 33 of file polynomial_regression.hpp.

Constructor & Destructor Documentation

◆ PolynomialRegression()

template<typename Type = double, bool WithStore = false>
PolynomialRegression ( size_t order)
inline

start new polynomial regression calculation

Definition at line 37 of file polynomial_regression.hpp.

Member Function Documentation

◆ add()

template<typename Type = double, bool WithStore = false>
PolynomialRegression & add ( const Type & x,
const Type & y )
inline

add point. this invalidates cached coefficients until next evaluate()

Definition at line 52 of file polynomial_regression.hpp.

◆ coefficients()

template<typename Type = double, bool WithStore = false>
const Coefficients & coefficients ( )
inline

return coefficients vector

Definition at line 127 of file polynomial_regression.hpp.

◆ evaluate()

template<typename Type = double, bool WithStore = false>
Type evaluate ( const Type & x)
inline

returns value of y predicted by the polynomial for a given value of x

Definition at line 122 of file polynomial_regression.hpp.

◆ fit_coefficients()

template<typename Type = double, bool WithStore = false>
void fit_coefficients ( )
inlineprotected

polynomial regression by inverting a Vandermonde matrix.

Definition at line 154 of file polynomial_regression.hpp.

◆ point()

template<typename Type = double, bool WithStore = false>
const Point & point ( size_t i)
inline

return a point. Only available if WithStore is true.

Definition at line 78 of file polynomial_regression.hpp.

◆ r_square()

template<typename Type = double, bool WithStore = false>
Type r_square ( )
inline

get r^2. Only available if WithStore is true.

mean value of y

Definition at line 96 of file polynomial_regression.hpp.

◆ size()

template<typename Type = double, bool WithStore = false>
size_t size ( ) const
inline

number of points

Definition at line 42 of file polynomial_regression.hpp.

Member Data Documentation

◆ coefficients_

template<typename Type = double, bool WithStore = false>
Coefficients coefficients_
protected

cached coefficients

Definition at line 151 of file polynomial_regression.hpp.

◆ order_

template<typename Type = double, bool WithStore = false>
size_t order_
protected

polynomial order

Definition at line 136 of file polynomial_regression.hpp.

◆ points_

template<typename Type = double, bool WithStore = false>
std::vector<Point> points_
protected

list of stored points if WithStore, else empty.

Definition at line 139 of file polynomial_regression.hpp.

◆ size_

template<typename Type = double, bool WithStore = false>
size_t size_
protected

number of points added

Definition at line 142 of file polynomial_regression.hpp.

◆ X_

template<typename Type = double, bool WithStore = false>
std::vector<Type> X_
protected

X_ = vector that stores values of sigma(x_i^2n)

Definition at line 145 of file polynomial_regression.hpp.

◆ Y_

template<typename Type = double, bool WithStore = false>
std::vector<Type> Y_
protected

Y_ = vector to store values of sigma(x_i^order * y_i)

Definition at line 148 of file polynomial_regression.hpp.


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