ICB-DCM / pyPESTO

python Parameter EStimation TOolbox
https://pypesto.readthedocs.io
BSD 3-Clause "New" or "Revised" License
207 stars 45 forks source link

Optimization with conditions specific for observables fails #1375

Closed arrjon closed 3 months ago

arrjon commented 3 months ago

Bug description Hierachical optimization with multipe conditions fails in the specific case if one has multiple conditions and observables specific to that condition. Amici returns zeros for those observables which are not seen in a condition and it seems like this is not correctly handled within pyPESTO.

These error occur

pypesto/hierarchical/relative/util.py:458: RuntimeWarning: divide by zero encountered in log
  0.5 * np.nansum(np.log(2 * np.pi * sigma_i**2))
pypesto/hierarchical/inner_calculator_collector.py:558: RuntimeWarning: divide by zero encountered in log
  np.log(2 * np.pi * sigma_i**2) + (data_i - sim_i) ** 2 / sigma_i**2

and not even one single start fails or stops. It just runs forever. I debugged the nllh value and it is always -inf.

To reproduce I attached a minimal petab problem to recreate the issue. test.zip

It is a minimal parameter estimation problem with two conditions. The model produces an observable y which is then mapped via an observable mapping to either obs_y_1 = scaling_obs_y_1 * y + offset_obs_y_1 or obs_y_2 = scaling_obs_y_2 * y + offset_obs_y_2. Measurements for obs_y_1 are only given by under the condition condition1 and obs_y_2 only for condition2.

Environment

arrjon commented 3 months ago

That the nllh is inf seems also to be the case if one disables the hierachical optimization (this is also true for the older pypesto verisions 0.4.1 and 0.4.2) @PaulJonasJost

arrjon commented 3 months ago

The problem lies in the observable mapping: parameters which are replaced by parameters from the petab measurement file cannot change with the condition. If you define them explicitly and replace them using the petab condition file, it works. However, then hierachical optimization is not supported anymore.

However, the behaviour of pypesto is very strange in that case and I would expect failure at some point since the opimizations only returns inf, but continues anyway.

Doresic commented 3 months ago

The problem lies in the observable mapping: parameters which are replaced by parameters from the petab measurement file cannot change with the condition.

To confirm, we showed this is not true, right?

The issue with hierarchical optimization was coming up due to some non-intended logarithms of 0, look at #1377

In the case of non-hierarchical optimization, the issue turned out to be not-ideal parameter bounds for the sigma parameters: the lower bound was set to 0, and it was possible to fit very well, so the optimizer pushed the noise parameter to exactly 0 which caused problems.

So this issue should be resolved with #1377

arrjon commented 3 months ago

Indeed, #1377 solves the problems! Thank you!