PolyFit class

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.

References

  1. "Polynomial regression". https://en.wikipedia.org/wiki/Polynomial_regression. Retrieved 2019-07-22.
  2. "Polynomial regression". //https://rosettacode.org/wiki/Polynomial_regression#C_sharp. Retrieved 2019-07-22.
  3. "polynomial.py". https://github.com/numpy/numpy/blob/v1.15.0/numpy/lib/polynomial.py#L393-L606. Retrieved 2019-07-22.
  4. "numpy.polyfit". https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.polyfit.html. Retrieved 2019-07-22.
  5. "Princeton Analysis Polynomial Regression". https://algs4.cs.princeton.edu/14analysis/PolynomialRegression.java.html. Retrieved 2019-07-22.

Examples

const int degree = 2;
var x = Array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]);
var y = Array([1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0]);
var p = PolyFit(x, y, degree);

print(p);

/*
2.999999999999736 x^2 +  2.0000000000002807 x +  1.0000000000000122   (R^2 = 0.9922700105981427)
*/

Constructors

PolyFit(Array x, Array y, int degree)
Performs a polynomial reggression on the data points (y[i], x[i]). x the values of the predictor variable y the corresponding values of the response variable degree the degree of the polynomial to fit

Properties

beta Array2d
getter/setter pair
degree int
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sse double
getter/setter pair
sst double
getter/setter pair
variableName String
getter/setter pair

Methods

coefficient(int j) double
Returns the {@code j}th regression coefficient. j the index return the {@code j}th regression coefficient
coefficients() Array
return the coefficients of the polynomial regression p(x) = p0 * x**deg + ... + p[deg]
compareTo(PolyFit that) int
Compare lexicographically.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
polyDegree() int
Returns the degree of the polynomial to fit. return the degree of the polynomial to fit
predict(double x) double
Returns the expected response {@code y} given the value of the predictor variable {@code x}. x the value of the predictor variable return the expected response {@code y} given the value of the predictor variable {@code x}
R2() double
Returns the coefficient of determination R2. return the coefficient of determination R2, which is a real number between 0 and 1
toString() String
Returns a string representation of the polynomial regression model. return a string representation of the polynomial regression model, including the best-fit polynomial and the coefficient of determination R2
override

Operators

operator ==(Object other) bool
The equality operator.
inherited