[R] VECM - GARCH using maxLik()
Arne Henningsen
arne.henningsen at googlemail.com
Wed Feb 9 16:44:11 CET 2011
On 9 February 2011 16:21, Philipp Grueber <philipp.grueber at ebs.edu> wrote:
> 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)
It is because, your loglik() function returns NAs with the initial values:
> loglik(c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
[1] 0.000000 -1.281288 -1.129472 -1.897470 -2.009189 -1.603896 -1.422840
[8] -1.295774 -1.372789 -1.386749 -1.180154 -2.296334 -1.497573 -1.304747
[15] -1.728566 -2.013113 -1.550148 -1.331233 -1.619898 -1.474260 -1.257694
[22] -2.639963 -1.551604 -1.729651 -1.439579 -2.336881 -2.140721 -1.789380
[29] -1.589067 -1.366046 -1.237783 -1.122181 -1.047587 -1.426377 -1.202710
[36] -1.300351 -1.298771 -1.217437 -1.096333 -3.968950 -1.889532 -1.914252
[43] -1.647921 -1.792587 -2.206678 -1.812484 -1.637345 -1.847598 -1.793146
[50] -1.492001 -1.289086 -1.210335 -1.824306 -1.580044 -1.350120 -3.977663
[57] -2.213723 -2.022477 -1.704629 -1.599071 -1.789774 -1.421933 -2.004710
[64] -1.645671 -1.544476 -1.371070 -2.947443 -1.845836 -1.840481 -1.664951
[71] -1.564801 -1.327754 -1.463221 -1.230692 -1.099063 -2.188419 -1.395483
[78] -3.801520 -1.934664 -1.641021 -1.508269 -1.670946 -1.762663 -1.496547
[85] -1.836904 -1.855429 -1.850838 -1.883408 -1.808358 -1.487548 -1.893714
[92] -1.794318 -1.552698 -1.367103 -1.223706 -1.152354 -1.145350 -2.550406
[99] NA NA
Best wishes from Copenhagen,
Arne
--
Arne Henningsen
http://www.arne-henningsen.name
More information about the R-help
mailing list