# st: finding roots in Mata

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject st: finding roots in Mata Date Fri, 30 Jan 2009 13:05:16 -0500

```Roger et al.--
Out of curiosity, I compared:
1. using Ben Jann's mm_root() (findit moremata) to find a zero of f
to
2. using official Stata optimize() to find a min of f^2
for a problem of occasional interest--finding the modal kth order
statistic in a sample of n draws from a standard normal distribution,
which is given by the zero of f(x)=
(k-1)*(1-normal(x))*normalden(x)-(n-k)*normalden(x)*normal(x)-normal(x)*(1-normal(x))*x
per Gupta (1961),
http://projecteuclid.org/handle/euclid.aoms/1177704982
and in this case Ben Jann's mm_root() is far superior.

For n more than about 13, and k=n, optimize() skips over interior
solutions and skitters off toward a very large x where normalden(x)
and 1-normal(x) both approach zero.  There is an alternative
parametrization for which optimize() performs as well as mm_root(), in
terms of finding the right answer, but optimize() takes 7 msec and
mm_root takes 2 msec:
f=(k-1)/normal(x)-(n-k)/normal(-x)-x/normalden(x)

The above also illustrates a possibly common pitfall in such problems,
which frequent users of -ml- may already know well--beware of using
quantities like 1-normal(x), which can easily reach zero as x gets
large while the equivalent normal(-x) is nonzero.

On Thu, Jan 29, 2009 at 3:03 AM, Roger Harbord <rmharbord@googlemail.com> wrote:
> On Thu, Jan 29, 2009 at 12:57 AM, Austin Nichols
> <austinnichols@gmail.com> wrote:
> <snip>
>>
>> I think Mata's optimizer is probably the best option for this problem,
>> if one has Stata 10 --Roger Harbord says "Mata -optimize()-  is also
>> designed as a maximizer (or minimizer) rather than a root-finder" but
>> note that Mata's optimize() can minimize (F(x)-A)^2 just fine which is
>> the same as finding a root of F(x)-A.
>>
>>
> I just had a gut feeling that changing a root-finding problem to a
> minimization problem in this way loses information in some ill-defined
> sense so is likely to be less efficient, and maybe less robust too. I
> have no more formal or empirical evidence either way though. Anyone?
> --
> Roger Harbord
> http://www.epi.bris.ac.uk/staff/rharbord.htm
*
*   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/
```