Class PolynomialRegression

java.lang.Object
com.flarerobotics.lib.math.PolynomialRegression
All Implemented Interfaces:
Comparable<PolynomialRegression>

public class PolynomialRegression extends Object implements 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

    Constructors
    Constructor
    Description
    PolynomialRegression(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 Type
    Method
    Description
    double
    beta(int j)
    Returns the jth regression coefficient.
    int
    Compare lexicographically.
    int
    Returns the degree of the polynomial to fit.
    double
    predict(double x)
    Returns the expected response y given the value of the predictor variable x.
    double
    R2()
    Returns the coefficient of determination R2.
    Returns a string representation of the polynomial regression model.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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 variable
      y - the corresponding values of the response variable
      degree - the degree of the polynomial to fit
      Throws:
      IllegalArgumentException - if the lengths of the two arrays are not equal
    • PolynomialRegression

      public PolynomialRegression(double[] x, double[] y, int degree, String variableName)
      Performs a polynomial reggression on the data points (y[i], x[i]).
      Parameters:
      x - the values of the predictor variable
      y - the corresponding values of the response variable
      degree - the degree of the polynomial to fit
      variableName - 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 the jth regression coefficient.
      Parameters:
      j - the index
      Returns:
      the jth 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 response y given the value of the predictor variable x.
      Parameters:
      x - the value of the predictor variable
      Returns:
      the expected response y given the value of the predictor variable x
    • toString

      public String toString()
      Returns a string representation of the polynomial regression model.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the polynomial regression model, including the best-fit polynomial and the coefficient of determination R2
    • compareTo

      public int compareTo(PolynomialRegression that)
      Compare lexicographically.
      Specified by:
      compareTo in interface Comparable<PolynomialRegression>