cjekel / piecewise_linear_fit_py

fit piecewise linear data for a specified number of line segments
MIT License
289 stars 59 forks source link

Add Constraint #26

Closed adussarps closed 5 years ago

adussarps commented 5 years ago

Add an option to constraint linear coefficients to a range of values (e.g >0 ).

cjekel commented 5 years ago

I could potentially create a constrained fit where each breakpoint is subjected to per-determined constraints. The following code sets up this constraint lower and upper limits, just as an example.

# for two breakpoints; arbitrary example 
# 0.1 <= breakpoint[0] <= 0.15
# 0.5 <= breakpoint[1] <= 0.7
g = np.zeros((2, 2)  # constraints
g[0, 0] = 0.1
g[0, 1] = 0.15
g[1, 0] = 0.5
g[1, 1] = 0.7

Then the optimization would find the best breakpoint subject to g.

Do I understand this correctly?

cjekel commented 5 years ago

For now this could be done using a custom optimizer like https://jekel.me/piecewise_linear_fit_py/examples.html#use-custom-optimization-routine where you manually specify the bounds.

I'll try to include an optional bounds parameter in the fitting routines for this case.

cjekel commented 5 years ago

@adussarps I'm not sure if I understand your request to constrain linear coefficients, but I added an option to constrain the search space for breakpoints. Does this example solver your issue? or are you looking to add constraints for each slope (and the change from the previous slope)?

adussarps commented 5 years ago

Yes it's fine thanks!!