Regresja Liniowa-Funkcja Wiarygodno Ci Gradient I Hesjan Wersja

cap pr drop myreg3

program define myreg3

	version 8 

	args todo b lnf g negH

	tempvar theta

	tempname sigma 

	mleval `theta' 		= `b', eq(1) 

	mleval `sigma' 	= `b', eq(2) scalar

	mlsum `lnf' = -($ML_y1-`theta')^2/(2*`sigma'^2)-0.5*ln(`sigma'^2) 

	if (`todo' ==0 | `lnf' ==.) exit 

	tempvar fb fs

	mlvecsum `lnf' `fb' = ($ML_y1-`theta')/`sigma'^2,	 	eq(1) 

	mlvecsum `lnf' `fs' = ($ML_y1-`theta')^2/(`sigma'^3)-1/`sigma',	eq(2)

	matrix `g' = (`fb',`fs') 
	if (`todo'==1 | `lnf'==.) exit 


	tempvar fbb fbs fss

	mlmatsum `lnf' `fbb'	= -1/`sigma'^2, 					eq(1)

	mlmatsum `lnf' `fbs'	= -2*($ML_y1-`theta')/(`sigma'^3), 			eq(1,2)

	mlmatsum `lnf' `fss'	= -3*($ML_y1-`theta')^2/(`sigma'^4)+1/(`sigma'^2),	eq(2) 

	matrix `negH' = (`fbb',`fbs' \ `fbs'',`fss')

end

ml model d2 myreg3 (reg:y=x1 x2) /sigma

ml maximize, //hess grad