Statalist


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

st: Using for within Mata optimize to loop over observations


From   "Glenn Goldsmith" <glenn.goldsmith@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Using for within Mata optimize to loop over observations
Date   Mon, 16 Feb 2009 23:12:41 -0000

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/



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