[Uwgarp-commits] r107 - in pkg/GARPFRM: . R demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 7 22:50:58 CET 2014


Author: rossbennett34
Date: 2014-03-07 22:50:57 +0100 (Fri, 07 Mar 2014)
New Revision: 107

Added:
   pkg/GARPFRM/demo/univariate_GARCH.R
Modified:
   pkg/GARPFRM/DESCRIPTION
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/garch11.R
Log:
Adding support for univariate GARCH models and demo

Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION	2014-03-06 01:12:08 UTC (rev 106)
+++ pkg/GARPFRM/DESCRIPTION	2014-03-07 21:50:57 UTC (rev 107)
@@ -12,6 +12,7 @@
     PerformanceAnalytics (>= 1.0.0)
 Suggests:
     quadprog
+    rugarch (>= 1.3.1)
 License: GPL
 Collate:
     'backTestVaR.R'

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-03-06 01:12:08 UTC (rev 106)
+++ pkg/GARPFRM/NAMESPACE	2014-03-07 21:50:57 UTC (rev 107)
@@ -5,17 +5,21 @@
 export(endingPrices)
 export(EWMA)
 export(fcstGarch11)
+export(forecast)
 export(garch11)
 export(getAlphas)
 export(getBetas)
 export(getCor)
 export(getCov)
+export(getFit)
+export(getSpec)
 export(getStatistics)
 export(hypTest)
 export(monteCarlo)
 export(plot.capm_mlm)
 export(plot.capm_uv)
 export(plotEndingPrices)
+export(uvGARCH)
 S3method(countViolations,xts)
 S3method(fcstGarch11,DCCfit)
 S3method(getAlphas,capm_mlm)
@@ -25,6 +29,8 @@
 S3method(getCor,corEWMA)
 S3method(getCov,covEWMA)
 S3method(getCov,varEWMA)
+S3method(getFit,uvGARCH)
+S3method(getSpec,uvGARCH)
 S3method(getStatistics,capm_mlm)
 S3method(getStatistics,capm_uv)
 S3method(hypTest,capm_mlm)

Modified: pkg/GARPFRM/R/garch11.R
===================================================================
--- pkg/GARPFRM/R/garch11.R	2014-03-06 01:12:08 UTC (rev 106)
+++ pkg/GARPFRM/R/garch11.R	2014-03-07 21:50:57 UTC (rev 107)
@@ -1,29 +1,54 @@
-#' GARCH(1,1)
+
+# Because our function is a wrapper around functions in the rugarch and
+# rmgarch packages, we need to give proper credit (i.e. citation("rugarch")
+# and citation("rmgarch"))
+
+# rugarch: univariate garch models
+# rmgarch: multivariate garch models
+
+# we need to support GARCH models for both univariate and multivariate data
+
+#' GARCH Models
 #' 
-#' Description of GARCH(1,1)
+#' This function is a basic wrapper of functions in the rugarch and rmgarch
+#' packages to specify and fit GARCH models. The rugarch and rmgarch packages
+#' provide functions to specify and fit a rich set of GARCH models. 
+#' The purpose of this function is to specify and fit a GARCH model while 
+#' abstracting away some complexities.
 #' 
-#' @param R GARCH(1,1)
+#' The rugarch package implements univariate garch models and the
+#' rmgarch package implements multivariate garch models. Univariate or 
+#' multivariate data is automatically detected and the appropriate GARCH model
+#' will be specified and fit.
+#' 
+#' For complete functionality of GARCH models, it is recommended to 
+#' directly use functions in the rugarch and rmgarch packages.
+#' 
+#' @param R xts object of asset returns
 #' @param model “sGARCH”, “fGARCH”, “eGARCH”, “gjrGARCH”, “apARCH” and “iGARCH” and “csGARCH”
 #' @param distribution.model. Valid choices are “norm” for the normal distibution, “snorm” for the skew-normal distribution, “std” for the student-t, “sstd” for the skew-student, “ged” for the generalized error distribution, “sged” for the skew-generalized error distribution, “nig” for the normal inverse gaussian distribution, “ghyp” for the Generalized Hyperbolic, and “jsu” for Johnson's SU distribution. 
 #' @export
 # By default we use UV N~GARCH(1,1) and Bollerslev for each series
 garch11 <- function(R, model = "sGARCH", distribution.model = "norm"){
-garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)), 
-                          variance.model = list(garchOrder = c(1,1), model = model), 
-                          distribution.model)
-
-# DCC specification: GARCH(1,1) for conditional cor
-nbColumns = ncol(R)
-dcc.garch11.spec = dccspec(uspec = multispec( replicate(nbColumns, garch11.spec) ), 
-                           dccOrder = c(1,1), distribution = "mvnorm")
-dcc.garch11.spec
-
-dcc.fit = dccfit(dcc.garch11.spec, data = R)
-class(dcc.fit)
-slotNames(dcc.fit)
-names(dcc.fit at mfit)
-names(dcc.fit at model)
-return(dcc.fit)
+  # if univariate data, load the rugarch package
+  # if multivariate data, load the rmgarch package
+  
+  garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)), 
+                            variance.model = list(garchOrder = c(1,1), model = model), 
+                            distribution.model)
+  
+  # DCC specification: GARCH(1,1) for conditional cor
+  nbColumns = ncol(R)
+  dcc.garch11.spec = dccspec(uspec = multispec( replicate(nbColumns, garch11.spec) ), 
+                             dccOrder = c(1,1), distribution = "mvnorm")
+  dcc.garch11.spec
+  
+  dcc.fit = dccfit(dcc.garch11.spec, data = R)
+  class(dcc.fit)
+  slotNames(dcc.fit)
+  names(dcc.fit at mfit)
+  names(dcc.fit at model)
+  return(dcc.fit)
 }
 
 #' Forecast GARCH(1,1)
@@ -47,4 +72,146 @@
   class(result at mforecast)
   names(result at mforecast)
   return(result)
-}
\ No newline at end of file
+}
+
+
+#' Univariate GARCH Model
+#' 
+#' Specify and fit a univariate GARCH model
+#' 
+#' @param R xts object of asset returns.
+#' @param model GARCH Model to specify and fit. Valid GARCH models are
+#' “sGARCH”, “fGARCH”, “eGARCH”, “gjrGARCH”, “apARCH”, “iGARCH” and “csGARCH”.
+#' @param garchOrder the ARCH(q) and GARCH(p) orders.
+#' @param armaOrder the autoregressive and moving average orders.
+#' @param distribution conditional density to use for the innovations. Valid 
+#' distributions are “norm” for the normal distibution, “snorm” for the 
+#' skew-normal distribution, “std” for the student-t, 
+#' “sstd” for the skew-student, “ged” for the generalized error distribution, 
+#' “sged” for the skew-generalized error distribution, 
+#' “nig” for the normal inverse gaussian distribution, 
+#' “ghyp” for the Generalized Hyperbolic, and “jsu” for Johnson's SU distribution.
+#' @param fixedParams named list of parameters to keep fixed.
+#' @param solver the solver to use to fit the GARCH model. Valid solvers are
+#' “nlminb”, “solnp”, “lbfgs”, “gosolnp”, “nloptr” or “hybrid” 
+#' @param outSample number of periods of data used to fit the model. 
+#' \code{nrow(R) - outSample} number of periods to keep as out of sample data
+#' points.
+#' @param fitControl named list of arguments for the fitting routine
+#' @param solverControl named list of arguments for the solver
+#' @return a list of length two containing GARCH specification and GARCH fit objects
+#' @export
+uvGARCH <- function(R, model="sGARCH", 
+                    garchOrder=c(1, 1), 
+                    armaOrder=c(1,1), 
+                    distribution="norm",
+                    fixedParams=NULL,
+                    solver="hybrid",
+                    outSample=0,
+                    fitControl=NULL,
+                    solverControl=NULL){
+  # Function to specify and fit a univariate GARCH model
+  
+  stopifnot("package:rugarch" %in% search() || require("rugarch", quietly = TRUE))
+  
+  if(is.null(fixedParams)){
+    fixedParams <- list()
+  }
+  
+  # Specify the GARCH model
+  # uGARCHspec object
+  spec <- ugarchspec(variance.model=list(model=model, garchOrder=garchOrder),
+                     mean.model=list(armaOrder=armaOrder),
+                     distribution.model=distribution,
+                     fixed.pars=fixedParams)
+  
+  # Fit the GARCH model
+  # uGARCHfit object
+  
+  if(is.null(fitControl)){
+    fitControl <- list(stationarity = 1, fixed.se = 0, scale = 0, rec.init = 'all')
+  }
+  
+  if(is.null(solverControl)){
+    solverControl <- list()
+  }
+  
+  fit <- ugarchfit(spec=spec, data=R, out.sample=outSample, solver=solver, 
+                   fit.control=fitControl, solver.control=solverControl)
+  
+  # structure and return the univariate GARCH model specification and fit
+  return(structure(list(spec=spec, fit=fit),
+                   class="uvGARCH"))
+}
+
+#' Get GARCH Specification
+#' 
+#' Function to extract the GARCH specification object 
+#' 
+#' @param garch a GARCH model specification and fit created with \code{uvGARCH}
+#' @return an object of class uGARCHspec
+#' @export
+getSpec <- function(garch){
+  UseMethod("getSpec")
+}
+
+#' @method getSpec uvGARCH
+#' @S3method getSpec uvGARCH
+getSpec.uvGARCH <- function(garch){
+  garch$spec
+}
+
+#' Get GARCH Model Fit
+#' 
+#' Function to extract the GARCH fit object 
+#' 
+#' @param garch a GARCH model specification and fit created with \code{uvGARCH}
+#' @return an object of class uGARCHfit
+#' @export
+getFit <- function(garch){
+  UseMethod("getFit")
+}
+
+#' @method getFit uvGARCH
+#' @S3method getFit uvGARCH
+getFit.uvGARCH <- function(garch){
+  garch$fit
+}
+
+#' Plot GARCH Model
+#' 
+#' Plots for fitted GARCH Models
+#' 
+#' @param x uvGARCH object create via \code{uvGARCH}
+#' @param y
+#' @param \dots additional parameters passed to plot method for uGARCHfit objects
+#' @param which plot selection
+plot.uvGARCH <- function(x, y, ..., which){
+  plot(getFit(x), which=which, ...=...)
+}
+
+#' Forecast Univariate GARCH Models
+#' 
+#' Forecasting for GARCH models fit via \code{uvGARCH}
+#' 
+#' @note For rolling forecasts specified with the \code{nRoll} argument, the
+#' GARCH model must be fit with \code{outSample} argument greater than or 
+#' equal to \code{nRoll}.
+#' 
+#' @param garch GARCH model fit via \code{uvGARCH}
+#' @param nAhead number of steps ahead to forecast
+#' @param nRoll number of rolling forecasts
+#' @param externalForecasts named list of external regressors in the mean and/or
+#' variance equations
+#' @param \dots additional parameters passed to \code{ugarchforecast}
+#' @return a uGARCHforecast object with the GARCH forecast data
+#' @export
+forecast <- function(garch, nAhead=10, nRoll=0, externalForecasts=NULL, ...){
+  
+  if(is.null(externalForecasts)){
+    externalForecasts <- list(mregfor = NULL, vregfor = NULL)
+  }
+  out <- ugarchforecast(garch$fit, n.ahead=nAhead, n.roll=nRoll, 
+                        external.forecasts=externalForecasts, ...=...)
+  return(out)
+}

Added: pkg/GARPFRM/demo/univariate_GARCH.R
===================================================================
--- pkg/GARPFRM/demo/univariate_GARCH.R	                        (rev 0)
+++ pkg/GARPFRM/demo/univariate_GARCH.R	2014-03-07 21:50:57 UTC (rev 107)
@@ -0,0 +1,78 @@
+library(GARPFRM)
+
+# Load weekly returns datasets
+data(crsp_weekly)
+
+# Use the MSFT (Microsoft) returns for univariate GARCH modeling
+R <- largecap_weekly[,"MSFT"]
+
+# Specify and fit the MSFT returns to a standard ARMA(0,0)-GARCH(1,1) model
+model0 <- uvGARCH(R, armaOrder=c(0,0))
+
+# The uvGARCH function uses the excellent rugarch packages, which has a rich
+# set of functions for analysis of fitted GARCH models. The fitted model can 
+# be extracted with the getFit function. Refer to help("uGARCHfit-class") 
+# for available all methods for the uGARCHfit object that is returned by getFit.
+# Here we can extract the GARCH model specification and fit
+spec <- getSpec(model0)
+spec
+
+fit <- getFit(model0)
+fit
+
+slotNames(fit)
+names(fit at fit)
+
+# Here are a few functions to extract parameter estimates
+coef(fit)
+head(fitted(fit))
+head(residuals(fit))
+head(sigma(fit))
+
+# 12 different plots are available. The user will be prompted to make a plot 
+# or can specify a plot with the which argument
+plot(fit)
+plot(fit, which=1)
+
+# Forecast 10 periods ahead using the standard ARMA(0,0)-GARCH(1,1) model 
+forecast1 <- forecast(model0, nAhead=10)
+forecast1
+plot(forecast1)
+
+# Note that to do rolling forecasts, the outSample argument in uvGARCH must
+# be greater than or equal to the nRoll argument specified in the forecast
+# function.
+
+# Here we specify the model with outSample=100 so that we can keep the last 100
+# data points available for out of sample testing and can call the forecast
+# method with the nRoll argument specified.
+model11 <- uvGARCH(R, outSample=100)
+forecast2 <- forecast(model11, nRoll=10)
+plot(forecast2)
+plot(forecast2, which=2)
+
+# Several distributions are available for the innovations. Distributions include:
+# “norm”: normal distibution
+# “snorm”: skew-normal distribution
+# “std”: student-t distribution
+# “sstd”: skew-student distribution
+# “ged”: generalized error distribution
+# “sged”: skew-generalized error distribution
+# “nig”: normal inverse gaussian distribution
+# “ghyp”: Generalized Hyperbolic distribution
+# “jsu”: Johnson's SU distribution.
+
+# Here we specify and fit the MSFT returns to a standard ARMA(0,0)-GARCH(1,1) 
+# model with student-t innovations
+model0.std <- uvGARCH(R, armaOrder=c(0,0), distribution="std")
+
+# The default arguments for uvGARCH are to specify and fit a standard 
+# ARMA(1,1)-GARCH(1,1) model
+model11 <- uvGARCH(R)
+
+# In addition to specifyin the model with different ar and ma orders, the 
+# ARCH(q) and GARCH(p) orders can also be specified. Here we fit a standard
+# ARMA(1,1)-GARCH(2,10 model
+model21 <- uvGARCH(R, garchOrder=c(2,1))
+getSpec(model21)
+getFit(model21)



More information about the Uwgarp-commits mailing list