# Script para el ajuste de series temporales financieras con R. Consultar la ayuda de cada una # de las funciones para conocer las diferentes opciones. rm(list = ls()) setwd("G:/PedroTrabajo/USC/Docencia/Master-2010-2011/Proyectos") # Importar datos HSI <- read.csv2("G:/PedroTrabajo/USC/Docencia/Master-2010-2011/Proyectos/HSI.csv",header=TRUE,sep=",",dec=".",fill=TRUE) # Importar librerias necesarias para el análisis. Puede ser necesario instalar dichas librerias desde el CRAN library(moments) library(tseries) library(fBasics) library(fGarch) # Hay que instalar la libreria rgarch que se encuentra en la página web http://rgarch.r-forge.r-project.org/ # Para ello hay que guardar la libreria en un directorio y utilizar la opción Instalar paquetes a partir # de archivos zip locales. library(rgarch) # Gráfico de los rendimientos HSI[,1]<-as.Date(HSI[,1]) plot(HSI,type="l",xlab="Year",ylab="Log-returns of HSI") # Momentos mean(HSI[,2]) sd(HSI[,2]) skewness(HSI[,2]) kurtosis(HSI[,2]) # Estimacion kernel d1 = density(HSI[,2]) plot(d1$x,d1$y,type='l',lwd = 2, col="black") # ACF de los rendimientos acf(HSI[,2],main="ACF of log returns of HSI index") # Contraste Ljung-Box sobre los rendimientos Box.test(HSI[,2],lag=12,type="Ljung-Box") # ACF y PACF de los rendimientos al cuadrado acf(HSI[,2]^2,main="ACF of log returns of squared HSI index") pacf(HSI[,2]^2,main="PACF of squared log returns of Hang-Seng index") # Ljung-Box sobre los residuos at = HSI[,2] - mean(HSI[,2]) Box.test(at^2,lag=12,type="Ljung-Box") # Modelo ARCH ajusteARCH <- garchFit(HSI[,2] ~ garch(11,0), data = HSI[,2], trace = F) ajusteARCH ajusteARCH <- garchFit(HSI[,2] ~ garch(10,0), data = HSI[,2], trace = F) ajusteARCH # Modelo GARCH ajusteGARCH <- garchFit(HSI[,2] ~ garch(1,1), data = HSI[,2], trace = F) ajusteGARCH # Gráfico de la volatilidad estimada volatilityGARCH = matrix(NA,nrow=2488,ncol=2) volatilityGARCH[,1] = HSI[2:2489,1] volatilityGARCH[,2] = ajusteGARCH@h.t volatilityGARCH = as.data.frame(volatilityGARCH) volatilityGARCH[,1] = as.Date(volatilityGARCH[,1]) plot(volatilityGARCH,type="l",ylab="",xlab="Time") # Modelo IGARCH spec1 <- ugarchspec(variance.model = list(model = "iGARCH", garchOrder = c(1, 1), submodel = NULL, external.regressors = NULL, variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), include.mean = TRUE, garchInMean = FALSE, inMeanType = 1, arfima = FALSE, external.regressors = NULL), distribution.model = "norm", start.pars = list(), fixed.pars = list()) ajusteIGARCH <- ugarchfit(spec1,HSI[,2]) ajusteIGARCH # Modelo EGARCH spec1 <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(2, 1), submodel = NULL, external.regressors = NULL, variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), include.mean = TRUE, garchInMean = FALSE, inMeanType = 1, arfima = FALSE, external.regressors = NULL), distribution.model = "norm", start.pars = list(), fixed.pars = list()) ajusteEGARCH <- ugarchfit(spec1,HSI[,2]) ajusteEGARCH # Modelo TGARCH spec1 <- ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1), submodel = NULL, external.regressors = NULL, variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), include.mean = TRUE, garchInMean = FALSE, inMeanType = 1, arfima = FALSE, external.regressors = NULL), distribution.model = "norm", start.pars = list(), fixed.pars = list()) ajusteTGARCH <- ugarchfit(spec1,HSI[,2]) ajusteTGARCH