This is both a conceptual question, and a practical one of what packages I could use to solve this.

### Problem statement

I am trying to solve the following problem. Let a \in \mathbb{R}^M be a vector of parameters, and p \in \mathbb{R}^N prices. *Market clearing* F: \mathbb{R}^M \times \mathbb{R}^N \to \mathbb{R}^N requires that

where F is differentiable in both arguments (both in theory and practice). Take a leap of faith and assume that for each a, there is a unique p(a). I am minimizing some function M of a, p, and data d, ie

Currently, I have explored two options.

### Inner loop rootfinding

For each a, find p(a) st F(a, p) = 0, eg via NLsolve.jl, then plug the objective M(a, p(a), d) into a solver like NLopt.jl or Optim.jl. This works, but is somewhat expensive and differentiation is tricky (but doable).

### Penalty

Optimize M(a, p, d) + \lambda \| F(a, p) \|^2_2 or similar in (a, p). The idea is that the market-clearing p will also be optimal, so F(a, p) = 0 anyway. But in practice, this does not always work, I get stuck in local optima with F \ne 0.

### Penalty + renormalization

Run the optimizer above for a bit, then if F(a, p) gets â€ślargeâ€ť, reach for the rootfinder.

Suggestions are welcome. I can make an MWE but the actual problem is about 20k LOC so it is not possible to consense all the quirks (in particular, nonlinearity and occasional ill-conditioning) into a simple example.

In particular, I am wondering if there is a systematic way of following the implicit p via homotopy.