1

遵循一些关于 ARMA(1,1)-GARCH(1,1) 的标准教科书(例如 Ruey Tsay 的金融时间序列分析),我尝试编写一个 R 程序来估计 ARMA(1,1) 的关键参数-英特尔股票收益的 GARCH(1,1) 模型。由于某种随机原因,我无法破译我的 R 程序出了什么问题。R 包 fGarch 已经给了我答案,但是我的自定义函数似乎没有产生相同的结果。

我想构建一个 R 程序来帮助估计基线 ARMA(1,1)-GARCH(1,1) 模型。然后我想调整这个基线脚本以适应不同的 GARCH 变体(例如 EGARCH、NGARCH 和 TGARCH)。如果您能在这种情况下提供一些指导,我们将不胜感激。下面的代码是用于估计英特尔股票收益的 ARMA(1,1)-GARCH(1,1) 模型的 6 个参数的 R 脚本。无论如何,我很高兴知道您的想法和见解。如果您有类似的示例,请随时分享您在 R 中的现有代码。非常感谢提前。

艾米丽

# This R script offers a suite of functions for estimating  the volatility dynamics based on the standard ARMA(1,1)-GARCH(1,1) model and its variants.
# The baseline ARMA(1,1) model characterizes the dynamic evolution of the return generating process. 
# The baseline GARCH(1,1) model depicts the the return volatility dynamics over time.
# We can extend the GARCH(1,1) volatility model to a variety of alternative specifications to capture the potential asymmetry for a better comparison:
# GARCH(1,1), EGARCH(1,1),  NGARCH(1,1), and TGARCH(1,1).

options(scipen=10)

intel= read.csv(file="intel.csv")
summary(intel)

raw_data= as.matrix(intel$logret)

library(fGarch)
garchFit(~arma(1,1)+garch(1,1), data=raw_data, trace=FALSE)


negative_log_likelihood_arma11_garch11=
function(theta, data)
{mean =theta[1]
 delta=theta[2]
 gamma=theta[3]
 omega=theta[4]
 alpha=theta[5]
 beta= theta[6]

 r= ts(data)
 n= length(r)

 u= vector(length=n)
 u= ts(u)
 u[1]= r[1]- mean

 for (t in 2:n)
 {u[t]= r[t]- mean- delta*r[t-1]- gamma*u[t-1]}

 h= vector(length=n)
 h= ts(h)
 h[1]= omega/(1-alpha-beta)

 for (t in 2:n)
 {h[t]= omega+ alpha*(u[t-1]^2)+ beta*h[t-1]}

 #return(-sum(dnorm(u[2:n], mean=mean, sd=sqrt(h[2:n]), log=TRUE)))
 pi=3.141592653589793238462643383279502884197169399375105820974944592
 return(-sum(-0.5*log(2*pi) -0.5*log(h[2:n]) -0.5*(u[2:n]^2)/h[2:n]))
}


#theta0=c(0, +0.78, -0.79, +0.0000018, +0.06, +0.93, 0.01)
theta0=rep(0.01,6)
negative_log_likelihood_arma11_garch11(theta=theta0, data=raw_data)


alpha= proc.time()
maximum_likelihood_fit_arma11_garch11=
nlm(negative_log_likelihood_arma11_garch11,
    p=theta0,
    data=raw_data,
    hessian=TRUE,
    iterlim=500)
#optim(theta0, 
#      negative_log_likelihood_arma11_garch11,
#      data=raw_data,
#      method="L-BFGS-B",
#      upper=c(+0.999999999999,+0.999999999999,+0.999999999999,0.999999999999,0.999999999999,0.999999999999),
#      lower=c(-0.999999999999,-0.999999999999,-0.999999999999,0.000000000001,0.000000000001,0.000000000001),
#      hessian=TRUE)

# We record the end time and calculate the total runtime for the above work.
omega= proc.time()
runtime= omega-alpha
zhours = floor(runtime/60/60)
zminutes=floor(runtime/60- zhours*60)
zseconds=floor(runtime- zhours*60*60- zminutes*60)
print(paste("It takes ",zhours,"hour(s)", zminutes," minute(s) ","and ", zseconds,"second(s) to finish running this R program",sep=""))


maximum_likelihood_fit_arma11_garch11

sqrt(diag(solve(maximum_likelihood_fit_arma11_garch11$hessian)))
4

0 回答 0