Group Abstract Group Abstract

Message Boards Message Boards

Can I embed an inner loop (rather long) in a function that I can then optimize numerically?

Posted 14 days ago

Hello!
I'm not an expert in Mathematica.

I've tried brute-force inner loop-outer loop grid search for an optimization exercise for a rather rich problem: the inner loop solves a problem that involves solving a number of smaller problems for a (potentially large) sample and performing operations on the results of said smaller problems.

I would like to automatize the outer loop for it to use numerical optimization (eg NMinimize) to find a global optimum. For that, I need NMinimize (or alternative) to work with an objective function that calls the inner loop and gets, as return, the scalar value of the objective function for a given set of values for the choice variables.

I'm not sure about the right/best way to do this. I've tried the attached code below for a much simpler inner loop and it does work. I will try to now embed the much longer inner loop of the full problem following this idea but any ideas to improve upon it would be much appreciated. Thanks very much for your attention!

POSTED BY: Paco Martín
5 Replies
Posted 13 days ago

There are so many symbols that are undefined here that I cannot give you a definitive answer (I'm missing, es, s, emax, astar, and NN). I'm assuming that you don't want to provide all of your code because you think it's too complicated, but you either need to provide enough code that I can at least run it, or you need to pare it down to a minimal example that demonstrates the problem.

Also, can you please explain what type of error you're seeing? I don't even know what you think is going wrong. Do you get error messages (and if so, what are they)? Are you getting invalid results?

The place where all of the undefined symbols are is the Which expression. So, I'm guessing that the problem is that one of the conditions in Which is not evaluating to True or False. If a Which encounters a condition that doesn't evaluate to True or False, then it just quits and returns the whole Which expression. For example,

a = "not a number";
Which[
 a > 7, "something",
 True, "something else"]

This will return the full Which unevaluated, because it cannot resolve a > 7 to a truth value.

But that's just a guess.

POSTED BY: Eric Rimbey
Posted 13 days ago

Thanks so much again Eric.

The inner loop solves a general equilibrium model. I will use the code to both solve the optimization problem of a planner and to calibrate the model.

I did not know any way to do what I'm trying to do (embed the inner loop in a function and then numerically optimize with NMinimize).

The way I'm trying, which worked with the simple inner loop in the example in my initial post, does not work in a slightly richer inner loop (the loop to solve the full GE model is much longer). I paste to this post the new code with the slightly richer inner loop. My question at this point is: what am I doing wrong in the new code?

Note the inner loop as written works perfectly well outside the function SolveInnerLoop in another notebook, but I get the output you see in the attached document (that the objective function is not scalar-valued).

Attachments:
POSTED BY: Paco Martín
Posted 14 days ago

Well, all I can suggest is that you find a way to ask your question more concretely. There's just no way for me to guess what could be going wrong. I'd need to see what you're trying to do. I'm honestly not even sure how to interpret your question. By "a more complex inner loop" do you mean a different version of SolveInnerLoop, or is there something else that you haven't shown us yet?

Are you thinking that there's some general strategy that we can just tell you that you can then apply that somehow avoids whatever difficulties you're having? If that's what you're asking for, it seems to me that you're on the right track--I can't think of some general strategy that's always superior to what you're doing. But again, if I knew more about your real context, maybe I could identify some particular opportunity.

POSTED BY: Eric Rimbey
Posted 14 days ago

Thanks a lot for your answer. I just would like to learn a way to do this. The simple one I posted works but I'm not succeeding in embedding a more complex inner loop. (Thanks also for the suggestion on SolveInnerLoop function)

POSTED BY: Paco Martín
Posted 14 days ago

"What is the best way?" is a very difficult question to answer. What are your objectives? Are you running into performance problems? Do you want to generalize? I really don't think there's any way to answer your question without a lot of guesswork.

As for feedback on your Mathematica code, I think your SolveInnerLoop function could be simplified to this:

SolveInnerLoop[S_, \[CapitalGamma]m_, \[Beta]_] :=
 Sum[Log[astar[s[[1, n, 1]], \[Beta], s[[1, n, 2]], \[CapitalGamma]m]], {n, 10}]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard