acerbilab / pybads

PyBADS: Bayesian Adaptive Direct Search optimization algorithm for model fitting in Python
https://acerbilab.github.io/pybads/
BSD 3-Clause "New" or "Revised" License
69 stars 2 forks source link

My objective function required additional parameters/data #33

Closed anuj-sp closed 1 year ago

anuj-sp commented 1 year ago

My objective function requires additional parameters and data to be passed to it. The MATLAB implementation of BADS allows you to pass this data as the last argument when running BADS. Would a similar functionality be made available for pyBADS, where I can pass in the additional arguments when initializing BADS?

lacerbi commented 1 year ago

Thanks for your question!

It might be a nice-to-have; perhaps we could add it in the future if there is any actual need. In practice we didn't even think of it because one can very easily work around this. If you have a specific usage case in mind in which this is not "very easily" done, please let us know!

In practice, you can easily handle this with either a lambda function in Python or just defining the function you need to pass to PyBADS. This is exactly the same as in MATLAB, where you could handle this easily with a function handle / anonymous function. For example, in Python:

data = None # define your data
extra_params = None # define your function-specific parameters

def fun_for_pybads(x):
    return fun(x, data, extra_params)

# Pass fun_for_pybads to PyBADS

where fun is the function you want to optimize, which takes as first argument the function parameters to be optimized, and then extra data and extra (fixed) parameters. Note that fun_for_pybads only depends on x now, data and extra_params are given in the outer scope.