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

 From Bob Hammond To statalist@hsphsun2.harvard.edu Subject Re: st: Using for within Mata optimize to loop over observations Date Mon, 16 Feb 2009 21:20:50 -0500

```Glenn,

```
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.
```
Bob

Glenn Goldsmith wrote:
```
```Bob,

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.
```E.g.

```
************************************************* 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)
```  st_store(i,"x2",x)
}
```
*************************************************
```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.

Glenn.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```
--
------------------------------------------------------------------------
Bob Hammond
Department of Economics
North Carolina State University
Office: (919) 513-2871
Fax: (919) 515-7873
http://www4.ncsu.edu/~rghammon/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```