tlx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 More...
 
size_t size () const
 number of points More...
 
PolynomialRegressionadd (const Type &x, const Type &y)
 add point. this invalidates cached coefficients until next evaluate() More...
 
const Pointpoint (size_t i)
 return a point. Only available if WithStore is true. More...
 
Type r_square ()
 get r^2. Only available if WithStore is true. More...
 
Type evaluate (const Type &x)
 returns value of y predicted by the polynomial for a given value of x More...
 
const Coefficientscoefficients ()
 return coefficients vector More...
 

Protected Member Functions

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

Protected Attributes

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

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 ( size_t  order)
inline

start new polynomial regression calculation

Definition at line 37 of file polynomial_regression.hpp.

Member Function Documentation

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.

const Coefficients& coefficients ( )
inline

return coefficients vector

Definition at line 127 of file polynomial_regression.hpp.

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.

void fit_coefficients ( )
inlineprotected

polynomial regression by inverting a Vandermonde matrix.

Definition at line 154 of file polynomial_regression.hpp.

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.

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_t size ( ) const
inline

number of points

Definition at line 42 of file polynomial_regression.hpp.

Member Data Documentation

Coefficients coefficients_
protected

cached coefficients

Definition at line 151 of file polynomial_regression.hpp.

size_t order_
protected

polynomial order

Definition at line 136 of file polynomial_regression.hpp.

std::vector<Point> points_
protected

list of stored points if WithStore, else empty.

Definition at line 139 of file polynomial_regression.hpp.

size_t size_
protected

number of points added

Definition at line 142 of file polynomial_regression.hpp.

std::vector<Type> X_
protected

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

Definition at line 145 of file polynomial_regression.hpp.

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: