*! version 1.0.0  30apr2001
program define ereghet_glf_sh2
	version 7.0
	args todo b lnf g negH g1 g2

	tempvar beta
	tempname lntheta
	mleval `beta' = `b', eq(1)
	mleval `lntheta' = `b', eq(2) scalar

	local t = "$EREGt"
	local t0 = "$EREGt0"
	local d = "$EREGd"
	local by "by $EREG_by"

	quietly {
	        scalar `lntheta'=cond(`lntheta'<-20,-20,`lntheta')

	        tempname th
 	        tempvar sums sumd logs
	        scalar `th' = exp(`lntheta')

		gen double `logs' = exp(`beta')*(`t0'-`t') if $ML_samp
		`by': gen double `sums' = cond(_n==_N, /*
			*/ sum(`logs'),.) if $ML_samp
		`by': gen double `sumd' = cond(_n==_N, /*
			*/ sum(`d'),.) if $ML_samp

		mlsum `lnf' = cond(`sumd'!=., lngamma(1/`th' + /*
			*/  `sumd') - lngamma(1/`th') - /*
			*/  (1/`th'+`sumd')*ln(1-`th'*`sums') , 0) + /*
			*/  `d'*(`beta'+ln(`t')+`lntheta')

		if `todo'==0 | `lnf'==. {exit}
	}
end
exit