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.
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.
## 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
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.
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
## Effective knots: 0 1 2 3 4 5
Cubic B-spline basis built from the extended knot sequence.
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
Original basis and its first three derivative bases.
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()”.“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)
))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.
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
A B-spline can be stored in two forms:
non_callable_spline): a
plain list, accessed with $knot, $degree,
$coeff. Evaluation requires “spline_eval()”.callable_spline): an R
function, evaluated directly as f(x). The “make_spline()”
function switches between the two:## [1] "non_callable_spline" "list"
## [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
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
## 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
## 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)
Two functions are available for evaluation:
x.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
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:
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
## Max |y_bs - callable| = 1.776357e-15
## 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)
## max(abs(y - y_pp)) 1.776357e-15
## [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)
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). \]
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 \(\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 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.
der parameter :
constructionThe 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.
der parameter : view
the plotsThis method follows the same path as “spline_eval()”:
# 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
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
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:
# 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)## 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.
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
## Max differences: Method 1 vs Method 2: 8.999983e-10 Method 1 vs Method 3: 0
## Method 2 vs Method 3: 8.999983e-10
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.
Cubic B-spline basis with a double knot at x = 3.
view_basis(basis) with basis= basis1, basis2, bsasis3, basis4
| 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.