* illustration of method d2 for MLE procedure * Wooldridge textbook, page 80, exercise 4.14 * method d2 requires explicit expression for gradient and Hessian use http://fmwww.bc.edu/ec-p/data/wooldridge/ATTEND * first use canned routine regress stndfnl atndrte frosh soph * user-defined procedure with method d2 program drop _all program define MLE_d2_linear version 8 args todo b lnf g negH tempvar theta1 theta2 mleval `theta1' = `b', eq(1) mleval `theta2' = `b', eq(2) mlsum `lnf'=-0.5*ln(`theta2')-($ML_y1-`theta1')^2/(2*`theta2') if (`todo'==1|`lnf'>=.) exit tempname d1 d2 mlvecsum `lnf' `d1'=($ML_y1-`theta1')/`theta2', eq(1) mlvecsum `lnf' `d2'=-1/(2*`theta2')+($ML_y1-`theta1')^2/`theta2'^2/2, eq(2) matrix `g'=(`d1',`d2') if (`todo'==1|`lnf'>=.) exit tempname d11 d12 d22 mlmatsum `lnf' `d11'=1/`theta2', eq(1) mlmatsum `lnf' `d12'=($ML_y1-`theta1')/`theta2'^2, eq(1,2) mlmatsum `lnf' `d22'=-1/(2*`theta2'^2)+($ML_y1-`theta1')^2/`theta2'^3, eq(2) matrix `negH'=(`d11',`d12'\ `d12'',`d22') end ml model d2 MLE_d2_linear ( stndfnl= atndrte frosh soph) /s ml maximize /* remark 1: when defining the gradient and Hessian, the derivative is taken with (X'B) as a whole, no need to use chain rule*/ /* remark 2: when forming Hessian, be careful with the transpose operator--there are two '' after d12*/ /* remark 3: there is a blank following immediately after the slash "/" in matrix "negH" */ /* remark 4: d2 method breaks when log_likelihood cannot be linearly formed */