[R] VECM - GARCH using maxLik()
Philipp Grueber
philipp.grueber at ebs.edu
Wed Feb 9 16:21:30 CET 2011
Dear all,
I am still relatively new to R & currently working with a large set of
(intraday) financial data (about 3.3m observ.). I wish to estimate a vector
error correction model similar to the following:
r = a_0 + a1*r_(t-1) + a2*r_(t-2) + a3*s_(t-1) + a4*s_(t-2) +
a5*c_(t-1) + e
s = a_0 + a1*s_(t-1) + a2*s_(t-2) + a3*r_(t-1) + a4*r_(t-2) +
a5*c_(t-1) + e
(where c=s-r)
Estimating both equations of the VECM individually using OLS, I discovered
ARCH effects in my errors. Thus, I wish to include a GARCH term. I tried to
work with the fGarch package which, as far as I could see, is pretty
inflexible in terms of model specification and, in particular, unable to
consider exogenous variables (i.e. my error correction term or, at a later
stage, a transition function etc.). I have read about the rgarch package but
have not found a way to specify the model I need.
Thus, a colleague and I tried to write our own code using maxLik() -- but we
ran into some problems.
PROBLEM:
The code below does not work, with the error being: “Return code 100:
Initial value out of range.” We have tried various (more or less realistic)
starting values and manipulated the loglik-function but the error has
persisted. In addition, in our attempts we used a for-loop to estimate sig2.
However, for my dataset, I prefer a solution without such loops because I
assume they would take a lot of time.
QUESTION:
Does anybody know how to fix the code? Any hints what the problem might be?
Maybe someone has already found an alternative way to estimate such a GARCH
model using R?
The following data has exactly the same format as the data I am working
with:
library(maxLik)
library(fGarch)
lag<-function(x,k){
c(rep(NA,k),x[1:(length(x)-k)])
}
r<- as.vector(garchSim(garchSpec(rseed = 1985), n = 102)[,1]) [3:102]
r_1<- lag(r,1)[3:102]
r_2<-lag(r,2) [3:102]
s<-rnorm(102)[3:102]
s_1<- lag(s,1)[3:102]
s_2<-lag(s,2) [3:102]
c_1<-lag(s-r,1) [3:102]
data<-as.matrix(cbind(r,r_1,r_2,s_1,s_2,c_1))
As in my original dataset, I lag every parameter individually -- even though
I know that in many situations, there is an easier way to lag the data. In
my original dataset, this is the result of lagging the time series on a
per-day basis. In other words: My dataset comprises multiple days of data
but I do not want to link the first observation of each day with the last
observation of the previous day. Therefore, it would be helpful if any
possible solution would refer to the above data format.
loglik <- function(param) {
res <- param[1]
a0 <- param[2]
a1 <- param[3]
a2 <- param[4]
a3 <- param[5]
a4 <- param[6]
a5 <- param[7]
omega <- param[8]
alpha <- param[9]
beta <- param[10]
res <- r–a0–a1*r_1–a2*r_2–a3*s_1–a4*s_2-a5*c_1
sig2 <- numeric(100)
ll <- numeric(100)
sig2[1] <- 0
for (i in 2:100) {
sig2[i] <- omega + alpha*res[i-1]^2 + beta*sig2[i-1]
ll[i] <- -1/2*log(2*pi*sig2[i]) - 1/2*res[i]^2/sig2[i]
}
ll
}
est <- maxLik(loglik, start=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
summary(est)
Any help is highly appreciated. Thank you very much in advance,
Philipp Grueber
____________
EBS Universitaet fuer Wirtschaft und Recht i.Gr.
FARE Department
Wiesbaden/ Germany
www.ebs.edu
-----
____________________________________
EBS Universitaet fuer Wirtschaft und Recht
FARE Department
Wiesbaden/ Germany
http://www.ebs.edu/index.php?id=finacc&L=0
--
View this message in context: http://r.789695.n4.nabble.com/VECM-GARCH-using-maxLik-tp3297453p3297453.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list