B-spline and Basis Manipulation

Introduction

This part covers the B-spline basis manipulation tools provided by the ‘BsplineQuantReg’ package. B-splines are a powerful tool for nonparametric regression and function approximation. The package provides a comprehensive set of functions for building, manipulating, and evaluating B-spline bases. The calculations are standard and follow authors such as de Boor or Schumaker.

Building a B-spline basis

Basic construction

The primary function for building a B-spline basis is “Bspline_base()”, which takes an extended knot sequence and returns the basis functions in piecewise polynomial form. see the help: base
Coefficients in the local bases, in the form of an 3-d array [j,nu,coeff], j : the number of the spline in the basis, nu: the number of the interval in the extended notation, coeff : the coefficients in decreasing (matlab) order convention on the local bases (t-s_nu)^l, l=3.2.1 base[j,,] is a matrix of piecewise polynomial function compatible with the pp-form.

# Extended knot sequence for cubic B-splines on [0, 5]
# with internal knots at 1, 2, 3, 4
sn <- c(0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5)

# Build the basis
basis <- Bspline_base(sn, degree = 3)

Basic construction: Inspect the basis structure

## List of 4
##  $ base    : num [1:8, 1:11, 1:4] 0 0 0 0 0 0 0 0 0 0 ...
##  $ base0   : num [1:8, 1:11, 1:4] 0 0 0 0 0 0 0 0 0 0 ...
##  $ ext_knot: num [1:12] 0 0 0 0 1 2 3 4 5 5 ...
##  $ knot    : num [1:6] 0 1 2 3 4 5

Visualising the basis

The view_basis() function provides a quick visualisation of all basis functions.

Cubic B-spline basis on [0, 5] with internal knots at 1, 2, 3, 4.

Cubic B-spline basis on [0, 5] with internal knots at 1, 2, 3, 4.

Understanding the extended knot sequence

For a B-spline of degree \(d\) with \(k_n\) intervals, the extended knot sequence has length \(k_n + 1 + 2d\):

# d = 3, kn = 5 (intervals: [0, 1], [1, 2], [2, 3], [3, 4], [4, 5])
# Length: 5 + 1 + 6 = 12
sn_example <- c(0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5)
# The first and last d knots are repeated to enforce boundary conditions
basis_example <- Bspline_base(sn_example, degree = 3)
cat("Number of basis functions:", basis_example$n_splines, "\n")
## Number of basis functions: 8
cat("Effective knots:", basis_example$knot, "\n")
## Effective knots: 0 1 2 3 4 5

Visualising B-spline bases

Visualising a B-spline basis

view_basis(basis_example)
Cubic B-spline basis built from the extended knot sequence.

Cubic B-spline basis built from the extended knot sequence.

Visualising different degrees : prepare the bases

Visualising different degrees : the view with view_basis()

Customising visualisation

x_fine <- seq(-0.5, 5.5, length.out = 300)
view_basis(basis, x_values = x_fine)
Cubic B-spline basis evaluated on an extended range [-0.5, 5.5].

Cubic B-spline basis evaluated on an extended range [-0.5, 5.5].

Differentiating a B-spline basis

The Bspline_base_deriv() function computes the basis of derivatives of a B-spline basis.

The degree of the differentiated basis is lowered accordingly

## Original degree: 3
## 1st derivative degree: 2
## 2nd derivative degree: 1
## 3rd derivative degree: 0

Visualising derivative bases

Original basis and its first three derivative bases.

Original basis and its first three derivative bases.

B-spline objects

What is a B-spline object?

B-spline objets are the outputs of all the main functions “SplineCubicQuant()”, “SplineQuarticQuant()”, etc. and “SplinePolynQuant()”.

A B-spline object in ‘BsplineQuantReg’ is a list with three components:

  • knot: the effective knot partition (endpoints included, not the extended sequence);
  • degree: the polynomial degree \(d\);
  • coeff: the vector of coefficients on the B-spline basis. This representation is compatible with the Matlab and Python representations of B-splines: the same three components are used to describe a spline in ‘BsplineQuantRegpy’ and in the Matlab toolbox. (but scipy uses the extended knot representation) Note: The extreme knots must be repeated \(d + 1\) times in the extended sequence used by “Bspline_base()”.

Create a B-spline. Method 1: from a bare list

“make_spline()” accepts a list with knot, degree and coeff:

tn <- c(0, 1, 2, 3, 4, 5)
bs <- make_spline(list(
  knot   = tn,
  degree = 3,
  coeff  = c(1, -2, 3, -1, 2, 1, 0, 0.5)
))

Create a B-spline. Method 2: from a basis

This is an equivalent way to build a B-spline object in “BsplineQuantReg’.

Build the basis with “Bspline_base()”, then attach the coefficients: The coefficients, knots, degree stay attached to the object.

sn <- c(0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5)
basis <- Bspline_base(sn, degree = 3)
basis$coeff <- c(1, -2, 3, -1, 2, 1, 0, 0.5)

Methods to create a B-spline — compare

The two objects are equivalent and evaluate to the same function:

x <- seq(0, 5, length.out = 200)
y1 <- spline_eval(bs, x)
y2 <- spline_eval(basis, x)

cat("Max |y1 - y2| =", max(abs(y1 - y2)), "\n")
## Max |y1 - y2| = 0

Methods to create a B-spline — view

Callable and non-callable B-splines

A B-spline can be stored in two forms:

  • Non-callable (non_callable_spline): a plain list, accessed with $knot, $degree, $coeff. Evaluation requires “spline_eval()”.
  • Callable (callable_spline): an R function, evaluated directly as f(x). The “make_spline()” function switches between the two:

Callable and non-callable B-splines: switch

bs_nc <- make_spline(bs, callable = FALSE)
class(bs_nc)   
## [1] "non_callable_spline" "list"
bs_c  <- make_spline(bs_nc, callable = TRUE)
class(bs_c)    
## [1] "callable_spline" "function"
# Same evaluation, two syntaxes
x <- seq(0, 5, length.out = 100)
y_nc <- spline_eval(bs_nc, x)
y_c  <- bs_c(x)

cat("Max |y_nc - y_c| =", max(abs(y_nc - y_c)), "\n")
## Max |y_nc - y_c| = 0

Getting the parameters of a B-spline object: non callable spline directly

The “get_parameter()” function allows to obtain the parameters of both non-callable and callable Bsplines, as well as their equivalent pp-object (callable and non callable) A non-callable B-spline is simply a list, and its parameters can be obtained directly.

##  obtain the parameters directly from the non callable bspline
## [1] 0 1 2 3 4 5
## [1]  1.0 -2.0  3.0 -1.0  2.0  1.0  0.0  0.5
## [1] 3
## for bs_nc (non callable)
## $degree
## [1] 3
## 
## $knot
## [1] 0 1 2 3 4 5
## 
## $coeff
## [1]  1.0 -2.0  3.0 -1.0  2.0  1.0  0.0  0.5
## 
## $result
## NULL

Getting the parameters of a B-spline object : callable spline

## for bs_c (callable)
## $degree
## [1] 3
## 
## $knot
## [1] 0 1 2 3 4 5
## 
## $coeff
## [1]  1.0 -2.0  3.0 -1.0  2.0  1.0  0.0  0.5
## 
## $result
## NULL

Getting the parameters of a B-spline object :

Alterniveley print the informations

## Non callable Spline List 
## ======================== 
##  $degree:  3 
##  $knot : [ 0, 1, 2, 3, 4, 5 ] ( 6 knots ) 
##  $coeff (rounded 10^(-7)) :[ 1, -2, 3, -1, 2, 1, 0, 0.5 ]  ( dim Basis is  8 )
## callable_spline Object
## ======================
##   Degree: 3 
##   Knots ( 6 ):  0 1 2 3 4 5 
##   Coefficients ( 8 ):  1 -2 3 -1 2 1 0 0.5 
## for result, type get_parameter(spline)

Evaluating a B-spline: description and code

Two functions are available for evaluation:

  • “spline_eval(bs, x)”: evaluates the spline (linear combination of basis functions) at the points x.
  • “bs_direct(basis, x)”: evaluates the basis matrix at x, of size n_splines × length(x).

The two are related by matrix multiplication:

x <- seq(0, 5, length.out = 100)

# Direct evaluation
y_direct <- spline_eval(bs, x)

# Via the basis matrix
Bmat <- bs_direct(Bspline_base(c(0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5),
                               degree = 3), x)
y_matrix <- t(Bmat) %*% bs_nc$coeff

cat("Max |y_direct - y_matrix| =", max(abs(y_direct - y_matrix)), "\n")
## Max |y_direct - y_matrix| = 0

Evaluating a B-spline: the view

Converting a Bspline to PP-form

Use of “Bsplinetopp()” : example code

The piecewise polynomial (PP) form represents the spline piece by piece, as a polynomial on each interval. Conversion is done with “Bsplinetopp()”:

## [1] "non_callable_pp"
## [1] "callable_pp" "function"

The PP-form can be evaluated with “evalpp()” (non-callable) or directly (callable). The two representations of the spline coincide:

Use of “Bsplinetopp()” : example comparison

x <- seq(0, 5, length.out = 200)
y_bs <- spline_eval(bs, x)
y_pp <- evalpp(pp_nc, x)
y_pc <- pp_c(x)

cat("Max |y_bs - evalpp| =", max(abs(y_bs - y_pp)), "\n")
## Max |y_bs - evalpp| = 1.776357e-15
cat("Max |y_bs - callable| =", max(abs(y_bs - y_pc)), "\n")
## Max |y_bs - callable| = 1.776357e-15

Use of “Bsplinetopp()” : example view

Use of “Bsplinetopp()” : example comparison again

pp <- Bsplinetopp(bs, Bsbasis = basis$basis, callable = FALSE)
print(pp)
## Piecewise Polynomial (PP) (non-callable)
## ================================
##   $degree: 3 
##   $knot: 0 1 2 3 4 5 
##  coefficients dimension: 5 x 4 
## 
##   $coeff:
##     Intervals 1 : -7.4167, 16.5, -9, 1 
##     Intervals 2 : 3.0833, -5.75, 1.75, 1.0833 
##     Intervals 3 : -1.8333, 3.5, -0.5, 0.1667 
##     Intervals 4 : 0.5833, -2, 1, 1.3333 
##     Intervals 5 : 1.0833, -0.25, -1.25, 0.9167 
##  Usage: pp_eval(pp, x_values)
x_plot<-x
y_pp <- evalpp(pp, x_plot)
y <- spline_eval(bs, x_plot)
## max(abs(y - y_pp)) 1.776357e-15

Conversion to Callable PP objects

## [1] "callable_pp" "function"
## Callable Piecewise Polynomial (PP) Object
## ==========================================
##   Degree: 3 
##   Intervals: 5 
##   Knots ( 6 ) : 0, 1, 2, 3, 4, 5 
##  coefficients dimension: 5 x 4 
## 
##   $coeff:
##     Intervals 1 : -7.4167, 16.5, -9, 1 
##     Intervals 2 : 3.0833, -5.75, 1.75, 1.0833 
##     Intervals 3 : -1.8333, 3.5, -0.5, 0.1667 
##     Intervals 4 : 0.5833, -2, 1, 1.3333 
##     Intervals 5 : 1.0833, -0.25, -1.25, 0.9167 
##  Usage: pp(x_values) or evalpp(pp, x_values)

Differentiating a B-spline function

There are at least four methods to compute evaluate derivatives of a B-spline function at given values with this package. In a given configuration with \(k\) knots \(t_\nu\) including extremities, and degree \(d \in \mathbb{N}\), the B-spline basis is \((\pi_j(x))_{j=1,\dots,N}\), with \(N = k + d - 1\).

A spline \(f(x)\) with this degree and these knots is a vector written on the basis. The derivative is obtained by differentiating each term of the sum:

\[ f(x) = \sum_{j=1}^{N} \alpha_j \pi_j(x). \Rightarrow f'(x) = \sum_{j=1}^{N} \alpha_j \pi_j'(x). \]

First and second method for derivatives : description

The first method. Each B-spline \(\pi_\nu\) is described with its coefficients on the local power basis for each of its pieces. When computing the basis, these local coefficients are stored in the BB$base[, nu, ] array. The differentiation is done on these local polynomial pieces. The der parameter is passed through the “spline_eval()” function to compute a derivative of the basis.

The second method The differentiation is a decomposition of the function spline_eval when the parameter der is activated. Step by step : differentiating the Bspline basis, then evaluate the basis at the given values, and at last reconstruct the derivative \(f'(x)\) with these values and the same coefficients \(\alpha_i\) as those of \(f(x)\) in its writing on the basis.

Third method: description

Third method: description \(\pi_{d,j}\) be the \(j\)-th spline of degree \(d\), and \(\pi_{d,j}'\) be the \(j\)-th spline of degree \(d-1\) \[ f'(x)=\sum_{j=1}^{N-1} \alpha_j \pi_{d,j}'(x) = \sum_{j=1}^{N} \alpha_j' \pi_{d-1,j}(x), \] \(\alpha_j'\): the coefficients of f’(x) on the basis of degree \(d-1\). Formula for \(\pi_{d,j}'\): that derived by de Boor, implemented in “Bspline_deriv()”. \[\alpha_i'=\frac{\alpha_{j+1}-\alpha_j}{(t_{i+d}-t_i)/d}\]

The fourth method : description

The fourth method When computing its PP-form, the spline is written as a sum of disjointed piecewise polynomials of degree \(d\).

\[ f(x) = \sum_{\nu=1}^{k-1} p_\nu(x) \mathbb{1}_{\left[t_\nu, t_{\nu+1}\right[}, \] It suffices to differentiate each polynomial piece \(p_\nu\). In practice, the Bspline is written as a pp-object with Bsplinetopp(), and differentiated with the pp_deriv() function.

Method 1: “spline_eval()” with the der parameter : construction

The simplest method is to use “spline_eval()” with the “der” argument. This function calculates the derivative of the B-spline basis, then the values of the differentiated basis, and finally evaluates the function.

bs_function<-bs # bs is  the preceding example 

x_plot <- seq(0, 5, length.out = 100)
y  <- spline_eval(bs_function, x_plot, der = 0)
y1 <- spline_eval(bs_function, x_plot, der = 1)
y2 <- spline_eval(bs_function, x_plot, der = 2)
y3 <- spline_eval(bs_function, x_plot, der = 3)

Method 1: “spline_eval()” with the der parameter : view the plots

Method 2: Evaluation using basis derivatives : the code construction of a derivative spline

This method follows the same path as “spline_eval()”:

  1. Compute the derivative basis.
  2. Evaluate the basis at points using “bs_direct()”.
  3. Use matrix multiplication with the coefficients.
# Step 1: Compute the derivative basis (derivative order = 1)
basis_der1 <- Bspline_base_deriv(basis, der = 1)
# Step 2: Evaluate the derivative basis at points using bs_direct

x_plot <- seq(0, 5, length.out = 100)
Bvalues_der1 <- bs_direct(basis_der1, x_plot)

Method 2: Evaluation using basis derivatives : the code

# Step 3: Compute the derivative values using matrix multiplication
y_der1 <- t(Bvalues_der1) %*% basis$coeff

# Step 4: Compare with spline_eval (direct method)
cat("Maximum difference:", max(abs(y_der1 - y1)), "\n")
## Maximum difference: 0
basis_der2 <- Bspline_base_deriv(basis, der = 2)
Bvalues_der2 <- bs_direct(basis_der2, x_plot)
y_der2 <- t(Bvalues_der2) %*% basis$coeff
basis_der3 <- Bspline_base_deriv(basis, der = 3)
Bvalues_der3 <- bs_direct(basis_der3, x_plot)
y_der3 <- t(Bvalues_der3) %*% basis$coeff
## Maximum difference |y_der2-y2|: 0
## Maximum difference |y_der3-y3|: 0

Method 2: Step-by-step evaluation using basis derivatives : view the spline

Method 3: “Bspline_deriv()” for derivative coefficients

The second method computes the coefficients of the \(d\)-th derivative B-spline directly on a B-spline basis, with the new degree \(d_{\text{new}} = d - \text{der}\).

## max(abs(y1 - y1_coeff)) 3.683254e-10
## max(abs(y2 - y2_coeff)) 7.67681e-10

Method 4: Differentiating the PP-form with “pp_deriv()” : the method

The fourth method relies on the PP-form representation of the spline. Since, on each interval, the spline coincides with a polynomial \(p_\nu\), its derivative is simply the derivative of each \(p_\nu\). The function “pp_deriv()” implements this piecewise differentiation:

  1. Convert the B-spline to its PP-form with “Bsplinetopp()”.
  2. Differentiate each piece with “pp_deriv()”.
  3. Evaluate the resulting PP-form with “evalpp()” (or directly, if the PP object is callable).

Method 4: Differentiating the PP-form with “pp_deriv()” : the code

# Convert the B-spline to PP-form (non-callable, for clarity)
pp <- Bsplinetopp(bs_function, callable = FALSE)

# Differentiate each piece
pp_d1 <- pp_deriv(pp, der = 1)
pp_d2 <- pp_deriv(pp, der = 2)
pp_d3 <- pp_deriv(pp, der = 3)

# Evaluate the differentiated PP-forms on the same grid
y_pp<- evalpp(pp, x_plot)
y1_pp <- evalpp(pp_d1, x_plot)
y2_pp <- evalpp(pp_d2, x_plot)
y3_pp <- evalpp(pp_d3, x_plot)

Method 4: Differentiating the PP-form with “pp_deriv()” : the view

## Max |y - y_pp| = 1.776357e-15     Max |y1 - y1_pp| = 5.77316e-15
## Max |y2 - y2_pp| = 3.552714e-15    Max |y3 - y3_pp| = 4.440892e-16
The four differentiation methods coincide.

The four differentiation methods coincide.

Derivatives at knots

Use Spline_der_knot() function to compute derivative values at knot points rapidly. This is a very simple function, but with limited use.

##      [,1]  [,2]  [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    0  0.00  0.00  0.0  0.0    0    0    0
## [2,]    0  0.00  0.00  0.0  0.0    0    0    0
## [3,]    0  0.00  0.00  0.0  0.0    0    0    0
## [4,]   -3  3.00  0.00  0.0  0.0    0    0    0
## [5,]    0 -0.75  0.25  0.5  0.0    0    0    0
## [6,]    0  0.00 -0.50  0.0  0.5    0    0    0

Coherence of derivative evaluation methods with other degree

degree 5 for coherence of the derivative methods example code

degree 5 for coherence of the derivative methodds : example view

## Max differences:   Method 1 vs Method 2: 8.999983e-10   Method 1 vs Method 3: 0
## Method 2 vs Method 3:  8.999983e-10

Advanced: knot multiplicity and regularity

Knot multiplicity controls the smoothness of B-splines at knot points. The regularity at a knot is determined by the multiplicity \(m\) and the degree \(d\): the spline is \(C^{d - m}\) at that knot.

Creating B-splines with multiple knots

Cubic B-spline basis with a double knot at x = 3.

Cubic B-spline basis with a double knot at x = 3.

Effect of multiplicity on regularity : example code

Effect of multiplicity on regularity : example view

view_basis(basis) with basis= basis1, basis2, bsasis3, basis4

Summary

Feature Function Description
Build basis Bspline_base() Create B-spline basis
View basis view_basis() Visualise basis functions
Differentiate basis Bspline_base_deriv() Compute derivative basis
Differentiate function spline_eval(der = ...) Evaluate derivatives
Derivative coefficients Bspline_deriv() Get derivative B-spline
PP conversion Bsplinetopp() Convert to PP form
Callable spline make_spline() Create callable function
Extract params get_parameters() Get parameters from callable
Derivatives at knots Spline_der_knot() Efficient knot derivatives

All B-spline functions are implemented in pure R and support B-splines of any degree (greater than 0), with full derivative and PP-form capabilities. The limitation to degree 4 applies only to regression with constraints over intervals.