[R-SIG-Finance] Independence test rugarch package
alexios ghalanos
alexios at 4dscape.com
Sat Apr 26 01:38:02 CEST 2014
Hi,
Please download latest version from bitbucket, re-test and report back.
I've made some changes to the way the test is calculated in order to
avoid problems when N is large (leading ot NaN or -Inf).
To download use the following code:
require(devtools)
install_bitbucket("rugarch","alexiosg")
Alexios
On 25/04/2014 20:36, philippe wrote:
> Dear contributors, dear alexhios
>
> I am trying to get the log-likelihood ratio values of the independence test
> of christopherson (1988). I identified the problem when I received -Inf for
> the uncond. coverage test due to too small number and in the end the log of
> zero and thus, wanted to run the independence test solely. I have the CHF
> which is in the following file
> https://drive.google.com/folderview?id=0BwSzkfw3xUH3REsxRHprZEU1R00&usp=sharing
>
> I built a parametric VaR model with the normal distributions at 4 confidence
> levels. The problem arose with the following code of your package below
>
> I checked your code an fully agree with your computation, however I do not
> know why the T01 and T10 values are both zero and the T11 value is equal to
> N -> the which() function below in the code clearly indicates that there are
> definitely not 163 "ones" in VaR.ind followed by "ones"! However, I can't
> find the error in your code or the difference to the one of my slightly
> adapted to compute 4 p-values at a time!
>
> In advance, thanks for your support and a nice weekend! Best,
> philippe
>
> ############
> library(rugarch)
> ret <- CHF
> seq <- 2000 #alternatively 500, 1000, 1500
>
> #Normal Distribution VaR
> VaRdistn <- function(ret, prob=.05) {
> ans <- -qnorm(prob, mean=0, sd=sd(ret))
> signif(ans, digits=7)
> }
>
> ### VaR
> fun <- VaRdistn
> p <- c(0.05, 0.01, 0.005, 0.001) # confidence values for VaR
> act <- ret[(seq+1):length(ret)] # actual return
> VaRmatrix <- matrix(nrow=length(ret)-seq, ncol=length(p))
> for (i in 1:nrow(VaRmatrix) ) {
> VaRmatrix[i,]<- sapply(X=p, FUN=VaRdistn,ret=ret[i:(seq+i-1)] )
> colnames(VaRmatrix) <- c("0.05", "0.01", "0.005", "0.001")
> }
>
> #### RUGARCH package (getAnywhere(.LR.cc))
> VaR <- -VaRmatrix[,2]
> p <- 0.01
>
> VaR.ind = ifelse(act < VaR, 1, 0)
> N = sum(VaR.ind)
> TN = length(VaR.ind)
> T00 = sum(c(0, ifelse(VaR.ind[2:TN] == 0 & VaR.ind[1:(TN-1)] == 0, 1, 0)))
> T11 = sum(c(0, ifelse(VaR.ind[2:TN] == 1 & VaR.ind[1:(TN-1)] == 1, 1, 0)))
> T01 = sum(c(0, ifelse(VaR.ind[2:TN] == 1 & VaR.ind[1:(TN-1)] == 0, 1, 0)))
> T10 = sum(c(0, ifelse(VaR.ind[2:TN] == 0 & VaR.ind[1:(TN-1)] == 1, 1, 0)))
> T0 = T00 + T01
> T1 = T10 + T11
> pi0 = T01/T0
> pi1 = T11/T1
> pe = (T01 + T11)/(T0 + T1)
> stat.ind = -2 * log((1 - pe)^(T00 + T10) * pe^(T01 + T11)) +
> 2 * log((1 - pi0)^T00 * pi0^T01 * (1 - pi1)^T10 * pi1^T11)
> stat.ind
> which(VaR.ind == 1)
>
> ##### my code
> conf <- 0.95
> p <- c(0.05, 0.01, 0.005, 0.001)
> #fill vectors
> uc.LR <- cc.LR <- numeric(length(p))
> uc.pval <- cc.pval <- numeric(length(p))
> uc.statement <- cc.statement <- numeric(length(p))
> ind.LR <- ind.pval <- numeric(length(p))
> VaR.ind <- matrix(nrow=length(ret)-seq, ncol=length(p))
> N <- TN <- T00 <- T11 <- T01 <- T10 <- T0 <- T1 <- pi0 <- pi1 <- pe <-
> numeric(length(p))
> for(i in 1:length(p)){
> uc.LR[i] <- VaRTest(alpha = p[i], act, -VaRmatrix[,i], conf.level =
> conf)$uc.LRstat
> uc.pval[i] <- VaRTest(alpha = p[i], act, -VaRmatrix[,i], conf.level =
> conf)$uc.LRp
> VaR.ind[,i] = ifelse(act < -VaRmatrix[,i], 1, 0)
> N[i] = sum(VaR.ind[,i])
> TN[i] = length(VaR.ind[,i])
> T00[i] = sum(c(0, ifelse(VaR.ind[,i][2:TN[i]] == 0 & VaR.ind[,i][1:(TN[i]
> - 1)] == 0, 1, 0)))
> T11[i] = sum(c(0, ifelse(VaR.ind[,i][2:TN[i]] == 1 & VaR.ind[,i][1:(TN[i]
> - 1)] == 1, 1, 0)))
> T01[i] = sum(c(0, ifelse(VaR.ind[,i][2:TN[i]] == 1 & VaR.ind[,i][1:(TN[i]
> - 1)] == 0, 1, 0)))
> T10[i] = sum(c(0, ifelse(VaR.ind[,i][2:TN[i]] == 0 & VaR.ind[,i][1:(TN[i]
> - 1)] == 1, 1, 0)))
> T0[i] = T00[i] + T01[i]
> T1[i] = T10[i] + T11[i]
> pi0[i] = T01[i]/T0[i]
> pi1[i] = T11[i]/T1[i]
> pe[i] = (T01[i] + T11[i])/(T0[i] + T1[i])
> ind.LR[i] = -2 * log((1 - pe[i])^(T00[i] + T10[i]) * pe[i]^(T01[i] +
> T11[i])) +
> 2 * log((1 - pi0[i])^T00[i] * pi0[i]^T01[i] * (1 - pi1[i])^T10[i] *
> pi1[i]^T11[i])
> ind.pval[i] <- 1 - pchisq(ind.LR[i], df = 1)
> cc.LR[i] <- VaRTest(alpha = p[i], act, -VaRmatrix[,i], conf.level =
> conf)$cc.LRstat
> cc.pval[i] <- VaRTest(alpha = p[i], act, -VaRmatrix[,i], conf.level =
> conf)$cc.LRp
> }
> ind.LR
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Independence-test-rugarch-package-tp4689473.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
>
More information about the R-SIG-Finance
mailing list