# RE: st: nonlinear constraints

 From "Nick Cox" To Subject RE: st: nonlinear constraints Date Wed, 14 Feb 2007 12:32:54 -0000

```Just looking at this, I see scope for some simplifications:

program define pplogit
tempvar theta
version 6
args lnf theta1 theta2 theta3
gen double `theta' = `theta1' + `theta3' + (`theta2'*`theta3')
quietly replace `lnf' = ln(exp(`theta')/(1+exp(`theta'))) if \$ML_y1==1
quietly replace `lnf' = ln(1/(1+exp(`theta'))) if \$ML_y1==0
end

In the line

quietly replace `lnf' = ln(exp(`theta')/(1+exp(`theta'))) if \$ML_y1==1

the log of a ratio is the difference of the logs

= ln(exp(`theta')) - ln(1 + exp(`theta')) if \$ML_y1 == 1

but then the first term simplifies too:

= `theta' - ln(1 + exp(`theta')) if \$ML_y1 == 1

A similar story below:

quietly replace `lnf' = ln(1/(1+exp(`theta'))) if \$ML_y1==0

= ln(1) - ln(1+exp(`theta')) if \$ML_y1==0
= - ln(1+exp(`theta')) if \$ML_y1==0

Check my algebra and code, and I don't know how much difference it makes
if correct.

Nick
n.j.cox@durham.ac.uk

Richard Williams

> At 06:11 AM 2/14/2007, Maarten buis wrote:
> >--- Valérie Orozco:
> > > I also want to impose nonlinear constraints and I don't find
> > > any way to do it.
> >
> >The only way to impose non-linear constraints in Stata is to
> >write your own -ml- program in which the constraint is hard
> >coded. An example of such a program can be found in the appendix
> >of (Hauser and Andrew 2006), though it has some bad programming
> >style, especially doing some intermediate calculations by
> >creating a variable that is a float and not a double. These
> >models tend to have a pretty hard time converging, and appear to
> >be particularly sensitive to loss of precision in intermediate
> >calculations.
>
> As Maarten notes, the program printed in the
> Hauser and Andrew paper is sub-optimal because it
> does not use double precision.  Part of their code is
>
> program define pplogit
> tempname theta
> version 6
> args lnf theta1 theta2 theta3
> gen `theta' = `theta1' + `theta3' + (`theta2'*`theta3')
> quietly replace `lnf' = ln(exp(`theta')/(1+exp(`theta'))) if
> \$ML_y1==1
> quietly replace `lnf' = ln(1/(1+exp(`theta'))) if \$ML_y1==0
> end
>
>
> If anyone is using it, I've found that this
> relatively simple tweak makes it run much more
> quickly and (presumably) accurately.
>
> program define pplogit
>          tempvar theta
>          version 6
>          args lnf theta1 theta2 theta3
>          gen double `theta' = `theta1' + `theta3' +
> (`theta2'*`theta3')
>          quietly replace `lnf' =
> ln(exp(`theta')/(1+exp(`theta'))) if \$ML_y1==1
>          quietly replace `lnf' = ln(1/(1+exp(`theta'))) if \$ML_y1==0
> end

*
*   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/
```