Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
William Pratt <williamrpratt@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: Optimize for 2 unknowns looping over observations using Mata |

Date |
Mon, 5 Mar 2012 10:23:56 -0600 |

I am trying to optimize two unknowns while looping over observations. I receive the statements "too many )", "x not found" or "anything but for is expected where found", as this is my first transition to mata from stata, I would greatly appreciate any advice / help, my script is presented below, and the error reporting further down. I have referred to prior postings: - Using for within Mata optimize to loop over observations, http://www.stata.com/statalist/archive/2009-02/msg00668.html - Setting up optimize for 2 equations in 2 unknowns, http://www.stata.com/statalist/archive/2008-07/msg01027.html **time and sqrtT are constant **sige, Ix, divrate, rf, ve gen double a = . gen double b = . mata void bsmp0(todo, p, x, lnf, g, H) { a = p[1] b = p[2] x = st_data( i,("sige", "Ix", "divrate", "rf", "time", "ve", "sqrtT")) f1= a * exp(-x[3]*x[5])*normal(ln(a/x[2])+x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7])) f2= x[2]*exp(-x[4]*x[5])*normal(ln(a/x[2])+x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7])) f3= (1-exp(-x[3]*x[5]))*a-x[6] f4= ((a*b*exp(-x[3]*x[5]))/x[6])*normal(ln(a/x[2])+ x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7]))-x[1] lnf = (f1-f2)^2+(f3-f4)^2 } for(i=1;i<=st_nobs();i++) { S = optimize_init() optimize_init_evaluator(S, &bsmp0()) optimize_init_evaluatortype(s,"v0") optimize_init_params(S,(0,0)) optimize_init_argument(S,1,i) p = optimize(S) st_store(i,"va", a) st_store(i,"siga", b) } end After trying numerous combinations based on help, the mata manual and online guides, I am not sure how to proceed. Thanks for your consideration, William . mata ------------------------------------------------- mata (type end to exit) ---------------------------------------- : void bsmp0(todo, p, x, lnf, g, H) > { > a = p[1] > b = p[2] > x = st_data( i,("sige", "x", "divrate", "r", "time", "ve", "sqrtT")) > > f1= a * exp(-x[3]*x[5])*normal(ln(a/x[2])+x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7])) too many ')' r(3000); : : f2= x[2]*exp(-x[4]*x[5])*normal(ln(a/x[2])+x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7])) too many ')' r(3000); : : f3= (1-exp(-x[3]*x[5]))*a-x[6] <istmt>: 3499 x not found r(3499); : : f4= ((a*b*exp(-x[3]*x[5]))/x[6])*normal(ln(a/x[2])+ x[5]*(x[4]-x[3]+(b^2/2)))/(b*x[7]))-x[1] too many ')' r(3000); : : lnf = (f1-f2)^2+(f3-f4)^2 <istmt>: 3499 f1 not found r(3499); : : } expression invalid r(3000); : : for(i=1;i<=st_nobs();i++) { > S = optimize_init() > optimize_init_evaluator(S, &bsmp0()) > optimize_init_evaluatortype(s,"v0") > optimize_init_params(S,(0,0)) > optimize_init_argument(S,1,i) > p = optimize(S) > st_store(i,"va", a) > st_store(i,"siga", b) > } <istmt>: 3499 s not found r(3499); -- wrpratt@utpa.edu University of Texas-Pan American College of Business Administration Department of Finance and Economics South Texas Border Health Disparities Center o - 956.665.7937 f - 956.665.7310 * * 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/

**Follow-Ups**:**st: RE: Optimize for 2 unknowns looping over observations using Mata***From:*"Mak, Timothy" <timothy.mak07@imperial.ac.uk>

**st: RE: Optimize for 2 unknowns looping over observations using Mata***From:*Nick Cox <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: qnorm** - Next by Date:
**Re: st: RE: How to calculate standardized difference in means with survey weighted data?** - Previous by thread:
**st: Significance of the variance of GLLAMM random intercept** - Next by thread:
**st: RE: Optimize for 2 unknowns looping over observations using Mata** - Index(es):