Shape constraints in ‘BslineQuantReg’

Introduction

This vignette covers shape constraints in B-spline quantile regression using the Karlin-Studden SOCP formulation. Shape constraints allow you to incorporate prior knowledge about the function’s behavior (monotonicity, convexity, etc.) into the estimation process.

Shape constraint for quantile regression with cubic splines is initially the main purpose of this package, based on the paper of Abbes (2025). It has been extended to other degrees, and the package also allows to switch to ordinary mean square regression.

Available Demos

The package includes several demos that illustrate shape constraints:

# Example of available demos
#dev.new()
# Run specific demos:
# demo(package = "BsplineQuantReg","monotonicity")      #- Increasing/decreasing constraints
# demo(package = "BsplineQuantReg","convexity")         #- Convexity/concavity constraints  
# demo(package = "BsplineQuantReg","der3")              #- Third derivative constraints
# demo(package = "BsplineQuantReg","comprehensive")     #- All constraint types
# demo(package = "BsplineQuantReg","temperature") #and
#demo(package = "BsplineQuantReg",temperature2) #- Real-world example with temperature data

Uniform Constraints

Monotonicity Constraints : example code

Monotonicity constraints force the fitted function to be non-decreasing (monot = 1) or non-increasing (monot = -1).

# Generate data
set.seed(42)
n <- 100
x <- seq(0, 1, length.out = n)
y <- 2*x + 0.5*sin(4*pi*x) + 0.1*rnorm(n)
knots <- quantile(x, probs = seq(0, 1, length.out = 10))
# Unconstrained
fit_uncon <- quantile_spline(x, y, knots,
                             tau = 0.5)
# Increasing constraint
fit_inc <- quantile_spline(x, y, knots, tau = 0.5,
                           monot = 1)
# Decreasing constraint
fit_dec <- quantile_spline(x, y, knots, tau = 0.5,
                           monot = -1)

Monotonicity Constraints : example view

Convexity Constraints: code example

Convexity constraints force the second derivative to be non-negative (convcons = 1) or non-positive (convcons = -1).

# Convex function
y_conv <- (x - 0.5)^2 + 0.05 * rnorm(n)

fit_convex   <- quantile_spline(x, y_conv, knots, 
                                tau = 0.5, convcons = 1)
fit_uncon_v  <- quantile_spline(x, y_conv, knots,
                                tau = 0.5, convcons = 0)

Convexity Constraints: example view

Third Derivative Constraints: code example

Third derivative constraints are not commonly intuitive. They control the variations of the second derivative, that is to say the evolution of curvature. Some examples show that the control of its sign allows sometimes enforcing smoothness of the curvature, and strongly enforces smoothness of the curve.

# Cubic function with varying third derivative
y_cubic <- x^3 - 1.5 * x^2 + 0.5 * x + 0.05 * rnorm(n)

fit_der3_pos <- quantile_spline(x, y_cubic, knots, tau = 0.5, der3cons = 1)
fit_der3_nc  <- quantile_spline(x, y_cubic, knots, tau = 0.5, der3cons = 0)

Third Derivative Constraints: example view

Partial Constraints

Partial constraints apply monotonicity or convexity only on specific intervals, not the entire domain.

Partial Monotonicity

# Increasing only on first half
# Increasing only on the first half
monot_partial <- c(rep(1, 5), rep(0, 4))

fit_partial <- quantile_spline(x, y, knots, tau = 0.5,
                               monot = monot_partial)
fit_uncon_p <- quantile_spline(x, y, knots, tau = 0.5, monot = 0)

partial monotonicity view

Partial Convexity

# Convex on the right half only
conv_partial <- rep(0, length(knots))
for (i in seq_along(knots)) {
  if (knots[i] > 0.5) conv_partial[i] <- 1
}

fit_conv_partial <- quantile_spline(x, y, knots, tau = 0.5,
                                    convcons = conv_partial)
fit_uncon_pc     <- quantile_spline(x, y, knots, tau = 0.5,
                                    convcons = 0)

Partial Convexity : the view

Mixed Constraints

mixed constraints: the code

Multiple shape constraints can be applied simultaneously.

# Increasing AND convex
fit_both   <- quantile_spline(x, y, knots, tau = 0.5,
                              monot = 1, convcons = 1)
fit_uncon_m <- quantile_spline(x, y, knots, tau = 0.5,
                               monot = 0, convcons = 0)


fit_both <- quantile_spline(x, y, knots, tau = 0.5, 
                            monot = 1, convcons = 1)

mixed constraints: The view

Constraints with Multiple Knots

When using knot multiplicities, constraints need to be adapted to the reduced regularity. Constraints apply only on regular pieces : monotonicity is ensured on continuous pieces only.

# Create knots with multiplicity at a point
sn_mult <- c(0, 0, 0, 0, 0.3, 0.4,0.4,0.4, 0.5,
             0.5,0.5, 0.7, 1, 1, 1, 1)
#basis_mult <- Bspline_base(sn_mult, degree = 3)
knots_mult <- c(0, 0.3, 0.3,0.3,0.3,0.4, 
                0.5,0.7,0.7,0.7,0.7, 1)

Constraints with Multiple Knots : view

# Fit with constraints
fit_mult <- quantile_spline(x, y, knots_mult, tau = 0.5, monot = 1)
plot(x, y, pch = 16, cex = 0.5, col = "gray", 
     main = "Constraints with Multiple knots")
lines(x_eval, spline_eval(fit_mult, x_eval), col = "blue", lwd = 2)

Recap: Visualizing Constraint Effects

Karlin-Studden SOCP Formulation

The shape constraints are implemented using the Karlin-Studden characterization of non-negative polynomials of degree 2 or 3.

Depending on the degree of the spline (3 or 4), these constraints apply to the first or second derivative:

Constraints by Degree: table

  • Degree 0 (constant): monotonicity (constant). No convexity, no third derivative.
  • Degree 1 (linear): monotonicity (linear). No convexity, no third derivative.
  • Degree 2 (quadratic): monotonicity (linear), convexity (constant). No third derivative.
  • Degree 3 (cubic): monotonicity (Karlin), convexity (linear), third derivative (linear or constant).
  • Degree 4 (quartic): monotonicity (Karlin), convexity (Karlin), third derivative (linear).
  • Degree 5 (quintic): convexity (Karlin). No monotonicity, third derivative not implemented.
  • Degree \(\geq 6\): no constraint available.

“Karlin” here means “quadratic” inequality.

Constraints by Degree: Comment

Other constraints pointwise constraints, at knots or not, are not implemented, but they would be implemented as simple linear inequalities.

The complete set of constraints forms a Second-Order Cone Program (SOCP). This SOCP formulation ensures exact shape constraints, unlike approximations that only enforce constraints at knots or at a finite set of points.

The particular structure of the problem allows the use of the CVXR package, which handles this type of mixed quadratic/linear conic problems efficiently through its DCP (Disciplined Convex Programming: see for instance https://www.cvxpy.org/tutorial/dcp/)). framework and supports multiple solvers (CLARABEL, OSQP, ECOS, SCS).

Summary

The package provides: - Uniform constraints: Apply globally - Partial constraints: Apply on specific intervals - Mixed constraints: Multiple constraints simultaneously - SOCP formulation: Exact (not approximated) constraints on the whole intervals. Constraints concern monotone or convex