-
Notifications
You must be signed in to change notification settings - Fork 22
GarchSVR Example.
The GarchSVR function aiming provide an easy way to do the cross-validation for the Garch-SVR model. The Garch SVR model is described by Correia, P. (2016).
The first step to proceed with the GarchSVR is create a vector of returns based, for example, on the Closed Price of some stocks. In this example we worked with the Ibov Index. The R code is described as follow:
#Clean the R's workspace
rm(list=ls())
#Invoke the libraries
library(mlRFinance)
library(quantmod)
#New enviroment
stockData <- new.env()
#Specify the Time Series range
startDate = as.Date("2011-01-01")
endDate = as.Date("2011-12-31")
#Get the Time Series for IBOV Index
getSymbols("^BVSP", src="yahoo",from=startDate,to=endDate)After getting the results, the next step is calculate the return vector and also separate between training dataset and validating dataset.
#Computes the log-return
return <- na.omit(diff(log(Cl(BVSP))))
#Training set
train <- as.numeric(return [1:180])
#Validation set
valid <- as.numeric(return [181:216])The next step is to define the range of the parameters. This range will be used in the cross-validation procedure. The Garch SVR model needs two specification, one for the Mean Equation and another for the Volatility equation.
As stated in Correia, P. (2016) the Mean Equation considered is as follow:
And the Volatitility Equation is given by:
For each type of equation the user must define the type of the kernel, its parameters, costs and insensitive parameters (epsilon):
#Cost parameter - Mean Equation
Cmean<-seq(0.01,10)
#Epsilon parameter - Mean Equation
epsilonMean <-seq(0.04,0.5,length.out=7)
#Kernel mean equation
kernelMean <- "Gaussian"
#parameters kernel - Mean Equation
parmsMean <-matrix(c(1.0,
1.5,
2.0),ncol=1,nrow=3,byrow=T)
#Cost parameter - Volatility Equation
Cvola <-seq(0.5,0.7,length.out = 4)
#Epsilon parameter - Volatility Equation
epsilonVola <-seq(0.01,0.9,length.out=4)
#Kernel - Volatility Equation
kernelVolat <- "Polynomial"
#parameters kernel - Volatility Equation
parmsVola <- matrix(c(2,1,
1.5,5,
2.0,7),ncol=2,nrow=3,byrow=T)The last step is to invoke the GarchSVR function:
crossGarch<-GarchSVR(train,valid,Cmean,epsilonMean,kernelMean,parmsMean,
Cvola,epsilonVola,kernelVolat,parmsVola)The object crossGarch contains all parameters combinations and also measures of errors using the validation dataset and training using the train dataset.
Obs: Before run the code, It is important to proceed with some sensitive analysis to test the ideal range of parameters. For instance, if the user chooses a bad range, the algorithm will run for long time looking for the Near Positive Definite matrix for the kernel matrices and also trying to solve the Quadratic Programming.