Nested optimization

I have a model from which I estimate some parameters, and the parameters are “data” to my second model. I want to be able to optimize the parameters of the first model in a way that the second model fits well. Since optimize just takes functions (cost functions) I cannot write both optimizations right after another in one function. Is there any specific package in Julia for this kind of optimization?

function DEmodel()

function DEsolve()

function second_model()
... [it is just a curve fit, not a DE model]

function residuals_to_firstModel()
DEmodel() - data

r = optimize(residuals_to_firstModel, initial, LevenbergMarquardt())
parameters = r.minimizer

function residuals_to_secondModel()
second_model() - parameters
p  = optimize(residuals_to_secondModel, initial,  LevenbergMarquardt())

I don’t know how to make a loop so that the second model’s goodness of fit, feedbacks to the first model to adjust the parameters.

I appreciate any ideas!

Don’t use a loop. Optimize all of the parameters simultaneously. That is, minimize residuals_to_secondModel as a function of the parameters of both models.


Thank you