*! version 1.0.0 Stephen P. Jenkins, Dec 1997
*! Fitting of Singh-Maddala distribution by ML
*! Called by smfit.ado

program define sm_ll

version 4.0

	local  c = "`1'"  /* Starting value vector on input   */
	local  f = "`2'"  /* Scalar to contain function value */

	tempname a b q

	local  p1 = `c'[1,1]
	local  p2 = `c'[1,2]
	local  p3 = `c'[1,3]

	scalar `a' = exp(`p1') + 1
	scalar `b' = exp(`p2') + 1
	scalar `q' = exp(`p3')

	quietly  {

		tempvar lnx lnp
		summ $S_mlwgt
		local sum0 = _result(3)*_result(1)
		gen double `lnx' = $S_mlwgt * ln($S_mldepn)
		summ `lnx'
		local sum1 = _result(3)*_result(1)
		gen double `lnp' = $S_mlwgt * ln(1 + ($S_mldepn/`b')^`a')
		summ `lnp'
		local sum2 = _result(3)*_result(1)

		scalar `f' = `sum0' * ( ln(`a'*`q') - `a'*ln(`b') ) /*
		                   */  + (`a'-1)*`sum1' - (`q'+1)*`sum2'
	}

end