[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using for within Mata optimize to loop over observations

From   Bob Hammond <>
Subject   Re: st: Using for within Mata optimize to loop over observations
Date   Mon, 16 Feb 2009 21:20:50 -0500


That simple fix handled the problem. Thank you very much. Also, I checked the FAQ and found that I should reply to say that the problem is solved:

"Don't walk away from the thread you started Continuing or closing a thread you started is important, especially by answering secondary questions and by reporting what solved your problem. You can then thank those who tried to help."

I would have thought that filling everyone's inbox when all that I have to say is "Thanks" would be frowned upon but I'm happy to express gratitude publicly rather than privately as I had been doing.


Glenn Goldsmith wrote:

To get you're existing code to work, you need to pass "i" to your -eval0()-
function as an argument, using -optimize_init_arguments()- (see -help
mf_optimize- for details). You'll also need to modify -eval0()- slightly to
take the argument.

************************************************* void eval0(todo, x, real scalar i, v, g, H) { z = st_data(i,"z")
  v = -(x-z)^2

for(i=1;i<=st_nobs();i++) {
  S = optimize_init()
  optimize_init_evaluator(S, &eval0())
  optimize_init_params(S, 0)
optimize_init_argument(S,1,i) x = optimize(S)
In theory you could also accomplish this sort of thing without using
explicit loops, by coding a v-type evaluator that optimizes for a
(row)vector x rather than a scalar. But that's likely to be a pretty
inefficient way of proceeding here.

Hope that's somewhat useful.


*   For searches and help try:

Bob Hammond
Department of Economics
North Carolina State University
Office: (919) 513-2871
Fax: (919) 515-7873
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index