*! version 1.0.0  17apr2008
program _haplologit_d1_rare
	version 10.0
	args todo b lnf g
	// dependent variable
	local y    $MY_depvar
	// haplotype indices
	local hap1 $MY_hap1
	local hap2 $MY_hap2
	// subject ID
	local by $MY_panel
	tempvar L last
	qui gen double `L' = .
	qui by `by': gen byte `last' = (_n==_N)
	// haplotype effects
	local neffect = colsof($MY_riskhapmat)
	local nriskhparms = `neffect'
	forvalues i=1/`neffect' {
		tempvar riskhap`i'
		mleval `riskhap`i'' = `b', eq(`i')
		local riskhapxb `riskhapxb' `riskhap`i''
	}
	// haplotype frequencies
	tempname numat
	mat `numat' = 0
	local n = `neffect'+1
	forvalues i = $ML_n(-1)`n' {
		local j = `i'-1
		tempname nu`j'
		mleval `nu`j'' = `b', eq(`i') scalar
		mat `numat' = `nu`j'', `numat'
	}
	tempname scores
	mata: _haplologit_nr(&$MY_inhermodel(), `"`scores'"')

	mlsum `lnf' = `L' if `last'
	if (`todo'==0 | `lnf' >= .) exit

	// matrix scores contains first-order derivatives
	// computed in _haplologit_nr()
	matrix `g' = (`scores')

end