*! lrcotest--LR test of coefficients in cointegrating relationships
*!           You must run mlcoint before running this test.
*! version 2.0     Ken Heinecke     2/22/93     sts9: STB-21
/*
	lrcotest uses the following matrices stored by mlcoint:

		S_E_ISDD, S_E_SDL, S_E_SLL, S_E_TEVL, S_E_TSDL
*/
program define lrcotest
quietly {
	version 3.1
	if "$S_E_cmd"~="mlcoint" { error 301 }
	local varlist "req ex min(1)"
	local options "Cirel(int 1) Restrict"
	parse "`*'"
	local restric = "`restric'"!=""
	tempname H HP _H IRCHOLD MAXL _M NPHIP NRBETAP NTREVEC RALPHA RCHOLD REIGVAL REVECT TIRCH TRALPHA TREVAL TREVECT Y1 Y2 Y3 Y4 Y5 Y6 Y7
/*
	Create restriction matrix H from the variables in the model.
*/
	mat `H' = I($S_E_nv)
	mat colnames `H' = $S_E_vl
	parse "`varlist'", parse(" ")
	local tvar : word count `varlist'
/*
	Now create a list of the variables in the model that are NOT
	being tested.
*/
	parse "$S_E_vl", parse(" ")
	local i 0
	while `i'<$S_E_nv {
		local i = `i'+1
		_invlist ``i'' `varlist'
		if $S_1==0 { local clist = "`clist'"+" ``i''" }
	}
/*
	Create restriction matrix
*/
	parse "`clist'",parse(" ")
	while "`1'"!="" {
	 	mat `_H' = `H'[.,"`1'"]
		mat `_M' = `_M',`_H'
		mac shift
	}
	mat `H' = `_M'
	mat rownames `H' = $S_E_vl
/*
	Finally, create a numbered list of vectors
*/
	local j 0
	while  `j'<`cirel' {
		local j = `j' + 1
		local vecl = "`vecl'" + " vec`j'"
	}
/*
	Calculate eigenvalues from the restricted model
*/
	mat `HP' = `H''
	mat `Y1'= `HP' * S_E_SLL
	mat `Y2' = `Y1' * `H'
	mat `RCHOLD' = cholesky(`Y2')
	mat `IRCHOLD' = inv(`RCHOLD')
	mat `TIRCH' = `IRCHOLD''
	mat `Y3' = `IRCHOLD' * `HP'
	mat `Y4' = `Y3' * S_E_TSDL
	mat `Y5' = `Y4' * S_E_ISDD
	mat `Y6' = `Y5' * S_E_SDL
	mat `Y7' = `Y6' * `H'
	mat `MAXL' = `Y7' * `TIRCH'
	mat symeigen `REVECT' `REIGVAL' = `MAXL'
	mat `TREVAL' = `REIGVAL''
	if `restric' {			/* Display restricted estimates */
		noi di _new in gr "Eigenvalues from restricted model"
		noi mat l `TREVAL', noh nonames noblank
/*
	Calculate and display the normalized restricted alpha and
	beta prime matrices
*/
		mat `TREVECT' = `REVECT''
		mat `NTREVEC' = `TREVECT' * `IRCHOLD'
		mat `NPHIP' = `NTREVEC'[1..`cirel',.]
		mat `NRBETAP' = `NPHIP' * `HP'
		mat rownames `NRBETAP' = `vecl'
		noi di _new in gr "Normalized restricted Beta'"
		noi mat l `NRBETAP', names nob noh
		mat `TRALPHA' = `NRBETAP' * S_E_TSDL
		mat `RALPHA' = `TRALPHA''
		mat rownames `RALPHA' = $S_E_vl
		noi di _new in gr "Normalized restricted Alpha"
		noi mat l `RALPHA', nob noh
	}			/* end restricted estimates */
/*
	Calculate likelihood ratio test statistics
*/
	local i 0
	local stat = 0
	while `i'<`cirel' {
		local i = `i'+1
		local stat = `stat' + log((1-`TREVAL'[`i',1])/(1-S_E_TEVL[`i',1]))
	}
	global S_3 = `cirel' * `tvar'		/* degrees of freedom */
	global S_6 = $S_E_N * `stat'		/* LR statistic       */
	global S_7 = chiprob($S_3,$S_6)	/* P-value	      */
	local lrstat = round($S_6,.01)
	local pval = round($S_7,.01)
} 	/* end quietly */
	di _new in gr "Cointegration: likelihood ratio test" _col(40) "chi2(" in ye "$S_3" in gr ") = " in ye "`lrstat'"
	di in gr _col(40) "Prob > chi2 = " in ye "`pval'"
end