我的教学
R工具包名字 rugarch
工具包的www.github.com网址
https://github.com/cran/rugarch
工具包PDF手册网址
https://cran.r-project.org/web/packages/rugarch/rugarch.pdf
模型数学公式
In GARCH models, the density function is usually written in terms of the
location and scale parameters, normalized to give zero mean and unit variance,
αt=(μt,σt,ω),
where the conditional mean is given by
μt=μ(θ,xt)=E(yt∣xt),
and the conditional variance is,
σt2=σ2(θ,xt)=E((yt−μt)2∣xt),
with ω=ω(θ,xt) denoting the remaining parameters of
the distribution, perhaps a shape and skew parameter. The conditional mean and
variance are used to scale the innovations,
zt(θ)=σ(θ,xt)yt−μ(θ,xt),
having conditional density which may be written as,
g(z∣ω)=dzdP(zt<z∣ω),
and related to f(y∣α) by,
f(yt∣μt,σt2,ω)=σt1g(zt∣ω).
The rest of this section discusses the various flavors of GARCH implemented in
the package, while Section \ref{section:distributions} discusses the
distributions implemented and their standardization for use in GARCH processes.
\subsubsection{The standard GARCH model (‘sGARCH’)}\label{section:sgarch}
The standard GARCH model (\cite{Bollerslev1986}) may be written as:
σt2=(ω+j=1∑mζjvjt)+j=1∑qαjεt−j2+j=1∑pβjσt−j2,
with σt2 denoting the conditional variance, ω the intercept
and εt2 the residuals from the mean filtration process discussed
previously. The GARCH order is defined by (q,p) (ARCH, GARCH), with possibly
\verb@m@ external regressors vj which are passed \emph{pre-lagged}.
If variance targeting is used, then ω is replaced by,
σˉ2(1−P^)−j=1∑mζjvˉj
where σˉ2 is the unconditional variance of ε2 which
is consistently estimated by its sample counterpart at every iteration of the
solver following the mean equation filtration, and vˉj represents the
sample mean of the jth external regressors in the variance equation
(assuming stationarity), and P^ is the persistence and defined below. If a
numeric value was provided to the \emph{variance.targeting} option in the specification
(instead of logical), this will be used instead of σˉ2 for the
calculation.\footnote{Note that this should represent a value related to the variance
in the plain vanilla GARCH model. In more general models such as the APARCH, this is
a value related to σδ, which may not be obvious since δ is not
known prior to estimation, and therefore care should be taken in those cases.
Finally, if scaling is used in the estimation (via the fit.control option), this value will
also be automatically scale adjusted by the routine.}
One of the key features of the observed behavior of financial data which GARCH
models capture is volatility clustering which may be quantified in the
persistence parameter P^. For the ‘sGARCH’ model this may be calculated as,
P^=j=1∑qαj+j=1∑pβj.
Related to this measure is the ‘half-life’ (call it h2l) defined as the number
of days it takes for half of the expected reversion back
towards E(σ2) to occur,
h2l=logeP^−loge2.
Finally, the unconditional variance of the model σ^2, and related
to its persistence, is,
σ^2=1−P^ω^,
where ω^ is the estimated value of the intercept from the GARCH model.
The naming conventions for passing fixed or starting parameters for this model
are:
\begin{itemize}
\item ARCH(q) parameters are ‘alpha1’, ‘alpha2’, …,
\item GARCH§ parameters are ‘beta1’, ‘beta2’, …,
\item variance intercept parameter is ‘omega’
\item the external regressor parameters are ‘vxreg1’, ‘vxreg2’, …,
\end{itemize}
简单的代码
library(rugarch)
require(xts)
require(parallel)
data(sp500ret)
spx = as.xts(sp500ret)
nn = nrow(spx)
nx = nn-round(0.9*nn,0)
if(nx h = (nx/50)-1
indexin = lapply(1:h, function(j){ tail(seq(1,(nn-nx)+j*50, by=1),250) }) indexout = lapply(indexin, function(x){ (tail(x,1)+1):(tail(x,1)+50) })
cl = makePSOCKcluster(5)
mod = arfimacv(spx, indexin, indexout, ar.max = 2, ma.max = 2, criterion = c("rmse","mae","berkowitzp")[1], berkowitz.significance = 0.05, arfima = FALSE, include.mean = NULL, distribution.model = "norm", cluster = cl, external.regressors = NULL, solver = "solnp")
stopCluster(cl)
展望
其一 前几天论坛有朋友留言,希望我多在统计学板块发帖,所以今天来发帖一文;
其二 今天在《中国青年金融学者基地》群(去年发到论坛的群名字和群号码的,去年的中国数量金融群)做一个R源代码工具包开发的教学;同时给大家提到怎么把国际上作者提供的 /Vignettes 文件夹里的 .tex 文件(LATEX 格式的数学公式排版文件)里的数学公式提取到论坛的 markdown 发文模式。
首先, latex 的数学公式是
\begin
\sigma^2=\beta
\end{equantion}
来输入的,下面我用一个程序源代码的模式,输入markdown 的数学公式源代码。这里我也是上面那样的;
σ2=β
LATE 代码
$\sigma^2=\beta$
教学结束,论坛朋友的要约我会明年写帖子发。 因为今年后面三个月还有好几篇朋友的博士论文的代码。实在没时间,明年会把很多R的有趣代码和MATLAB 国际上最顶级的代码发出来,以及配合MARKDOWN 格式写出数学公式教学。
Yours : Daniel tulips liu (丹尼尔·郁金香·刘)