Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have some problems in writing a module for spline smoothing. Actually, I have been trying for about two weeks. My listing is here:

SplSmooth[data_, knots_, lambda_, degree_] := 
  Module[{M, Knots, NKnots, NBasis, X, Dsq, a},
   M = Length@data;
   Knots = Flatten@{Table[1, {i, 1, degree}], knots,Table[M, {i,1,degree}]};
   NKnots = Length@Knots;
   NBasis = NKnots - degree - 1;
   X = Table[
     Evaluate @ BSplineBasis[{degree, Knots}, n, t] // N, {t, 1, M}, 
       {n, 0, NBasis - 1}];
   Dsq = Differences[X, 2];
   a=Inverse[Transpose[X].X + lambda*Transpose[Dsq].Dsq // N].Transpose[X].data // N;

When I try to place a knot in every point in my data, numerical errors arise, such as:

Inverse::luc: Result for Inverse of badly conditioned matrix {{1.251,-0.1255,-0.251,0.0836667,0.0418333,0.,0.,0.,0.,0.,<<72>>},<<9>>,<<72>>} may contain significant numerical errors. >>

Obviously, the corresponding result is wrong (I can see it from the plot). It seems that the matrix to be inverted is ill-conditioned:

a = Inverse[Transpose[X].X + lambda*Transpose[Dsq].Dsq // N].Transpose[X].data // N;

but now comes the other problem. I use equidistant knots (let's say with 7 points distance) to overcome this problem. But then sometimes the algorithm works with:

Knots = Flatten @ {Table[1, {i, 1, degree}], knots, Table[M, {i, 1, degree}]};

and some other times works with

Knots = Flatten @ {Table[1, {i, 0, degree}], knots, Table[M, {i ,0, degree}]};

Now, I think that there is some kind of problem in BSplineBasis function.

Q: Can you spot the problem please? Or has anyone of you implemented a simillar function in the past with BSplineBasis function?

share|improve this question
Always avoid calling Inverse when you can use LinearSolve. Solving a linear system is much faster and stabler than calculating an inverse. –  ssch Sep 29 '13 at 16:47
have you seen this?… –  belisarius is forth Sep 29 '13 at 17:00
Thank you very much ssch. The algorithm is more stable now. But I am still in doubt for the duplication of the first and the last point as knots. Anyway, you helped me a lot. –  jojosthegreat Sep 29 '13 at 17:09
Actually the problem persists: LinearSolve::luc: "Result for ... of badly conditioned matrix may contain significant numerical errors. –  jojosthegreat Sep 29 '13 at 17:21
Dear Belissarius thank you for your response. All these posts state that we must duplicate the external knots d times, where d is the degree of the spline. I know that fact, but why my second problem persists? Anyway, this correction seems to cure many instabilities, but not all: a = LinearSolve[ Transpose[X].X + lambda*Transpose[Dsq]. Dsq, Transpose[X].data, Method -> "Krylov"] // N; –  jojosthegreat Sep 29 '13 at 17:51

5 Answers 5

I'm not sure if this addesses all of the issues you are having but here is an implementation I put together some time ago that allows us to use LinearModelFit and BSplineBasis to do spline regression.

The benefit of this approach is that all of the properties of FittedModel are immediately available to us. This allows for checking for fit, residual diagnostics etc.

SplineModel[data_, deg_, knots_] := 
  Block[{basis, allKnots}, 
   basis = 
    Array[\[FormalX]^# &, deg + 1, 0]~Join~
     Table[BSplineBasis[{deg, knots}, i, \[FormalX]], 
        {i, 0, Length[knots] - deg - 2}];

   LinearModelFit[data, basis, \[FormalX]]

Lets generate some interesting data...

SeedRandom[249304]; data = 
   RiemannSiegelZ[i] + Sin[i] + 
    RandomReal[NormalDistribution[0, .2]]}, {i, 0, 25, .05}];

And now we pick some knots and smooth the data using cubic splines.

knots = Range[0, 25, 1];
mod = SplineModel[data, 3, knots];

Show[ListPlot[data], Plot[mod[x], {x, 0, 25}, PlotStyle -> Directive[Red, Thick]]]

enter image description here

share|improve this answer
Thank you Andy. It seems that my only problem is the ill conditioning. I tried your approach and everything works fine, except from the absence of the smoothing parameter. Now, I think that in my approach, the term lambda*Transpose[Dsq].Dsq , witch is a faster alternative for roughness penalty, causes all the trouble. –  jojosthegreat Sep 30 '13 at 20:53
Dear Andy, I've noticed that you are using as basis the terms 1,x,...x^degree and also the BSplineBasis function. Namely, you are using the B-splines and some functions of the truncated power basis. Is that approach correct? I am asking you because I've never seen this before. –  jojosthegreat Oct 1 '13 at 7:21
What I have found is that adding these has the effect of minimizing edge effects. Feel free to remove these extra basis functions but also keep in mind that there is nothing inherently wrong with adding any basis functions we wish. –  Andy Ross Oct 1 '13 at 13:33
Hmm, where are n kmin and kmax used inside of SplineModel? –  Ajasja Oct 16 '14 at 12:27
@Ajasja oops, must have been copy/paste. I'm surprised no one caught that before now. I suspect the reason it was there in the first place was because my original code picked the knots automatically rather than using pre-set ones. –  Andy Ross Oct 16 '14 at 13:28

I have long been looking for a good implementation of Cubic Spline Smoothing with adjustable roughness penalty parameter for Mathematica. Your module gave me enough hints to understand how to make this work in Mathematica so I basically made a Cubic spline smoothing from your code with minor adjustments (about knots, a little bit about performance)

CubicSplSmooth[data_, lambda_] := 
  Module[{M, Knots, X, Dsq, a},
          M = Length @ data;
          Knots = Flatten@{ 1, 1, 1, Range @ M, M, M, M};
          X = Table[ Evaluate @ N @ BSplineBasis[{3, Knots}, n, t], 
                     {t, 1, M}, {n, 0, M + 1}];
          Dsq = Differences[X, 2];
          a = LinearSolve[ Transpose[X].X + lambda*Transpose[Dsq].Dsq, 
                           Transpose[X].data, Method -> "Multifrontal"];

This is restricted to Cubic degree but can be generalized to arbitrary degree as in your example. Manipulate is a nice way to get a feeling for the performance by moving the slider around:

    smoothdata = CubicSplSmooth[data, 10^lambda];
    Show[ ListPlot[ data, PlotRange -> {-5, 3}], 
          ListLinePlot[ smoothdata, Mesh -> All, PlotStyle -> Red]],
    {{lambda, 0}, -5, 5}]

The smoother behaves very naturally, yielding the original data for low (close to 0) values of lambda and a linear fit to data for extremely high ones.

If comparing this to the performance Labview achieves with the Cubic Spline Fit VI, it is still slower especially for large datasets. But the source of that is not accessible I think. Anyway it works well up to now but I think that performance can be surely improved.

share|improve this answer
Welcome to mathematica.SE and thanks for sharing :) –  ssch Oct 27 '13 at 13:56
The speed is the last thing that can bother us. We can use the Compile function, so the code will be transformed in the background into C code. And if Mathematica can reach gcc, then the result is really fast. –  jojosthegreat Oct 29 '13 at 11:48
Also I've found that LeastSquares is a faster and more reliable command for matrix inversion problems, so it is preferred than LinearSolve for arbitrary data. –  jojosthegreat Oct 29 '13 at 11:51
I think that the problem is the B-Spline functions themselves. They have the advantage to transform data in such way that all the math from least squares fitting can be applied without problem. But they still have problems with the stability and they aren't well documented. I am planning to use the "old school" approach from the book: Pollock, Handbook of Time Series Analysis, Signal Processing, and Dynamics. –  jojosthegreat Oct 29 '13 at 12:57
@Tobi In the line: X = Table[ Evaluate @ N @ BSplineBasis[{3, Knots}, n, t],{t, 1, M}, {n, 0, M + 1}]; n & t have no definition. Was there a reason for these or is this a typo? –  R Hall Jan 5 '14 at 0:00

Here, I will give a Global B-spline Curve Fitting according to the least-squares method.


Assume that $p\geq 1, n \geq p$, and $Q_0 , ... , Q_m (m > n)$ are given. We seek a $p$-th degree non-rational curve $$C(u)=\sum_{i=0}^{n}N_{i,p}(u)P_i \quad u \in[0,1] $$

satisfying that:

  • $Q_0=C(0), Q_m=C(1)$
  • the remaining $Q_k$ are approximated in the least squares sense, i.e. $$\sum_{k=1}^{m-1}||Q_k-C(u_k)||^2$$ is a minimum with respect to the $n+ 1$ variables,where, $P_i, \{u_k\}$ are the pre-computed parameter values.(About the choose of parameter values, please see here)


$R_k=Q_k-N_{0,p}(u)Q_0-N_{n,p}(u)Q_m \quad k=1,2,\cdots m-1$

enter image description here


where $N$ is the $(m- 1) \times (n- 1)$ matrix of scalars

$ N= \begin{pmatrix} N_{1,p}(u_1) && N_{2,p}(u_1) && \cdots && N_{n-1,p}(u_1)\\ \vdots && \vdots && \vdots && \vdots \\ N_{1,p}(u_{m-1}) && N_{2,p}(u_{m-1}) && \cdots && N_{n-1,p}(u_{m-1})\\ \end{pmatrix} $

$R$ is the vector of $n - 1$ points

$ R= \begin{pmatrix} N_{1,p}(u_1)R_1 + \cdots + N_{1,p}(u_{m-1})R_{m-1} \\ \vdots \\ N_{n-1,p}(u_1)R_1 + \cdots + N_{n-1,p}(u_{m-1})R_{m-1} \\ \end{pmatrix} $


$ P= \begin{pmatrix} P_1\\ \vdots\\ P_{n-1} \end{pmatrix} $

enter image description here


L. Piegl and W. Tiller, The NURBS Book, 2nd ed., Berlin: Springer-Verlag, 1997 pp. 410–413.


Parametrization is an option that specifies which method is used to solve the parameter values.

ControlPointsNumber is an option that specifies the number of control points that genereating the B-spline curve.

SplineDegree is an option that specifies the degree of the B-spline curve.

Options[BSplineCurveFit] =
 {Parametrization -> Automatic,
  ControlPointsNumber -> Automatic, SplineDegree -> 3};

 pts : {{_, _} ..}, 
 opts : OptionsPattern[{BSplineCurveFit}]] /;
MatrixQ[pts, NumericQ] :=
 Module[{m, cpn, pz, sd, paras, knots, coeffMat, R, ctrlpts},
  m = Length@pts - 1;
  (*achieve the value of options*)
  cpn = OptionValue[ControlPointsNumber] /. 
         Automatic -> Min[5, m + 1] /.
          n_Integer :> (m + 1) /; n > (m + 1);
  pz = OptionValue[Parametrization];
  sd = OptionValue[SplineDegree] /. Automatic -> Min[3, cpn];
  paras = calcParas[pts, pz];
  (*calculate the knots*)
  knots = calcFitKnots[{m, cpn - 1}, sd, paras];
  (*calculate the coefficients of matrix*)
  coeffMat =
     With[{i = searchSpan[knots, u0]},
        ConstantArray[0, i - sd],
        BSplineBasis[{sd, knots}, #, u0] & /@ Range[i - sd, i],
        ConstantArray[0, cpn - 1 - i]]]] /@ ArrayPad[paras, -1])[[All, 2 ;; -2]];
  (*solve the control points of the B-Spline curve*)
  R = Transpose[coeffMat].(pts[[2 ;; -2]] -
       With[{Q0 = First@pts, Qm = Last@pts},
        BSplineBasis[{sd, knots}, 0, #] Q0 +
         BSplineBasis[{sd, knots}, cpn - 1, #] Qm & /@ 
          ArrayPad[paras, -1]]);
  ctrlpts =
    LinearSolve[Transpose@coeffMat.coeffMat, R], {Last@pts}];
  (*visualize the fitting result*)
  {BSplineCurve[ctrlpts, SplineDegree -> sd, SplineKnots -> knots],
   Blue, PointSize[Small], Point[pts]},
   AspectRatio -> 1/GoldenRatio]


searchSpan[knots_, u0_] :=
 With[{max = Max[knots]},
  If[u0 == max, 
   Position[knots, max][[1, 1]] - 2,
   Ordering[UnitStep[u0 - knots], 1][[1]] - 2]

calcFitKnots[{m_, n_}, deg_, paras_] :=
 With[{d = (m + 1)/(n - deg + 1)},
  Join[ConstantArray[0, deg + 1],
   (*calculate the interior knots*)
    Module[{i, alpha},
     i = Floor[j*d];
     alpha = j*d - i;
     (1 - alpha) paras[[i]] + alpha paras[[i + 1]]]] /@ 
      Range[1, n - deg],
   ConstantArray[1, deg + 1]]

calcParas[pts_, type_] :=
   type === Automatic || type === "ChordLength",
   FoldList[Plus, 0, Normalize[(Norm /@ Differences[pts]), Total]] // N,
   type === "Centripetal",
   FoldList[Plus, 0, 
    Normalize[(Norm /@ Differences[pts])^(1/2), Total]] // N,
   type === "EqualSpaced", 
   Range[0, 1, 1/(Length@pts - 1)] // N


data = 
  RiemannSiegelZ[i] + Sin[i] + 
  RandomReal[NormalDistribution[0, .2]]}, {i, 0, 25, .05}];

BSplineCurveFit[data, ControlPointsNumber -> 20]

enter image description here


Comparison with different Control Points Number, Parametrization and Spline degree

BSplineCurveFit[data, ControlPointsNumber -> #] & /@ {10, 20, 30, 40} //
 Partition[#, 2] & // Grid

enter image description here

BSplineCurveFit[data, Parametrization -> #, 
 ControlPointsNumber -> 20] & /@  {"ChordLength", "Centripetal", "EqualSpaced"}

enter image description here

BSplineCurveFit[data, SplineDegree -> #, 
 ControlPointsNumber -> 20] & /@ {1, 3, 5, 6} // Partition[#, 2] & // Grid

enter image description here

share|improve this answer

Here is a (simplified) implementation of Reinsch's smoothing spline, which is effectively equivalent to csaps() in MATLAB's Curve Fitting Toolbox. Fancier methods have come along since then (e.g. Wahba's cross-validation splines), but this old workhorse has still proved serviceable:

SmoothingSplineFunction[dat_?MatrixQ, p : (_?NumericQ | Automatic) : Automatic] :=
 Module[{n = Length[dat], pv = p, cc, dc, del, h, qg, qm, rh, tm, uv, xa, ya},
        {xa, ya} = Transpose[dat]; h = Differences[xa]; rh = 1/h; 
        del = Differences[ya] rh;
        qm = SparseArray[{Band[{1, 1}] -> Most[rh], 
                          Band[{1, 2}] -> -ListCorrelate[{1, 1}, rh], 
                          Band[{1, 3}] -> Rest[rh]}, {n - 2, n}];
        tm = SparseArray[{Band[{2, 1}] -> Most[Rest[h]],
                          Band[{1, 1}] -> ListCorrelate[{2, 2}, h],
                          Band[{1, 2}] -> Drop[h, -2]}, {n - 2, n - 2}];
        qg = qm.Transpose[qm];
        If[pv === Automatic, pv = 1/(1 + Tr[tm]/(6 Tr[qg]))];
        uv = LinearSolve[6 (1 - pv) qg + pv tm, Differences[del]];
        dc =
        ya - 6 (1 - pv) Differences[ArrayPad[Differences[ArrayPad[uv, 1]]/h, 1]];
        Interpolation[Transpose[{List /@ xa, dc, Append[Differences[dc]/h -
                                 h ListCorrelate[{2, 1}, ArrayPad[pv uv, 1]],
                                 pv Last[uv] Last[h] -
                                 (Subtract @@ Take[dc, -2])/Last[h]]}],
                      InterpolationOrder -> 3, Method -> "Hermite"]]

(It might be a bit confusing that the output is an InterpolatingFunction[] even though no interpolation is being done; it's just that I wanted to use the built-in facility for evaluating piecewise Hermite cubics.)

Let's use it on Andy's example:

            data = Table[{i, RiemannSiegelZ[i] + Sin[i] +
                          RandomVariate[NormalDistribution[0, .2]]},
                         {i, 0, 25, 0.05}]]
smth = SmoothingSplineFunction[data, 9/10];
Plot[smth[x], {x, 0, 25}, PlotStyle -> Directive[Thick, Red], 
     Prolog -> {Blue, AbsolutePointSize[5], Point[data]}]

Reinsch smoothing on Andy's example

There is an ad hoc element to choosing the "smoothing parameter" in the second argument, however. There have been a number of proposals on how to pick the best smoothing parameter, but I haven't gotten around to evaluating them, as I've mostly used smoothing splines only for looking at the approximate trend.

share|improve this answer

Anton Antonov has implemented smoothing splines in his Quantile regression with B-splines package (direct link to the M-file). This post (duplicated in this thread) and this WTC2014 talk explain how can it be used. See also this post of mine for an example of use.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.