Search code examples
c++cmodelingcurve-fittinggsl

Fitting a piecewise "broken stick" model with GSL


I am attempting to fit a function to some sampled data I have. I understand and have seen numerous examples of linear fitting within GSL, however the function I need to fit has the form

x_i = c_0 + c_1*t_i

for i < some arbitrary number (T)

x_i = c_0 + c_1*t_i + c_2*(T-t_i)

for i >= T.

To visualise this, it's a line (which has finite length) with a break in it at some point dictated by T. I've been trying to figure out how to implement this in GSL, but I can't figure out the syntax of fitting to two equations, and fitting both equations over different ranges of values. My math isn't as strong as it should be, and the GSL documentation is a little dense.

Alternatively, if GSL is unable to do this, are there any other C/C++ libraries capable of it?

edit: I think this actually might be equivalent to fitting the first half of the broken stick in the normal linear way, and then fitting the second half (with known c_0 and c_1, and fitting for c_2). I still don't know how to do that either, but I think it might be acceptable for what I'm doing if this is a possible solution. However, I would still like some method of locating the best T to break the stick at, but this isn't absolutely necessary.


Solution

  • I solved it!

    Turns out it's as simple as creating a smoothing b-spline, with two dimensions (i.e., 3 knots), which I believe GSL still does within linear time.