Record Class BSpline

java.lang.Object
java.lang.Record
smile.regression.gam.BSpline
Record Components:
degree - the degree of the B-spline polynomial (default: 3 for cubic splines).
knots - the full (extended) knot vector, including repeated boundary knots.
xmin - the minimum value of the predictor (used for clamping at prediction time).
xmax - the maximum value of the predictor (used for clamping at prediction time).
numBasis - the number of basis functions (columns of the basis matrix).
All Implemented Interfaces:
Serializable

public record BSpline(int degree, double[] knots, double xmin, double xmax, int numBasis) extends Record implements Serializable
B-spline basis matrix for a single predictor. A B-spline (basis spline) of degree d is a piecewise polynomial defined on a knot sequence. This class builds the basis matrix and the second-difference penalty matrix used in penalized regression splines (P-splines).

The resulting basis has k + d columns, where k is the number of inner knots. With a second-difference penalty on the B-spline coefficients, this achieves smoothing similar to a cubic smoothing spline but with the computational advantage of a banded penalty.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    BSpline(double[] x, int df)
    Constructor with default cubic degree.
    BSpline(double[] x, int df, int degree)
    Constructor.
    BSpline(int degree, double[] knots, double xmin, double xmax, int numBasis)
    Creates an instance of a BSpline record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    double[]
    basis(double x)
    Evaluates all B-spline basis functions at a single point x.
    double[][]
    basis(double[] x)
    Computes the B-spline basis matrix for a set of predictor values.
    int
    Returns the value of the degree record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    double[]
    Returns the value of the knots record component.
    int
    Returns the value of the numBasis record component.
    double[][]
    Computes the second-difference penalty matrix D'D of size numBasis x numBasis.
    final String
    Returns a string representation of this record class.
    double
    Returns the value of the xmax record component.
    double
    Returns the value of the xmin record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BSpline

      public BSpline(double[] x, int df, int degree)
      Constructor.
      Parameters:
      x - the predictor values (used to determine the knot placement).
      df - the desired degrees of freedom (number of basis functions). Must be >= degree + 1.
      degree - the polynomial degree (3 = cubic).
    • BSpline

      public BSpline(double[] x, int df)
      Constructor with default cubic degree.
      Parameters:
      x - the predictor values.
      df - the desired degrees of freedom (number of basis functions).
    • BSpline

      public BSpline(int degree, double[] knots, double xmin, double xmax, int numBasis)
      Creates an instance of a BSpline record class.
      Parameters:
      degree - the value for the degree record component
      knots - the value for the knots record component
      xmin - the value for the xmin record component
      xmax - the value for the xmax record component
      numBasis - the value for the numBasis record component
  • Method Details

    • basis

      public double[][] basis(double[] x)
      Computes the B-spline basis matrix for a set of predictor values. Returns an n x numBasis matrix where each row contains the basis function evaluations for one observation.
      Parameters:
      x - the predictor values.
      Returns:
      the basis matrix (n rows × numBasis columns).
    • basis

      public double[] basis(double x)
      Evaluates all B-spline basis functions at a single point x.
      Parameters:
      x - the point at which to evaluate the basis.
      Returns:
      an array of length numBasis of basis values.
    • penalty

      public double[][] penalty()
      Computes the second-difference penalty matrix D'D of size numBasis x numBasis. This penalizes the roughness of the fitted smooth function by penalizing differences in adjacent B-spline coefficients (a discrete approximation to the integrated squared second derivative).
      Returns:
      the penalty matrix.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • degree

      public int degree()
      Returns the value of the degree record component.
      Returns:
      the value of the degree record component
    • knots

      public double[] knots()
      Returns the value of the knots record component.
      Returns:
      the value of the knots record component
    • xmin

      public double xmin()
      Returns the value of the xmin record component.
      Returns:
      the value of the xmin record component
    • xmax

      public double xmax()
      Returns the value of the xmax record component.
      Returns:
      the value of the xmax record component
    • numBasis

      public int numBasis()
      Returns the value of the numBasis record component.
      Returns:
      the value of the numBasis record component