0

I have a complicated model function that is composed by 12 other functions. I have about 10 parameters. I use the model function both to fit data and to generate contour plots. Sometimes I am only interested in estimating only a few parameters, and fixing the rest, setting them as global variables. Other times I want to estimate most parameters, and fix only a few. Every time I change the set of which parameters to estimate and which to fit, I go through my 13 functions, changing all the calls to only include those parameters I want to estimate. Can this be done more dynamically? Perhaps also avoiding global variables?

2
  • 2
    Would you mind providing a minimal reproducible example? Commented Oct 16, 2022 at 10:22
  • Please provide enough code so others can better understand or reproduce the problem. Commented Oct 16, 2022 at 13:55

1 Answer 1

1

The partial function from the functools library can help you (here is the documentation).

For instance, one of your functions has the following form:

def f(x, a, b, c, d):
    # a very complex expression
    return ...

where a, b, c and d are the parameters to be optimized using curve_fit by SciPy.

Suppose that you need to optimize the parameters a and b and you want to keep c and d fixed. You can write:

# constant value
c0 = ...
# constant value
d0 = ...
f_partial = partial(f, c=c0, d=d0)

now you get a new function with a simplified signature and you can pass f_partial to curve_fit and only a and b will be optimized.

You can now leverage the partial function to choose which parameters to optimize.

This tutorial can also help you to understand better how the partial function works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.