⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _varfcast.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
				
			getSEb , endog_ts(`endogbf_ts') 		///
				 lastob(`lastob') steps(`steps') 	///
				 level(`level') b_ts(`bf_ts')

			foreach v of local eqlist {
				label variable `v'_f_L 		/*
				*/ `"`=strsubdp("`level'")'% lower bound:`v'_f"'
				label variable `v'_f_U 		/*
				*/ `"`=strsubdp("`level'")'% upper bound:`v'_f"'
				label variable `v'_f_se "s.e. for `v'_f"
			}
		}
	}	

	foreach v of local eqlist {
		local created `created' `v'_f 
	}

	if "`bs'`bsp'" != "" {
		foreach v of local eqlist {
			local created `created' `v'_f_L `v'_f_U `v'_f_se 
		}
	}
	else {
		if "`se'" == "" {
			foreach v of local eqlist {
local created `created' `v'_f_L `v'_f_U `v'_f_se 
			}
		}
	}
	
	local _varfcastnames : char _dta[_varfcastnames]
	if "`_varfcastnames'" != "" {
		local _varfcastprior : char _dta[_varfcastprior]
		local _varfcastprior : list _varfcastprior | _varfcastnames
		char define _dta[_varfcastprior] "`_varfcastprior'"
	}	
	char define _dta[_varfcastnames] "`created'"
	_estimates unhold `pest'

end	

program define fcast2
	version 8.0
	syntax varlist , steps(integer) lastob(integer) b_ts(string)

	/* there are three parallel varlists
		e(endog) contains names (possibly with ts ops) that
			were specified by user of var
		e(eqnames) contains names with "." after any
			ts op replaced by "_"
		varlist contains names of temporary variables generated
			from e(endog)
	   note that matrix `b_ts', passed in via t_ts(string) has had any 
	   	endogenous ts op variable replaced by tempvar name 
		via op_colnm `b_ts' orig tempname
	*/	

	local lastobp = `lastob' + `steps'

	if "`e(cmd)'" == "svar" {
		local svar _var
	}	

	local eqlist "`e(eqnames`svar')'"
	local endog "`e(endog`svar')'"
	local exog  "`e(exog`svar')'"
	local lags  "`e(lags`svar')'"

	qui tsset 
	local ta "`r(timevar)'"
	local tsfmt "`r(tsfmt)'"

	if "`ta'" == "" {
		di as error "you must tsset your data before using varfcast"
		exit 198
	}

	tempvar obsvar temp ones 
	
	local newobs=-1*(_N-(`lastob'+`steps'))
	local newobs=max(0,`newobs')+_N

	if _N < `newobs' { 
		local oldobs=_N
		set obs `newobs'	
		local addobs "yes"
	}
	else {
		local addobs "no"
	}

	if "`exog'" != "" {
		tempvar xsamp
		qui mark `xsamp' 
		qui markout `xsamp' `exog'
		qui sum `xsamp' in `lastob'/`lastobp' 
		if r(mean) < 1 {
			di as error "exogenous variables cannot be " /*
				*/ "missing in the forecast period"
			exit 198
		}	
	}

	tempname b_pred
	
	mat `b_pred'=`b_ts'

	local vars_orig : word count `varlist'
	local vars_for  : word count `eqlist'
	
	if `vars_orig' != `vars_for' {
		di as error "number of variables to forecast differs from " /*
			*/ "number of forecasted variables"
		exit 9000
	}


	if "`e(nocons`svar')'" != "" {
		local exog2 "`exog' "
	}
	else {
		local exog2 "`exog' _cons "
	}

	local predob=`lastob'+1


	forvalues i =1(1)`steps' {
		forvalues j = 1/`vars_for' {
			local varf : word `j' of `varlist'
			local eqn  : word `j' of `eqlist'
			mat score `varf'= `b_pred' in `predob', /*
				*/ eq(`eqn') replace
		}	
		local predob = `predob'+1	
	}

	foreach varf of local varlist {
		qui replace `varf'=. if _n<`lastob' | _n>=`predob' 
	}
end


program define getSEb
	version 8.0
	syntax , endog_ts(varlist) lastob(integer) steps(integer) /*
		*/ level(cilevel) b_ts(string)

	/* endog_ts contains endog_ts-list of temporary variables
		that contain dynamic precitions.

	   endog contains list endogenous variables specified in var,
	   	list may contain time series operators.
	   eqlist contains list of equation names, i.e. endog with "."
	  	replaced by "_"
	   Also note that b_ts is e(b) with original endogenous variables
	   	replaced by temporary names.
	  
	*/	

	if "`e(cmd)'" == "svar" {
		local svar _var
	}	

	local eqlist "`e(eqnames`svar')'"
	local endog "`e(endog`svar')'"
	
	foreach var of local eqlist {
		qui gen double `var'_f_L=.
		qui gen double `var'_f_U=.
		qui gen double `var'_f_se=.
	}
	
	tempname B thetai G J Bi A Bt Btt theta
	
	/* make B which is (Kp+1) x (Kp+1) */
	
	if "`e(nocons`svar')'" == "" {
		local cons cons
	}	

	local eqs = e(neqs)
	local mlag = e(mlag`svar')

	if "`cons'" != "" {
		local Bdim = `eqs'*`mlag'+1
	}
	else {
		local Bdim = `eqs'*`mlag'
	}
	local kpm1 = `eqs'*(`mlag'-1)

	_mka2  `endog_ts' , lags(`mlag') aname(`A') `cons' b_ts(`b_ts')

	if `mlag' > 1 {
		local lm1 = `mlag' - 1 
		forvalues i = 0(1)`mlag' {
			if `i' == 0 {
				if "`cons'" != "" {
					matrix `B' = 1 \ `A'cons \ J(`kpm1',1,0)
				}	
			}
			else {
				capture matrix drop `Btt'
				capture matrix drop `Bt'

				forvalues j = 1(1)`lm1' {
					if `j' == `i' {
						matrix `Btt' = nullmat(`Btt') \ I(`eqs')
					}
					else {
						matrix `Btt' = /* 
					     	*/nullmat(`Btt')\J(`eqs',`eqs',0)

					}
				}
				if "`cons'" != "" {
					matrix `Bt' = J(1,`eqs',0) \ `A'`i' \ `Btt' 
				}
				else {
					matrix `Bt' = `A'`i' \ `Btt' 
				}
				matrix `B' =( nullmat(`B') , `Bt' )
			}
		}
	
		if "`cons'" != "" {
			matrix `J' = J(`eqs',1,0) , I(`eqs') , J(`eqs',`kpm1', 0 ) 
		}
		else {
			matrix `J' =  I(`eqs') , J(`eqs',`kpm1', 0 ) 
		}

	}
	else {
		if `mlag' == 1 {
			if "`cons'" != "" {
				matrix `B' = 1 \ `A'cons 
					matrix `Bt' = J(1,`eqs',0) \ `A'1
				matrix `B' =( nullmat(`B') , `Bt' )
			}	
			else {
				matrix `B' = `A'1
			}
			if "`cons'" != "" {
				matrix `J' = J(`eqs',1,0) , I(`eqs') 
			}
			else {
				matrix `J' =  I(`eqs') 
			}
		}

	}
	
	local hm1 = `steps'-1
	
	matrix `theta'0 = I(`eqs')
	matrix `Bi' = `B'
	
	forvalues i = 1(1)`hm1' {
		matrix `theta'`i' = `J'*`Bi'*`J''
		matrix `Bi' = `Bi'*`Bi'
	}


	matrix `G'=e(G`svar')
	local T = e(T`svar') 
	
	tempname BTP BP sig_u O temp1 temp2 Ginv sigy sigyhat norm  /*
		*/ stderr stderrm Cns V2 sigyh sigyhath
	
	matrix `sig_u' = e(Sigma)
	local eqns3 : colnames `sig_u'
	local vns3  : colnames `G'

	foreach vn of local vns3 {
		foreach eqn of local eqns3 {
			local eqn : subinstr local eqn "." "_", all
			local nstripe "`nstripe' `eqn':`vn' "
		}
	}
	matrix `Ginv' = syminv(`G')

	mat `V2' = (1/`T')*(`Ginv'#`sig_u')
	mat colnames `V2' = `nstripe'
	mat rownames `V2' = `nstripe'

	if "`e(constraints`svar')'" != "" {

		_getvarcns
		local cnslist $T_VARcnslist 
		macro drop T_VARcnslist
		macro drop T_VARcnslist2

		_getcns  , v(`V2') cnslist(`cnslist') cnsmat(`Cns')
		capture constraint drop `cnslist'

		local c_cns = colsof(`Cns')
		local c_cns = `c_cns' - 1

		tempname v3 R vce1 IAR

		mat `vce1' = `V2'
		mat `R' = `Cns'[1...,1..`c_cns']
	
		matrix `v3' = syminv(`R'*`vce1'*`R'')
		local a_size = rowsof(`v3')

		local j = 1
		while `j' <= `a_size' {
			if `v3'[`j',`j'] == 0 { 
				error 412 
			} 
			local ++j 
		}

		matrix `v3' = `vce1'*`R''*`v3'
		matrix `IAR' = I(colsof(`vce1')) - `v3'*`R'
		matrix `V2' = `IAR' * `vce1' * `IAR''

	}

	matrix `BTP'0=I(`Bdim')
	matrix `BP'0  =I(`Bdim')

	forvalues i = 1(1)`hm1' {
		local im1 = `i' -1
		matrix `BTP'`i'= `BTP'`im1'*(`B')'
		matrix `BP'`i'= `BP'`im1'*(`B')
	}

	local predob=`lastob'+1

	local level = `level'/100
	scalar `norm' = abs(invnorm( (1-`level')/2))

/* begin new */

	tempname Qt Zt V OMh tempa tempb tempc work 
	tempvar csamp csamp2
						/* csamp is t for sample 
						   obs, note csamp constains
						   . where there are gaps
						*/
						   
	qui gen `csamp' = e(sample)
	qui gen byte `csamp2' = e(sample)
	qui replace `csamp' = sum(`csamp')
	qui replace `csamp' = . if !e(sample)
	qui sum `csamp'

	capture assert r(N) == `T' 
	if _rc > 0 {
		di as err "standard error sample does not agree with "/*
			*/ "estimation sample"
		di as err "(observation number incorrect)"	
		exit 498
	}	
	capture assert r(max) == `T' 
	if _rc > 0 {
		di as err "standard error sample does not agree with "/*
			*/ "estimation sample"
		di as err "(`T' incorrect)"	
		exit 498
	}	


	local vcols = colsof(e(bf`svar'))

	if "`cons'" != "" {
		local cpart "1,"
		local cons_in 1
	}	
	else {
		local cons_in 0
	}

	local deps "`e(depvar`svar')'"
	local lags `e(lags`svar')'


	forvalue lag = 1/`mlag' {
		foreach dvar of local deps {
			local tslist " `tslist' L`lag'.`dvar' "
		}
	}

	local olist `tslist'
	tsrevar `tslist'
	local tslist `r(varlist)'

	local NT = _N

	forvalues h=1(1)`steps' {
		mat `OMh'   = J(`eqs',`eqs', 0)
		mat `OMh'[1,2] = 1
		mat `OMh'[1,2] = 0
		mat `sigyh' = J(`eqs',`eqs', 0)
		local sm1   = `h' -1
		
		forvalues j4 = 0/`sm1' {
			matrix `sigyh' = `sigyh' + 	/*
				*/ `theta'`j4'*`sig_u'*(`theta'`j4')'	
		}


		_varfstd  `tslist', qt(`Qt') zt(`Zt') touse(`csamp2')	///
			btp(`BTP') theta(`theta') omh(`OMh')		///
			v2(`V2')  step(`h') constant(`cons_in')		///
			eqs(`eqs') vcols(`vcols') tempa(`tempa')	///
			tempb(`tempb') tempc(`tempc') work(`work')

/* See Note 1 below */	

		mat `OMh' = (1/`T')*`OMh'
		mat `sigyhath' = `sigyh' + `OMh'

		local i 1
		foreach var of local eqlist {
		 	scalar `stderr' = sqrt(`sigyhath'[`i',`i'])	
			
			qui replace `var'_f_L=`var'_f -`norm'*`stderr' /*
				*/ in `predob'
			qui replace `var'_f_U=`var'_f +`norm'*`stderr' /*
				*/ in `predob'
			qui replace `var'_f_se=`stderr' in `predob'
			local i = `i' + 1

		}
		local predob = `predob'+1	
	}


end
program define _getcns, eclass

	syntax , v(name) cnslist(numlist) cnsmat(name)

	tempname b v2 pest
	tempvar samp
	
	mat `v2' = `v'
	mat `b' = `v'[1,1...]

	qui _estimates hold `pest', nullok copy restore varname(`samp')

	eret post `b' `v2'
	
	mat makeCns `cnslist'

	mat `cnsmat' = get(Cns)

	_est unhold `pest'

	
end

exit

Note 1:

The ado code below serves as documentation for the internal routine 
	_varfstd.  These computations are performed by the internal routine

		forvalues ct =1/`NT'  {
			if `csamp2'[`ct'] == 1 {
				mat `Qt' = J(`eqs',`vcols',0)

				local tslist2 "`tslist'"

				gettoken dvar tslist2:tslist2
				local lpart = `dvar'[`ct']
			
				foreach dvar of local tslist2 {
					local val = `dvar'[`ct'] 
					local lpart "`lpart', `val' "
				}
				mat `Zt' = (`cpart'`lpart')
			
				forvalues i=0(1)`sm1' {
local hm1i = `h'-1-`i'
mat `Qt' = `Qt' + ( (`Zt'*`BTP'`hm1i')#`theta'`i')
				}

				mat `OMh' = `OMh' + `Qt'*`V2'*`Qt''
			}

		}
end ado code documentation for _varfstd() 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -