'How to fit a multi steps function lke this one with Python?
I have a curve that shows a multi-step feature, in each step, I guess it can be fitted by a linear function or a higher-order polynomial. The figure is shown here.
I think it is like one flat line connecting with a steap line and continues. I have found some posts to fit the step-like function. But unfortunately, all I can find is for functions only with one step. I have tried to extend them to my case, but I failed. Could you please tell me how can I fit my curves like this kind?
And also my curves will turn to show fewer features of clear steps, shown like this,
If the method could work them both, that would be much greater.
Any help would be much appreciated!
Solution 1:[1]
First of all, you need to know how many discontinuities you have and where they are. You can get them by smoothing the data with a median filter (preserves discontinuities) and check for the peaks in the differences. Once you got the discontinuities, you can either fit a multi-step function, or just loop through the segments and fit individually.
Here a list of helpful functions:
fitting: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
median filter: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.medfilt.html
find locations of maxima: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelmax.html
Edit: you could use a one-sided maximum filter instead of the median filter to make the function monotonic and then search for maxima in the differences. You could also do this with a threshold.
Also helpful for those analysis is the pandas library using pandas.Series.rolling: https://pandas.pydata.org/docs/reference/api/pandas.Series.rolling.html#pandas.Series.rolling
Edit: note, that the problem with multi step function fitting is the degeneracy of the solution. Each step introduces two additional parameters (location and step hight), but any permutation would lead to the same solution. Another special case happens, when two step fall into the same gap, then the steps can arbitrarily share the gap. These cases give the optimizer a hard time. You could add a constraint to the steps to sort them (xstep1 < xstep2 < ...), but this adds unnecessary complexity in my opinion. Therefore better find the gaps and fit the segments individually.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
