5
$\begingroup$

Context

In the context of this (vaguely controversial) question, I am stumbling into a performance issue with Fit.

Let me illustrate on fitting a set of BSplines to set of 64 data points:

dat = Table[{i, Sin[i^2]}, {i, 0, 2 Pi, Pi/32}] // N;
fspl = Interpolation[dat, Method -> "Spline"];
fb = First[Cases[fspl, _BSplineFunction, ∞]];
{sd, scpts, sk} = fb /@ {"Degree", "ControlPoints", "Knots"};
func = Flatten[Outer[Times, Sequence @@ MapThread[
Table[BSplineBasis[{#1, #2},k-1, #3], {k, #4}] &, {sd, sk, {x}, Dimensions[scpts]}]]];
fit = Fit[dat, func,x, FitRegularization -> {"Curvature", 1.}]; // Timing

(* 0.008626 *)

Plot[fit, {x, 0, 2 Pi}]

enter image description here

But if I now ask say for 1024 points

dat = Table[{i, Sin[i^2]}, {i, 0, 2 Pi, Pi/512}] // N;
fspl = Interpolation[dat, Method -> "Spline"];
fb = First[Cases[fspl, _BSplineFunction, ∞]];
{sd, scpts, sk} = fb /@ {"Degree", "ControlPoints", "Knots"};
func = Flatten[Outer[Times, Sequence @@ MapThread[
Table[BSplineBasis[{#1, #2}, k-1, #3], {k, #4}] &, {sd, sk, {x}, Dimensions[scpts]}]]];
fit = Fit[dat, func,x, FitRegularization -> {"Curvature", 1.}]; // Timing

(* 5.83293 *)

It takes dramatically much longer! (and it get much worse with higher sampling)

Question

Is is possible to improve the performance of Fit on this specific problem?

Thank you for your help.

I am aware that I could ask for less BSplines but...

$\endgroup$
2
  • 1
    $\begingroup$ How close a fit do you need? Suppose the root mean square error is a satisfactory measure of goodness-of-it. With SeedRandom[12345], 1024 gets you an RMSE of 0.000093, 512 points get you 0.00068, 256 points gets you 0.0049, 128 points gets you 0.039, and 64 points gets you 0.231. 64 points is almost certainly an inadequate number of points. But what is adequate? Is it "I'll know it when I see it" ? $\endgroup$ – JimB Apr 8 '20 at 16:49
  • $\begingroup$ @JimB sorry for the ambiguity. I meant to say it takes forever. The fit seems good enough. $\endgroup$ – chris Apr 8 '20 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.