Package com.flarerobotics.lib.math
Class PolynomialRegression
java.lang.Object
com.flarerobotics.lib.math.PolynomialRegression
- All Implemented Interfaces:
Comparable<PolynomialRegression>
The
PolynomialRegression
class performs a polynomial regression on an set of
N data points (yi, xi). That is, it fits a
polynomial y = β0 + β1 x +
β2 x2 + ... + βd
xd (where y is the response variable, x is
the predictor variable, and the βi are the regression coefficients)
that minimizes the sum of squared residuals of the multiple regression model. It also
computes associated the coefficient of determination R2.
This implementation performs a QR-decomposition of the underlying Vandermonde matrix, so it is neither the fastest nor the most numerically stable way to perform the polynomial regression.
-
Constructor Summary
ConstructorsConstructorDescriptionPolynomialRegression
(double[] x, double[] y, int degree) Performs a polynomial reggression on the data points(y[i], x[i])
.PolynomialRegression
(double[] x, double[] y, int degree, String variableName) Performs a polynomial reggression on the data points(y[i], x[i])
. -
Method Summary
Modifier and TypeMethodDescriptiondouble
beta
(int j) Returns thej
th regression coefficient.int
Compare lexicographically.int
degree()
Returns the degree of the polynomial to fit.double
predict
(double x) Returns the expected responsey
given the value of the predictor variablex
.double
R2()
Returns the coefficient of determination R2.toString()
Returns a string representation of the polynomial regression model.
-
Constructor Details
-
PolynomialRegression
public PolynomialRegression(double[] x, double[] y, int degree) Performs a polynomial reggression on the data points(y[i], x[i])
. Uses n as the name of the predictor variable.- Parameters:
x
- the values of the predictor variabley
- the corresponding values of the response variabledegree
- the degree of the polynomial to fit- Throws:
IllegalArgumentException
- if the lengths of the two arrays are not equal
-
PolynomialRegression
Performs a polynomial reggression on the data points(y[i], x[i])
.- Parameters:
x
- the values of the predictor variabley
- the corresponding values of the response variabledegree
- the degree of the polynomial to fitvariableName
- the name of the predictor variable- Throws:
IllegalArgumentException
- if the lengths of the two arrays are not equal
-
-
Method Details
-
beta
public double beta(int j) Returns thej
th regression coefficient.- Parameters:
j
- the index- Returns:
- the
j
th regression coefficient
-
degree
public int degree()Returns the degree of the polynomial to fit.- Returns:
- the degree of the polynomial to fit
-
R2
public double R2()Returns the coefficient of determination R2.- Returns:
- the coefficient of determination R2, which is a real number between 0 and 1
-
predict
public double predict(double x) Returns the expected responsey
given the value of the predictor variablex
.- Parameters:
x
- the value of the predictor variable- Returns:
- the expected response
y
given the value of the predictor variablex
-
toString
Returns a string representation of the polynomial regression model. -
compareTo
Compare lexicographically.- Specified by:
compareTo
in interfaceComparable<PolynomialRegression>
-