Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Simple evaluation of lf evaluators in mata...
From
Matthew Baker <[email protected]>
To
[email protected]
Subject
st: Simple evaluation of lf evaluators in mata...
Date
Tue, 15 Jan 2013 11:56:31 -0500
Dear Listers --
I am encountering the following problem with trying to evaluate
objective functions written up under different sorts of evaluators in
mata. The problem is, one can easily evaluate functions written for d0
evaluators through direct application of the function, but this
doesn't appear to be the case. That is, if I've constructed a model
statement M to go with a d0 evaluator and a function d0eval(), I can
do:
d0eval(0,M,params,val=.,g=.,H=.)
and val contains the value of the function evaluated at the
parameters. If I try this with a type lf evaluator lfeval(), and do:
lfeval(0,M,params)
the function is stuck at a previously determined value. My question:
is there a way to get a quick function evaluation in the lfeval()
situation, as in the d0eval() situation? Perhaps the following coded
example will illustrate the problem:
/* Begin example */
/* First, a type d0 evaluator */
clear all
sysuse auto
mata:
function lregeval(M,todo,b,crit,s,H)
{
real colvector p1, p2
real colvector y1
p1=moptimize_util_xb(M,b,1)
p2=moptimize_util_xb(M,b,2)
y1=moptimize_util_depvar(M,1)
crit=-(y1:-p1)'(y1:-p1)/(2*exp(p2))-rows(y1)/2*p2
}
M=moptimize_init()
moptimize_init_evaluator(M,&lregeval())
moptimize_init_evaluatortype(M,"d0")
moptimize_init_depvar(M,1,"mpg")
moptimize_init_eq_indepvars(M,1,"price weight displacement")
moptimize_init_eq_indepvars(M,2,"")
moptimize(M)
lregeval(M,0,J(1,5,0),crit1=.,s=.,H=.)
crit1
lregeval(M,0,runiform(1,5),crit2=.,s=.,H=.)
crit2
/* crit1 and crit2 faithfully reflect the value of the function at the
parameters */
/* Now, a type lf evaluator */
function lregeval_lf(M,b,lnf)
{
real colvector p1, p2
real colvector y1
p1=moptimize_util_xb(M,b,1)
p2=moptimize_util_xb(M,b,2)
y1=moptimize_util_depvar(M,1)
lnf=-(y1:-p1):^2:/(2*exp(p2)):-1/2*p2
}
P=moptimize_init()
moptimize_init_evaluator(P,&lregeval_lf())
moptimize_init_evaluatortype(P,"lf")
moptimize_init_depvar(P,1,"mpg")
moptimize_init_eq_indepvars(P,1,"price weight displacement")
moptimize_init_eq_indepvars(P,2,"")
moptimize(P)
lregeval_lf(P,J(1,5,0),lnf1=.)
sum(lnf1)
lregeval_lf(P,runiform(1,5),lnf2=.)
sum(lnf2)
/* sum(lnf1) and sum(lnf2) are "stuck" at their optimized values, and
dont change with parameter values */
/* End example */
Any advice? I suspect that the optimization is overwriting some aspect
of the structure underlying the problem and just uses that once
overwritten.
Best,
Matt Baker
--
Dr. Matthew J. Baker
Department of Economics
Hunter College and the Graduate Center, CUNY
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/