# B-splines: Matlab code

B-splines are mathematical curves with convenient properties. The B in B-spline means basis. A curve y(t) is defined in terms of weights w and knots k, such that $y(t) = \sum_i w_i B_i(t,\mathbf{k})$. Each basis function is a piecewise polynomial with compact support determined by the position of the knots. The order of the polynomial is determined by the difference between the number of knots and weights. If the number of knots is 2 more than the number of weights, you get a piecewise linear basis function which looks like a little tent. For 4 extra knots, you get a “hump” defined by a cubic, which looks like a Gaussian but has compact support (it is exactly zero outside its interval). Chapter 5 of The Elements of Statistical Learning offers an excellent introduction to splines.

One example use of B-splines I discussed recently is BARS, or Bayesian adaptive regression splines. Here a PSTH is approximated with a smooth curve, and the knots are positioned adaptively. Generalized additive models also frequently use B-splines as smoothers. In general, B-splines (especially the cubic variety) excel as non-parametric smoothers. Linear splines have been used to infer input nonlinearities in reverse correlation-type experiments.

Here is a lightweight Matlab class that implements B-Splines. It allows one to fit, evaluate and differentiate B-Splines, and is well documented.

Update (08 Sep. 2011): I’ve now included C code in the Matlab package to evaluate B-splines much more rapidly (by a factor ranging from 5x to 50x). This now makes the code appropriate for implementing large-scale GAMs.

### 9 responses to “B-splines: Matlab code”

1. Patrick, thanks for the excellent explanation of the concept of splines. The chapter in the book is very insightfull as well, but they don’t use the definition of “knots and weights” in the same manner.
Anyway, thanks again.

2. Joanna Klukowska says:

I downloaded your package, but so far I was not able to run it successfully. When I try to run TryBSplie.m file, I get an error:

??? Error: File: fastBSpline.m Line: 137 Column: 19
Expression or statement is incorrect–possibly unbalanced (, {, or [.

This refers to the second line of the following snippet:

if this.usemex
[~,thebins] = histc(x,this.knots);
firstknot = max(min(thebins-order,length(this.weights)),1);
lastknot = min(thebins+1,length(this.weights)+1);

Sx = evalBSpline(x,firstknot,lastknot,this.knots,this.weights,order);
else

I have some basic experience with Matlab, but I never used classes. I do not know what ~ notation does.
Could this be a problem with my version of Matlab? I am running the student version Matlab 7.8.0 (R2009a).

Joanna

• xcorr says:

The syntax [~,thebins] = histc(x,this.knots); means that the first argument is discarded. This syntax is only supported for Matlab >= 2009b, I believe. Try replacing with [discard,thebins] = histc(x,this.knots);

• Joanna Klukowska says:

Thanks a lot! This worked and the code is running now.

3. xcorr says:

It might be helpful to look at a book on generalized additive models or chapter 5 of the Hastie book, The Elements of Statistical Learning.

Thanks very much.

Is there is any way to solve this problem?

Regards.

AHAA

5. xcorr says:

With B-splines, you don’t specify end conditions directly; they’re implicitly determined by the knot vector and the weights you use. They’re not ideal for what you want to do.

Hi There,

I was using tow type B_spline the “SPAPS” and ‘SPAP2″ to fit and smooth scattered data of 3000 element. each one of them does not give me what I want. so I’m looking for specific B_spline that satisfies the following conditions:

-least square or higher order.
– with few intervals (breaks) << x_data points.
-you can control the end conditions.
-should go through the origin.
-second derivative should go to zero at the right end.
-preferably apply smoothing.

I am looking forwards for you help guys.

Regards

AHAA