di as result 		///
"This code was written by Anson Soderbery to accompany the paper " _newline ///
"Estimating Import Supply and Demand Elasticities: Analysis and Implications." _newline ///
"Code was last updated on 4/4/13."

mata 
mata clear

// LIML SEARCH OVER SIGMA AND RHO

real LIML2( todo, real vector d , real vector Y ,real matrix X ,real matrix Z ,Q, g ,H) { 
	ZZ = invsym( cross( Z , Z ) ) * Z'
	P = Z * ZZ
	theta0= d[1]
	theta1 = d[3] / ( (1 - d[3])*(d[2] - 1)^2 ) 
	theta2 = (2*d[3] - 1) /  ( (1 - d[3])*(d[2] - 1) )
	// CONSTRAIN SIGMA>1 AND 0<RHO<1
		d[2] = exp(log(d[2]-1))*(d[2]/(d[2]-1))
		d[3] = exp(log(d[3])+log(1-d[3]))*(1/(1-d[3]))
	theta =  (theta0 , theta1 , theta2 )
	A = Y - X*theta'
	AA = A'*A
	Q = ((A'*P*A) ) /AA 
}
end mata 
