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

📄 arch_p.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
*! version 6.1.5   24sep2004
program define arch_p  
	version 6, missing

		/* Step 1:
			place command-unique options in local myopts
			Note that standard options are
			LR:
				Index XB Cooksd Hat 
				REsiduals RSTAndard RSTUdent
				STDF STDP STDR noOFFset
			SE:
				Index XB STDP noOFFset
		*/

	local myopts AT(string) Dynamic(string) HET Residuals /*
		*/ STRuctural T0(string) Variance XB Y YResiduals

		/* Step 2:
			call _propts, exit if done, 
			else collect what was returned.
		*/
			/* takes advantage that -myopts- produces error
			 * if -eq()- specified w/ other than xb and stdp */

	_pred_se "`myopts'" `0'
	if `s(done)' { exit }
	local vtyp  `s(typ)'
	local varn `s(varn)'
	local 0 `"`s(rest)'"'


		/* Step 3:
			Parse your syntax.
		*/

	syntax [if] [in] [, `myopts' noOFFset]

	if "`structural'" != "" & "`e(ar)'`e(ma)'" == "" {
		noi di in blue "structural has no effect without ARMA terms"
		local structu
	}
	if "`dynamic'" != ""  {
		if "`at'" != "" {
			di in red "options at() and dynamic() may not be "  /*
				*/ "specified together"
			exit 198
		}
		if "`e(aparch)'`e(parch)'`e(pgarch)'`e(nparch)'`e(nparchk)'" /*
		*/ != "" & ("`variance'" != "" | "`e(archm)'" != "") {
			di in red "dynamic predictions involving the "	/*
			*/ "conditional variance not available "
			di in red "for models with power terms."
			exit 198
		}
	}

	if "`at'" != "" {
		PrsAt `"`at'"'
		local at_e `r(e_val)'
		capture confirm numeric variable `r(s2_val)'
		if _rc {			/* may refer to lags */
			tempvar at_s2
			gen double `at_s2' = `r(s2_val)'
		}
		else	local at_s2 `r(s2_val)'
	}
	else	local doat *


		/* Step 4:
			Concatenate switch options together
		*/

	local type  `het'`residuals'`variance'`xb'`y'`yresiduals'
	local args


		/* Step 5:
			quickly process default case if you can 
			Do not forget -nooffset- option.
		*/

		/* Step 6:
			mark sample (this is not e(sample)).
		*/
	marksample touse

			/* Set up one-step and dynamic samples */
	_ts timevar panvar, sort panel
	qui tsset
	markout `touse' `timevar' `panvar'
	tempname touseS touseD
	gen byte `touseS' = `touse'
	gen byte `touseD' = 0
	if "`t0'" != "" { 
		qui replace `touseS' = 0 if `timevar' < `t0' 
		local t0opt "t0(`t0')"
	}
	if "`dynamic'" != "" { 
		qui replace `touseD' = `touseS' & `timevar' >= `dynamic' 
		local dynopt "dyn(`dynamic')"
	}

			/* predicted variable labeling */

	if "`structural'" != "" { 
		local postlab `structural' 
	}
	if "`dynamic'" != "" { 
		local postlab = ltrim("`postlab' `dynopt'") 
	}
	else	local postlab = ltrim("`postlab' one-step")
	if "`t0'" != "" { local postlab = ltrim("`postlab' t0(`t0')") }

		/* Step 7:
			handle options that take argument one at a time.
			Comment if restricted to e(sample).
			Be careful in coding that number of missing values
			created is shown.
			Do all intermediate calculations in double.
		*/

	/* Get type(s) of model based on saved results */

	if "`e(doarch)'" == "*"	  	 { local doarch  * }
	if "`e(mhet)'" == ""      	 { local dohet   * }
	if "`e(earch)'" == ""     	 { local doearch * }
	if "`e(egarch)'" == ""    	 { local doegrch * }
	if "`e(earch)'`e(egarch)'" == "" { local doeaeg  * }
	if "`e(archm)'" == ""     	 { local doarchm * }
	if "`e(archmexp)'" == ""  	 { local darchme * }
	if "`e(tarch)'" == ""     	 { local dotarch * }
	if "`e(ar)'`e(ma)'" == "" 	 { local doarma  * }
	if e(power) >= .          	 { local dopowa  * }
	if "`e(abarch)'`e(atarch)'`e(sdgarch)'" == "" { local doab * }
	if "`e(sdgarch)'" == ""   	 { local dosd    * }
	if "`e(atarch)'" == "" 		 { local dotabse * }
	if "`e(parch)'`e(pgarch)'`e(tparch)'" == "" { local dopow   * }


	/* Get beta and restripe it for scoring */

	tempname b
	mat `b' = e(b)
	local origeqs : coleq `b'
	mat coleq `b' = `e(eqnames)'


	/* Get x*beta (if there is an equation for it) */
	/* this xb does not recurse if depvar is in rhs of model */
	tempvar xb T

	gettoken xeqnam : origeqs
	if "`xeqnam'" == "ARCH" | "`xeqnam'" == "ARCHM"			/*
		*/ | "`xeqnam'" == "ARMA" | "`xeqnam'" == "HET"		/*
		*/ | "`xeqnam'" == "SIGMA2"  | "`xeqnam'" == "POWER" {
		qui gen byte `xb' = 0 
		local doxb *

		local nms : colfullnames `b'
	}
	else {
		qui _predict double `xb', xb `offset'

		tempname b1		/* must avoid var names later */
		capture mat `b1' = `b'[1,"`e(depvar)':"]
		if _rc { mat `b1' = `b'[1,"eq1:"] }
		local nms1 : colfullnames `b1'
		mat `b1' = `b'[1,colsof(`b1')+1...]
		local nms : colfullnames `b1'
	}

	/* We need a sigma2 variable regardless of whether doarch is `*' */
	tempvar sigma2 tvar
	qui gen double `sigma2' = .
	qui gen double `tvar' = .

	if "`dohet'" == "" {
		tempvar xbhet
		qui mat score `xbhet' = `b', eq(HET)
		local hetterm + `xbhet'
		if "`e(earch)'`e(egarch)'" == "" {
			qui replace `xbhet' = exp(`xbhet')
			local hetdyn  update `sigma2' = `sigma2' + exp(`tvar')
		}
		else	local hetdyn  update `sigma2' = `sigma2' + `tvar'

		if "`doarch'" == "*" {
			qui replace `sigma2' = 0  if `touse'
		}
	}
	else if !e(archany) {
		qui capture qui mat `T' = `b'[1,"HET:"]
		if _rc {
			mat `T' = `b'[1,"SIGMA2:"]
		}
		qui replace `sigma2' = `T'[1,1]  if `touse'
		local dohet *
	}
	else {
		local dohet *
		if "`doarch'" == "*" {
			qui replace `sigma2' = 0  if `touse'
		}
	}

					/* NOTE, early processing of a 
					 * switch type */
	/* multiplicative heteroskedastic prediction */

	if "`type'" == "het" {
		if "`xbhet'" == "" {
			di in red "no multiplicative heteroskedasticity " /*
			*/ "component in model"
		}
		gen `vtyp' `varn' = `xbhet'
		label var `varn' "heteroskedasticity component"
		exit
	}


	tempvar te sarch te2 
	qui gen double `te'    =  . in 1
	qui gen double `sarch' = `sigma2'  if `touse'

	if e(archany) {
		qui gen double `te2'    =  . in 1
		local sig2_0  = e(archi)
		local sig_0 = sqrt(e(archi))
		local abse_0  = sqrt(2*`sig2_0'/_pi) 
		local lsig2_0 = ln(`sig2_0')
		`dopowa' local sigp_0  = `sig2_0'^(e(power)/2)
	}

	/* power parameter */

	if e(power) < . { local pow = e(power) }


	/* temporary variables */

	tempvar xbfin sigma abse tabse tarchme tarchm tz absz tarch 
	tempvar earch earcha lnsig2 egarch tu sigmap abse_p tabse_p

	qui gen double `xbfin' 		   = .
	qui gen double `sigma'             = .	    /* needed pretty often */
	`doab'  qui gen double `abse'      = .
	`dotabse' qui gen double `tabse'   = .
	`darchme' qui gen double `tarchme' = .
	qui gen double `tarchm'            = 0
	`doearch' qui gen double `tz'      = .
	`doearch' qui gen double `absz'    = .
	`dotarch' qui gen double `tarch'   = .

	`doeaeg'  qui gen double `earch'   = 0
	`doeaeg'  qui gen double `earcha'  = 0
	`doegrch' qui gen double `lnsig2'  = .
	`doeaeg'  qui gen double `egarch'  = 0

	`dopowa'  qui gen double `sigmap'  = 0
	`dopow'   qui gen double `abse_p'  = .
	`dopow'   qui gen double `tabse_p' = .

	qui gen double `tu'                = .

	local normadj = sqrt(2/_pi)

	local nms `"`nms' "'
	local nms : subinstr local nms ".ar "     ".`tu' "     , all
	local nms : subinstr local nms ".ma "     ".`te' "     , all
	local nms : subinstr local nms ".arch "   ".`te2' "    , all
	local nms : subinstr local nms ".garch "  ".`sigma2' " , all
	local nms : subinstr local nms ".saarch " ".`te' "     , all

	`dotarch' local nms : subinstr local nms ".tarch "   ".`tarch' " , all

	`doab'    local nms : subinstr local nms ".abarch "  ".`abse' "   , all
	`dotabse' local nms : subinstr local nms ".atarch "  ".`tabse' "  , all
	`dosd'    local nms : subinstr local nms ".sdgarch " ".`sigma' "   , all

	`doearch' local nms : subinstr local nms ".earch_a " ".`absz' "  , all
	`doearch' local nms : subinstr local nms ".earch "   ".`tz' "    , all
	`doegrch' local nms : subinstr local nms ".egarch "  ".`lnsig2' ", all

	`doarchm' local nms : subinstr local nms ".sigma2 "  ".`sigma2' ", all
	`darchme' local nms : subinstr local nms "ARCHM:sigma2ex"  /*
			*/ "ARCHM:`tarchme'"   , all
	`doarchm' local nms : subinstr local nms "ARCHM:sigma2"  /*
			*/ "ARCHM:`sigma2'"   , all

	`dopow' local nms : subinstr local nms ".pgarch "  ".`sigmap' "   , all
	`dopow' local nms : subinstr local nms ".parch "   ".`abse_p' "   , all
	`dopow' local nms : subinstr local nms ".tparch "  ".`tabse_p' "  , all

	if "`at'" != "" {
		local nms : subinstr local nms ".`sigma2' "  ".`at_s2' " , all
	}

	mat colnames `b' = `nms1' `nms'

	if "`structural'" != "" { clrARMA `b' }		/* clear ARMA coefs */

						/* arch in mean expressions */
	`darchme' local archmx `e(archmexp)'
	`darchme' local archmx : subinstr local archmx "X" "`sigma2'", all

						/* build update expressions */
							/* aparch and aarch */
	local powterm 0
	`dopow' local powterm `tvar'

	if "`e(aparch)'" != "" {
	    aparchEx aparchx : `b' `sigmap' `sigma2' `powterm' `sig_0' `te' 0 1
	}
	if "`e(aarch)'" != "" {
	    aparchEx aarchx  : `b' `sigma2' `sigma2'  0        `sig_0' `te' 0 0
	}
							/* narch and narchk   */
							/* nparch and nparchk */
	if "`e(nparch)'" != "" {
	     narchEx nparchx : `b' `sigmap' `sigma2' `powterm' `sig_0' `te' 0 1
	}
	if "`e(narch)'" != "" {
	     narchEx narchx  : `b' `sigma2' `sigma2'  0        `sig_0' `te' 0 0
	}

	if "`e(aparch)'`e(narch)'" == "" & "`dopow'" == "" {
		local aparchx update `sigmap' = `sigma2' + `tvar'
	}

	if "`doarma'" != "*" {			/*   ARMA term */
		tempvar tu_hat
		qui gen double `tu_hat' = . in 1
		local tuhterm " + `tu_hat'" 
		local tuterm " + `tu'" 
	}
	else	local tu_hat 0

				/* Perform computations, all 
				 * computation performed regardless
				 * of option */

					/* One-step ahead projections */

	qui _byobs {
		`doarch' score `sigma2' = `b', eq("ARCH") missval(`sig2_0')
		`dohet' update `sigma2' = `sigma2' `hetterm'
		`doab' score `tvar' = `b', eq("SDARCH") missval(`abse_0')
		`doab' update `sigma2' = `sigma2' + `tvar'^2

		`aarchx'
		`narchx' 

		`doearch' score `earch'  = `b', eq("EARCH") missval(0)
		`doearch' score `earcha' = `b', eq("EARCHa") missval(0)
		`doegrch' score `egarch' = `b', eq("EGARCH") missval(`lsig2_0')
	     `doeaeg' update `sigma2' = exp(`sigma2'+`egarch'+`earch'+`earcha')

		`dopow' score `tvar' = `b', eq("PARCH") missval(`sigp_0')
		`aparchx'
		`nparchx'
		`dopowa' update `sigma2' = `sigmap'^(2/e(power)) 

		`doegrch' update `lnsig2' = ln(`sigma2')

		`doat'`doegrch' update `lnsig2' = ln(`at_s2')
		`doat'`dopowa' update `sigmap' = `at_s2'^(e(power)/2)

		`darchme' update `tarchme' = `archmx'
		`doarchm' score `tarchm' = `b', eq("ARCHM") missval(`sig2_0')
	
		update `tu' = `e(depvar)' - `xb' - `tarchm'

		`doarma' score  `tu_hat' = `b', eq("ARMA") missval(0)
		update `xbfin' = `xb' + `tarchm' `tuhterm'
		update `te' = `tu' - `tu_hat'
		`doat' update `te' = `at_e'

		`doearch' update `tz'   = `te'/sqrt(`sigma2')
		`doat'`doearch' update `tz'   = `te'/sqrt(`at_s2')
		`doearch' update `absz' = abs(`tz') - `normadj'

		`doarch'  update `te2'   = `te'^2

⌨️ 快捷键说明

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