*! wcotest--Wald 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
/*
	wcotest uses the following matrices stored by mlcoint:

		S_E_NBET, S_E_TEVL
*/
program define wcotest
quietly {
	version 3.1
	local varlist "req ex min(1)"
	local options "Cirel(int 1)"
	if "$S_E_cmd"~="mlcoint"{error 301}
	parse "`*'"
	tempname B BPK D I ID IDlI IKvvK _J K KBDBK KP KPB KPBD KPv KvvK _K lambda NBETA stat v vPK

	mat `K' = I($S_E_nv)
	mat colnames `K' = $S_E_vl
	mat rownames `K' = $S_E_vl
	mat `NBETA' = S_E_NBET'
/*
	Create restriction matrix
*/
	parse "`varlist'", parse(" ")
	while "`1'"!="" {
		mat `_K' = `K'[.,"`1'"]
		mat `_J' = `_J',`_K'
		mac shift
	}
	mat `K' = `_J'
/*
	Calculate Wald squared statistic (J&J Oxford Bulletin 52,2 1990)
*/
	local tvar : word count `varlist'
	mat `B' = `NBETA'[.,1..`cirel']
	mat `KP' = `K''
	mat `KPB' = `KP'*`B'
	mat `BPK' = `KPB''
	mat `I' = I(`cirel')
	mat `lambda' = S_E_TEVL[1..`cirel',.]
	mat `D' = diag(`lambda')
	mat `ID' = inv(`D')
	mat `IDlI' = `ID'-`I'
	mat `D' = inv(`IDlI')
	mat `KPBD'=`KPB'*`D'
	mat `KBDBK' = `KPBD'*`BPK'
	local ncv = `cirel'+1
	mat `v' = `NBETA'[.,`ncv'...]
	mat `KPv' = `KP'*`v'
	mat `vPK' = `KPv''
	mat `KvvK' = `KPv'*`vPK'
	mat `IKvvK' = inv(`KvvK')
	mat `stat' = `KBDBK' * `IKvvK'
	global S_3 = `cirel'*`tvar'		/* degrees of freedom */
	global S_6 = $S_E_N * trace(`stat')	/* Wald statistic     */
	global S_7 = chiprob($S_3,$S_6)	/* P-value	      */
	local wald = round($S_6,.01)
	local pval = round($S_7,.01)
}					/* end quietly */
	di _new in gr "Cointegration: Wald test" _col(40) "chi2(" in ye "$S_3" in gr ") = " in ye "`wald'"
	di in gr _col(40) "Prob > chi2 = " in ye "`pval'"
end