# RE: st: MLE of a function with complex coefficients

 From "Pete Aling" To , Subject RE: st: MLE of a function with complex coefficients Date Wed, 5 Apr 2006 21:32:17 +0200

```Hi Jeff,

Thanks so much for the help with the question I posted last week. Your solutions helped a lot. There are two issues I'm still having though.

1. The likelihood function isn't the log of the Gaussian density function as you thought but rather minus twice the log of the Gaussian density. However, when I run the program I get an error message.

2. The model I'm estimating was the unrestricted version of a number of models. These models are created by imposing parameter restrictions on the alpha, beta, and gamma coefficients. My question is how do I impose and test these restrictions?

They are 	1. beta = 0, gamma = 0
2. gamma = 0
3. gamma = 1/2
4. alpha = 0, beta = 0, gamma = 1
5. alpha = 0, gamma = 1
6. gamma = 1
7. alpha = 0, beta = 0, gamma = 3/2
8. alpha = 0

Here is a copy of my code as it stands now.

capture program drop myest
program define myest
version 8.2

args lnf alpha beta sigma2 gamma
tempvar a b c d e m s
quietly gen double `a' = `sigma2'/(2*`beta')
quietly gen double `b' = exp(2*`beta') - 1
quietly gen double `c' = 2*`gamma'
quietly gen double `d' = exp(`beta')
quietly gen double `e' = `alpha'/`beta'

quietly gen double `m' = `d'*ltbrate+`e'*(`d'-1)
quietly gen double `s' = `a'*`b'*(ltbrate^`c')

quietly replace `lnf' = -2*lnnormalden(\$ML_y1,`m',`s')

end

ml model lf myest (alpha: tbrate=) /beta /sigma2 /gamma if ltbrate < .
ml maximize

If you don't mind helping me out I'd really appreciate it. I am using stata9 at the moment. Also, should I send you my dataset?

Regards,
Peter Aling

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Jeff Pitblado, StataCorp LP
Sent: 31 March 2006 07:43 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: MLE of a function with complex coefficients

Pete Aling <pete@unblinkinginsight.com> is having trouble getting -ml- to
iterate with his likelihood evaluator:

> I'm using STATA to do MLE and I'm using the lf method.
>
> I have only 1 variable, interest rate and it is explained only as a function
> of its previous value. The coefficients however are complex and non-linear
> thus I need to hard code the lag into the likelihood function. I'm not sure
> whether I've done this correctly since I get the following result when I run
> my do file.

> initial:       log likelihood =     -<inf>  (could not be evaluated)
>
> could not find feasible values
> r(491);
>
> The process that the interest rate follows is this
>
> tbrate = exp(beta)*L.tbrate + (alpha/beta)*(exp(beta)-1) + e
>
> where e is an error term. The likelihood function is in the paper and is
> based on the form of tbrate and the properties of the error term.

> Here is a copy of my code. If anyone has more experience with this and might
> be able to help I'd appreciate it.  tbrate is the interest rate variable and
> ltbrate is its lagged values.

> capture program drop myest
> program define myest
> 	version 6
>
> 	args lnf theta1 theta2 theta3 theta4
> 	tempvar a b c d e
> 	quietly gen double `a'= `theta3'/(2*`theta2')
> 	quietly gen double `b'= exp(2*`theta2') - 1
> 	quietly gen double `c'=	2*`theta4'
> 	quietly gen double `d'= exp(`theta2')
> 	quietly gen double `e'= `theta1'/`theta2'
> 	quietly replace `lnf' = (ln(`a'*`b'*(ltbrate)^`c')) + ((\$ml_y1-`d'*ltbrate-`e'*(`d'-1))^2)/(`a'*`b'*(ltbrate)^`c')
> end
> ml model lf myest (tbrate) /alpha /beta /sigma2 /gamma
> ml maximize

Pete does not say which Stata release he is using, and although he has a
-version 6- in his likelihood evaluator I will assume Stata 8.2.

I found the following issues with Pete's code:

1.  The -ml model- statements specifies 5 equations, but -myest- only
identifies 4.  I'll assume that '/alpha' was meant to be the first equation,
thus the -ml model- statement should be:

ml model lf myest (alpha: tbrate) /beta /sigma2 /gamma

2.  The dependent variable is not specified correctly; as coded above -ml-
thinks -tbrate- is an independent variable, thus the -ml model- statement
should be further changed to:

ml model lf myest (alpha: tbrate =) /beta /sigma2 /gamma

3.  -myest- refers to '\$ml_y1' when it should be referring to '\$ML_y1'
(capital M and capital L).

4.  The log-likelihood formula is up-side-down, and appears to be normal
density function (without the square root of 2*pi and missing a sqrt or 0.5 in
the first -ln()- term).  When possible I prefer to code with Stata's built-in
functions to simplify my expressions, this also keeps me from making mistakes
in my formulas.  In this case, I would use the -normden()- function
(also known as -normalden()- in Stata 9).

5.  -ml- does not get to see -ltbrate-, thus it cannot exclude the 1 expected
missing value from the estimation sample.  This is easily fixed with an -if-
statement in the call to -ml-.

Since I do not have Pete's dataset, I used the auto data to verify that my
changes would result in a converged model fit.  Here is my do-file:

***** BEGIN:
clear

capture program drop myest
program define myest
version 8.2

args lnf alpha beta lns2 gamma
tempvar a b c d e m s
quietly gen double `a' = exp(`lns2')/(2*`beta')
quietly gen double `b' = exp(2*`beta') - 1
quietly gen double `c' = 2*`gamma'
quietly gen double `d' = exp(`beta')
quietly gen double `e' = `alpha'/`beta'

quietly gen double `m' = `d'*ltbrate-`e'*(`d'-1)
quietly gen double `s' = `a'*`b'*(ltbrate)^`c'

quietly replace `lnf' = ln(normalden(\$ML_y1,`m',`s'))
end

* surrogate for Pete's data
sysuse auto
sort mpg
gen t = _n
tsset t
rename mpg tbrate
gen ltbrate = l.tbrate

ml model lf myest (alpha: tbrate=) /beta /lnsigma2 /gamma if ltbrate < .
ml init /beta = 1 /gamma = 1
ml maximize
***** END:

Note that I added my own initial values for '/beta' and '/gamma', this was to
prevent -ml- from having to use random initial values caused by starting out
with a missing log-likelihood (-ml-'s default initial values are all 0's
resulting in missing values in tempvars `a' and `e').

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

--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.3.3/295 - Release Date: 2006/03/28

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.3.5/300 - Release Date: 2006/04/03

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