1

When doing (ad hoc example)

from scipy import optimize def fit_func(x, a, b): return a*x + b optimize.curve_fit(fit_func, x_data, y_data) 

how can I put bounds like a>b? I know the option bounds, but it appears it does accept only explicit numbers.

Maybe some if in the definition of fit_func?

2 Answers 2

2

You can try with a workaround, instead of defining the function as:

def fit_func1(x, a, b): return a*x + b 

with constraint a>b, you can do:

def fit_func2(x, this_much_a_is_bigger_than_b, b): return (a+this_much_a_is_bigger_than_b)*x + b 

with constraint this_much_a_is_bigger_than_b > 0, 0 being an explicit number, and fit_func2 function is equivalent to the fit_func1 from a mathematical perspective.

Then you can have:

a = b + this_much_a_is_bigger_than_b b = b 
Sign up to request clarification or add additional context in comments.

Comments

2

Curce_fit only supports box constraints (via least_squares). There are workarounds, but I'd rather change variables to e.g a and b-a

1 Comment

or no constraints at all by b --> b and a --> b + a**2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.