This short tutorial teaches you how to tune parameters of univariate garch models. The function used for this type of models is the garch_reg() function and the parameters that you can tune are the following:

  • arch_order: An integer giving the order of the ARCH part for the variance model.

  • garch_order: An integer giving the order of the GARCH part for the variance model.

  • ar_order: An integer giving the order of the autoregressive part for the mean model.

  • ma_order: An integer giving the order of the moving averages part for the mean model.

Example

First, we load the packages:

We will use the rIBM dataset which is a tibble containing a date column and another column with the daily returns for the IBM company from 2007 to 2020. Extenderemos este dataset en el horizonte temporal tres fechas We will extend this dataset over the forecast horizon three dates and create our training and future dataset.

rIBM
#> # A tibble: 3,523 x 2
#>    date       daily_returns
#>    <date>             <dbl>
#>  1 2007-01-03      0.000926
#>  2 2007-01-04      0.0107  
#>  3 2007-01-05     -0.00905 
#>  4 2007-01-08      0.0152  
#>  5 2007-01-09      0.0118  
#>  6 2007-01-10     -0.0118  
#>  7 2007-01-11     -0.00243 
#>  8 2007-01-12      0.00699 
#>  9 2007-01-16      0.0149  
#> 10 2007-01-17     -0.00793 
#> # ... with 3,513 more rows
rIBM_extended <- rIBM %>%
    future_frame(.length_out = 3, .bind_data = TRUE) 

rIBM_train  <- rIBM_extended %>% drop_na()
rIBM_future <- rIBM_extended %>% filter(is.na(daily_returns))

Next, we define the model specification and mark which parameters we are going to tune. It is also necessary to use the tune_by argument in which we must specify either sigmaFor or seriesFor to refer to the series forecasts or sigma forecasts (based on these forecasts will be used in the tuning process and the metrics will be calculated).

# Model Spec
model_spec <-garchmodels::garch_reg(mode = "regression",
                                    arch_order = tune(),
                                    garch_order = tune(),
                                    tune_by = "sigmaFor") %>%
    set_engine("rugarch") 

The next step is to create a recipe in which we specify the formula we will use:

# Recipe spec
recipe_spec <- recipe(daily_returns ~ date, data = rIBM_train) 

We put everything together in a workflow:

# Workflow
wflw <- workflow() %>%
    add_recipe(recipe_spec) %>%
    add_model(model_spec)

Next, we need to generate the resamples. As it is a time series problem, the order is important in our observations so we will use the timetk function and visualize:

resamples <- timetk::time_series_cv(rIBM_train, 
                                    date_var = date, 
                                    initial = "6 years", 
                                    assess = "24 months",
                                    skip = "24 months",
                                    cumulative = TRUE,
                                    slice_limit = 3)

timetk::plot_time_series_cv_plan(resamples, .date_var = date, .value = daily_returns)