piecewise_linear_fit_py icon indicating copy to clipboard operation
piecewise_linear_fit_py copied to clipboard

Force positive slopes?

Open marvinseifert opened this issue 1 year ago • 2 comments

First, thanks for this amazing project. I was just wondering if it is at all possible to force the model to only use positive slope values. For example, I have values for which I know that the trend must be positive, it must never be negative. Yet, sometimes the model fits a segment which has an ever so slight negative slope. I could obviously just set that slope to 0, but it remains a fact that the best piecewise fit could have been different if every slope must be positive. I was just wondering if there is an easy way to force the slopes to be positive. I am using pwlf.PiecewiseLinFit(x, y) and fitfast() function.

Thank you

marvinseifert avatar Mar 12 '25 14:03 marvinseifert

I second this question, can the final function be made strictly non decreasing somehow ? Or perhaps @marvinseifert, have you found any other solution to your question ?

mat-ej avatar Mar 31 '25 11:03 mat-ej

So for monotonic fits, I've typically referred to pwlfit

I don't have a clean way to do this in my routine, since I normally rely on least squares fits.

However, I wonder if there is a good way to formulate an inequality constrained least squares problem to get monotonic and/or strictly positive piecewise linear fits. I already do constrained least squares to force the fit to go through a point.

Non negative least squares comes up as a potential solution for this https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.nnls.html

cjekel avatar May 03 '25 19:05 cjekel