I am just trying to learn the optimize() function, but cannot find a way to get things going. Let us assume that have
a = ?
b = ?
I have 10 observations and want to find the values of a and b for each observation. Presently, the code uses the equation 1 value of 174 and equation 2 of 22 manually inserted into the Mata code, which produce a = 10, b = 12; but how can I automate the process so that the Stata data is read into the Mata code and get back the results of a and b from the Mata code into Stata.
So the optimize function that I have is given below:
a = ?
b = ?
I have 10 observations and want to find the values of a and b for each observation. Presently, the code uses the equation 1 value of 174 and equation 2 of 22 manually inserted into the Mata code, which produce a = 10, b = 12; but how can I automate the process so that the Stata data is read into the Mata code and get back the results of a and b from the Mata code into Stata.
Code:
clear input float(eq1 eq2) 174 22 60 14 252 24 . . . . . . . . . . . . . . end
Code:
clear mata
mata
void mysolver(todo, p, lnf, S, H)
{
a = p[1]
b = p[2]
lnf = ((b^2)+(3*a)-174)^2\
(a+b-22)^2
}
S = optimize_init()
optimize_init_evaluator(S, &mysolver())
optimize_init_evaluatortype(S, "v0")
optimize_init_params(S, (1,1))
optimize_init_which(S, "min" )
optimize_init_tracelevel(S,"none")
optimize_init_conv_ptol(S, 1e-16)
optimize_init_conv_vtol(S, 1e-16)
p = optimize(S)
p
1 2
+-----------+
1 | 10 12 |
+-----------+
end

Comment