# st: Using multiple equations with Optimize()

 From "Andrew Lynch" To Subject st: Using multiple equations with Optimize() Date Mon, 12 Nov 2007 08:11:25 -0600

Stata Users,

I have run into a bit of a dilemma with programing a Kalman filter with the optimize() procedure. I have the full documentation and have not been able to clarify where exactly the optimize() command looks when it optimizes. I am minimizing the process below. My goal is to minimize the Log Likelihood function, but while I have observed data yt and xt, a and at are unobserved and my goal is to generate them. To do so, I need Stata to rerun the do loop every time it updates one of the four parameters (b1, b2, q and h). I say all that to ask the question, "Does optimize() only look to the v= equation to optimize with the parameters or does it look to the entire evaluation and move through it in some ordered fashion (thereby honoring the do loop)?" I have been unable to get this to run, and I am unsure whether it is my dumb coding error at some point or just that optimize() won't do what I want it to do.

Any enlightenment would be greatly appreciated!

Thanks,

Andrew Lynch
PhD Student
Department of Finance
University of Missouri - Columbia

/* Optimization Procedure */

void maxlik(todo, b, xt, yt, v, g, h)
{
b1=b[1]
b2=b[2]
q=b[3]
h=b[4]
i=1

do {
a[i]=b1*at[i] + b2*xt[i]
p[i]=(b1)^2*pt[i] + q
y[i]=a[i]
f[i]=ft[i] + h
at[i+1]=a[i] + ft[i+1]*p[i]*(yt[i]-y[i])
pt[i+1]=p[i] - ft[i+1]*p[i]*p[i]
L[i]=(.5)/f[i] + (.5)*(yt[i]-y[i])*(yt[i]-y[i])/f[i]
i=i+1

} while(i<(n+1))

v=sum(L)
}

S = optimize_init()
optimize_init_evaluator(S, &maxlik())
optimize_init_evaluatortype(S, "d0")
optimize_init_params(S, (0,0,0,0))
optimize_init_which(S, "min")
optimize_init_argument(S, 1, xt)
optimize_init_argument(S, 2, yt)
b=optimize(S)
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/