Bookmark and Share

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]

Re: st: moptimze: what's the difference of these practices


From   jpitblado@stata.com (Jeff Pitblado, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: moptimze: what's the difference of these practices
Date   Thu, 15 Nov 2012 16:10:51 -0600

Sun Yutao <yutao.sun.statalist@outlook.com> has written a log-likelihood
evaluator in Mata to be used with -moptimize()-, and is computing xb
calculation by hand instead of using -moptimize_util_xb()- but is getting an
error that the numerical derivatives cannot be computed:

> I'm working with -moptimize()- and I just want know what's the difference 
> between directly using the parameter feed by the -moptimize()- and 
> using -moptimize_util_xb()-
> 
> I have a callback function say: feval(M,p,v) and when I'm using the p directly 
> to compute the log-likelihood it always says "could not calculate numerical 
> derivatives -- flat or discontinuous region encountered", but according to the 
> help file: " The second argument, b (p in my function), is the entire 
> coefficient vector". Here is an example of how I would compute the 
> xb: -xb=XB*p[1::4]':+p[5]-   (4 x*b + a cons)
> 
> But when I switch to -xb=moptimize_util_xb(M,b,1)-, everything is ok again...
> 
> Does anyone know the difference of these two?

Short answer
------------

Sun does not provide the code for the likelihood evaluator for the calls the
the -moptimize()- subroutines, but we can infer that sun is doing a hand
calculation where the -moptimize_util_xb()- subroutine should be used.

This subroutine, -moptimize_util_xb()-, needs to be used to compute the xb
part of the log-likelihood calculation in the likelihood evaluator function
in order for -moptimize()- to compute numerical derivatives for the default
evaluator type, "lf".

Longer answer explaining why
----------------------------

Sun did not mention the evaluator type being used.

Given the above error message and the arguments to Sun's feval(M,p,v)
function, we suspect the evaluator type is "lf".

-moptimize()- uses the chain rule to compute the gradient vector and Hessian
matrix when it is tasked with computing the numerical derivatives for the "lf"
evaluator types.  In this case, the numerical derivatives are taken with
respect to each linear equation (xb) instead of each model parameter
(elements of b).

One way to compute the equation-level partials of the log-likelihood at each
observation is to compute the partial of the log-likelihood with respect to
the intercept, since they are the same (mechanically and mathematically).
This is a nice simplification, except that -moptimize()- does not require that
all equations have an intercept.  For this reason -moptimize()- cannot use
modifications to the b vector to compute equation-level partials of the
log-likelihood.

For the "lf" evaluator type, -moptimize()- computes the numerical derivatives
by introducing deviations to the xb calculations directly through the
-moptimize_util_xb()- function.  This means that "lf" evaluator types require
the evaluator function to use -moptimize_util_xb()- to calculate an xb for
each equation in order to properly compute the numerical derivatives.  

Sun's not using -moptimize_util_xb()- prevents -moptimize()- from properly
computing numerical derivatives.

--Jeff
jpitblado@stata.com
*
*   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/


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