*! version 1.0.0  31aug2001
program define weibhet_glfa_sh2
	version 7.0
	args todo b lnf g negH g1 g2

	tempvar beta
	tempname lntheta lnp
	mleval `beta' = `b', eq(1)
	mleval `lnp' = `b', eq(2) scalar
	mleval `lntheta' = `b', eq(3) 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 p
 	        tempvar sumh sums sumd
	        scalar `th' = exp(`lntheta')
		scalar `p' = exp(`lnp')

		`by': gen double `sumh' = cond(_n==_N, /*
		   */ sum(`d'*(-`p'*`beta'+`lnp'+`p'*ln(`t'))),.) if $ML_samp
		`by': gen double `sums' = cond(_n==_N, /*
			*/ sum(exp(-`p'*`beta')*(`t0'^`p' - `t'^`p')), 0) /*
			*/ if $ML_samp
		`by': gen double `sumd' = cond(_n==_N, /*
			*/ sum(`d'),.) if $ML_samp

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

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