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.

# Re: st: Maximum likelihood estimation

 From "Brian P. Poi" To statalist@hsphsun2.harvard.edu Subject Re: st: Maximum likelihood estimation Date Wed, 13 Feb 2013 16:00:46 -0600

```On 02/13/2013 03:27 PM, Joseph Monte wrote:
```
```Dear Statalisters,

I'll try again with a little more info since I did not get any
responses. Here is the code I have so far (based on previous Statalist
posts and "Maximum Likelihood estimation with Stata" by
Gould, Pitbaldo and Poi, 4th ed.). The paper I cited in my first email
below models the log of the variance of the regression error in
equation 2 while I believe I have modelled the log of sigma. I would
preferably like to model the log of the variance as in the paper cited
but am not sure how.

cscript
program mynormal_lf1
version 12
args todo b lnfj g1 g2
tempvar mu lnsigma sigma
mleval `mu' = `b', eq(1)
mleval `lnsigma' = `b', eq(2)
quietly {
gen double `sigma' = exp(`lnsigma')
replace `lnfj' =
ln(normalden(\$ML_y1,`mu',`sigma'))
if (`todo'==0) exit
tempvar z
tempname dmu dlnsigma
gen double `z' = (\$ML_y1-`mu')/`sigma'
replace `g1' = `z'/`sigma'
replace `g2' = `z'*`z'-1
}
end

ml model lf1 mynormal_lf1 (mu: y = x1 x2 x3 x4 x5 x6 x7 x8 x9)
(lnsigma: y = x1 x2 x3 x4 x5 x6 x7 x8 x9)
ml max

Thanks,

Joe

```
```
Joe,

Letting Stata do the work of computing numerical derivatives, here is the equivalent program that models the log variance rather than the log standard deviation:

-------------
cscript

program mynormal_lf0
version 12
args todo b lnfj
tempvar mu lnvar var
mleval `mu' = `b', eq(1)
mleval `lnvar' = `b', eq(2)
quietly {
gen double `var' = exp(`lnvar')
replace `lnfj' =  ln(normalden(\$ML_y1,`mu',sqrt(`var')))
}
end

ml model lf0 mynormal_lf0 (mu: y = x1 x2 x3 x4 x5 x6 x7 x8 x9)		///
(lnvar: y = x1 x2 x3 x4 x5 x6 x7 x8 x9)
ml max
-------------

The only real change is that since the normalden() function's third argument is the standard deviation, not the variance, we need to take the square root of the variance.  By construction, the variance variable `var' will be non-negative, so taking the square root is not a problem here.

-- Brian Poi
-- bpoi@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/
```