📄 arch_p.ado
字号:
update `sigma' = sqrt(`sigma2')
`doat' update `sigma' = sqrt(`at_s2')
`dotarch' update `tarch' = (`te'>0) * `te2'
`doab' update `abse' = abs(`te')
`dotabse' update `tabse' = (`te'>0) * abs(`te')
`dopow' update `abse_p' = abs(`te')^e(power)
`dopow' update `tabse_p' = (`te' > 0) * abs(`te')^e(power)
} if `touseS' & !`touseD'
/* Dynamic projections */
if "`dynamic'" != "" {
tempname te_d xbdyn /* xbdyn to handle l#.y rhs */
qui gen double `te_d' = `te' /* for use in nonlin exps */
qui gen double `xbdyn' = `e(depvar)'
op_colnm `b' `e(depvar)' `xbdyn' /* for lagged depv */
/* build update expressions */
/* aparch and aarch */
if "`e(aparch)'" != "" {
aparchEx aparchx : `b' `sigmap' `sigma2' `powterm' /*
*/ `sig_0' `te_d' 1 1 `sigma'
}
if "`e(aarch)'" != "" {
aparchEx aarchx : `b' `sigma2' `sigma2' 0 /*
*/ `sig_0' `te_d' 1 0 `sigma'
}
/* narch and narchk */
/* nparch and nparchk */
if "`e(nparch)'" != "" {
narchEx nparchx : `b' `sigmap' `sigma2' `powterm' /*
*/ `sig_0' `te_d' 1 1 `sigma'
}
if "`e(narch)'" != "" {
narchEx narchx : `b' `sigma2' `sigma2' 0 /*
*/ `sig_0' `te_d' 1 0 `sigma'
}
qui _byobs {
`doxb' score `xb' = `b', eq(#1)
`doarch' score `sigma2' = `b', eq("ARCH") missval(`sig2_0')
`dohet' score `tvar' = `b', eq("HET")
`hetdyn'
`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')
`darchme' update `tarchme' = `archmx'
`doarchm' score `tarchm' = `b', /*
*/ eq("ARCHM") missval(`sig2_0')
`doarma' score `tu' = `b', eq("ARMA") missval(0)
update `xbdyn' = `xb' + `tarchm' `tuterm'
update `te' = 0
`doearch' update `tz' = 0 /* `te'/sqrt(`sigma2') */
`doearch' update `absz' = 0 /* E[|`tz'| - `normadj' */
`doarch' update `te2' = `sigma2'
update `sigma' = sqrt(`sigma2')
`dotarch' update `tarch' = .5*`sigma2'
`doab' update `abse' = sqrt(2*`sigma2'/_pi)
`dotabse' update `tabse' = 0.5*`abse'
`dopow' update `abse_p' = _Eabsk(0, `sigma', e(power))
`dopow' update `tabse_p' = 0.5*`abse_p'
} if `touseD'
qui replace `xbfin' = `xbdyn' if `touseD'
}
/* Step 8:
handle switch options that can be used in-sample or
out-of-sample one at a time.
Be careful in coding that number of missing values
created is shown.
Do all intermediate calculations in double.
*/
if "`type'"=="" | "`type'" == "xb" {
if "`type'" == "" {
di in gr "(option xb assumed; linear prediction)"
}
/*
This adjustment removed. It was included only for
comparison to another package that did not include
the arch-in-mean component in the prediction.
if "`dynamic'" == "" {
`doarchm' local am "- `tarchm'"
}
*/
gen `vtyp' `varn' = `xbfin' if `touse'
label var `varn' "xb prediction, `postlab'"
exit
}
if "`type'" == "y" {
drop `xb' /* borrow xb */
op_inv `e(depvar)' `xbfin', gen(`xb') `dynopt', /*
*/ if `touseS' | `touseD'
gen `vtyp' `varn' = `xb'
label var `varn' "y prediction, `postlab'"
exit
}
if "`type'" == "residuals" {
gen `vtyp' `varn' = `e(depvar)' - `xbfin'
label var `varn' "residual, `postlab'"
exit
}
if "`type'" == "yresiduals" {
drop `xb' /* borrow xb */
op_inv `e(depvar)' `xbfin', gen(`xb') `dynopt', /*
*/ if `touseS' | `touseD'
tsrevar `e(depvar)', list
gen `vtyp' `varn' = `r(varlist)' - `xb'
label var `varn' "residual, `postlab'"
exit
}
if "`type'" == "variance" {
gen `vtyp' `varn' = `sigma2' if `touse'
label var `varn' "conditional variance, `postlab'"
exit
}
/* Step 9:
handle switch options that can be used in-sample only.
Same comments as for step 8.
*/
/* Step 10.
Issue r(198), syntax error.
The user specified more than one option
*/
error 198
end
program define PrsAt, rclass
tokenize `0', parse(", ")
if "`2'" == "," {
local 2 `3'
local 3
}
if "`3'" != "" {
di in red `"invalid at(`0')"'
exit 198
}
local i 1
while `i' <= 2 {
capture confirm numeric variable `1'
if _rc {
capture confirm number `1'
if _rc {
if `i' != 2 | "``i''" != "." {
di in red "arguments to at() must be either numeric variables or a numbers"
exit 198
}
}
}
local i = `i' + 1
}
return local e_val `1'
return local s2_val `2'
end
program define clrARMA
args b
local str " str."
local i 1
tokenize `e(eqnames)'
while "``i''" != "" {
if "``i''" == "ARMA" { mat `b'[1,`i'] = 0 }
local i = `i'+1
}
end
program define aparchEx
args exp colon bfull target initial init2 sigma0 e_t dynamic ispow /*
*/ sigma
if `ispow' {
local P "P"
local pp "p"
}
tempname b
capture mat `b' = `bfull'[1, "A`P'ARCH:"]
if _rc {
c_local `exp'
exit
}
tempname T
local ex update `target' = `initial' + `init2'
if `ispow' {
local p `e(power)'
}
else local p 2
local cols : colnames `b'
tokenize `cols'
local i 1
SplitNam op sinam : "``i''"
while "`sinam'" == "a`pp'arch" {
local g = `b'[1,`i']
local j = colnumb(`b', "`op'.a`pp'arch_e")
local k = `b'[1,`j']
local expval0 = (`sigma0'^`p'*2^(`p'/2-1) /*
*/ / sqrt(_pi))*((1-`k')^`p'+(1+`k')^`p') /*
*/ * exp(lngamma((`p'+1)/2))
if `dynamic' {
local expval (`op'.`sigma'^`p'*2^(`p'/2-1) / /*
*/ sqrt(_pi))*((1-`k')^`p'+(1+`k')^`p') * /*
*/ exp(lngamma((`p'+1)/2))
local ex `ex' + `g' * /*
*/ cond(`op'.`e_t'>=., /*
*/ cond(`op'.`sigma'>=., /*
*/ `expval0', /*
*/ `expval' /*
*/ ), /*
*/ (abs(`op'.`e_t') + `k'*`op'.`e_t')^`p' /*
*/ )
}
else {
local ex `ex' + `g' * cond(`op'.`e_t'>=., `expval0', /*
*/ (abs(`op'.`e_t') + `k'*`op'.`e_t')^`p')
}
local i = `i' + 1
SplitNam op sinam : ``i''
}
c_local `exp' `ex'
end
program define narchEx
args exp colon bfull target initial init2 sigma0 e_t dynamic ispow /*
*/ sigma
if `ispow' {
local P "P"
local pp "p"
}
tempname b
capture mat `b' = `bfull'[1, "N`P'ARCH:"]
if _rc {
c_local `exp'
exit
}
tempname T
local ex update `target' = `initial' + `init2'
if `ispow' {
local p `e(power)'
}
else local p 2
local cols : colnames `b'
tokenize `cols'
local i 1
SplitNam op sinam : "``i''"
while "`sinam'" == "n`pp'arch" {
local g = `b'[1,`i']
capture local j = colnumb(`b', "`op'.n`pp'arch_k")
if `j' >= . { local j = colnumb(`b', "n`pp'arch_k") }
local k = `b'[1,`j']
local expval0 = _Eabsk(`k', `sigma0', `p')
if `dynamic' {
local expval _Eabsk(`k', `op'.`sigma', `p')
local ex `ex' + `g' * /*
*/ cond(`op'.`e_t'>=., /*
*/ cond(`op'.`sigma'>=., /*
*/ `expval0', /*
*/ _Eabsk(`k', `op'.`sigma', `p') /*
*/ ), /*
*/ abs(`op'.`e_t' - `k')^`p' /*
*/ )
}
else {
local ex `ex' + `g' * cond(`op'.`e_t'>=., `expval0', /*
*/ abs(`op'.`e_t' - `k')^`p')
}
local i = `i' + 1
SplitNam op sinam : ``i''
}
c_local `exp' `ex'
end
program define SplitNam /* assumes op.si */
args opnam sinam colon name
tokenize `name', parse(":.")
c_local `opnam' `1'
c_local `sinam' `3'
end
exit
Notes:
------
1) Other packages may not include the arch-in-mean component in
the (xb) prediction.
2) Other packages may include the arma component of the mean even when
producing the static structural arch (variance) prediction. This means
that the static structural arch prediction would be no different than
the static (nonstructural) arch prediction.
3) Other packages may not include the sqrt(2/_pi) term in the egarch
type models. This will cause the constant term in the arch part
of the model to be different. Also, it will affect forecasts.
4) In the dynamic loop, all of the references to terms including e_t have
been replaced with term's appropriate expected value.
----
+
-
data structure
archterm addsto expression E[expression]
-------- ------ ---------- -----------------------
aarch sigma2
aparch sigmap (abs(e_t) - aparch_e*e_t)^power f(aparch_e, power, sigma)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -