---
title: "Nasdaq Baltic Benchmark index"
author: "Hele-Liis Peedosk"
date: "18 aprill 2019"
output: html_document
---

# Load libraries and functions. Run h2o.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r libraries} 
source("functions.R")
load_libraries()
```

```{r}
# Run 'h2o' interface
init_h2o(install=FALSE)
# h2o.shutdown(prompt=FALSE)
```


# Initialize results table
```{r}
results=init_results()
```


# Read in the data
```{r}    
data_dir <- "C:/Users/helts/Desktop/Masters thesis/R"
fname <- file.path(data_dir, "NBI_daily.csv")
data_NBID = read.csv(fname, header=TRUE, sep=";", dec=",") 
data_NBID = cbind(data_NBID[,1:2])
colnames(data_NBID)=c("date", "value")

# Change the date from factor type to date type, add columns month, weekday.
dates<-data_NBID$date
data_NBID$date=as.Date(dates, format="%d.%m.%Y")
```


```{r}
# Create tibble table with original data
trend=FALSE
diff=TRUE
tbl_data=create_tbl(data_NBID, remove_trend=trend, difference = diff)
ELD=tbl_data$data
data=tbl_data$tbl
data_orig=tbl_data$tbl_orig
```


# Draw a graph of the time series
```{r}
source("functions.R")
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)
latex_graph_df(series=df_orig, subseries=df_orig[df_orig$index>'2018-07-31',], y_lab="Euros", y_axis_lab_scale=waiver())
```


# Define the variables for this time series
```{r}
# History to be used

seasonality=7
frequency=365

# Choose input size, horizon, hidden layers, seed, epochs, activation_function
seed = 1
epochs = 50
activation_function = "Rectifier"

# Information for results table
data_name = "NBID"
rolling = FALSE
comments = "-"
history_start = "Whole history"


#Choose whether to use validation set
val_split = 0.8
val_set = TRUE

order=NULL #c(1,1,0) 
seasonal=NULL #list(order=c(0,0,0), period=seasonality)

```



# Determine manual grid

```{r}
# Grid search parameters
inputs=c(7, 14, 28, 35, 42)
horizons = c(1, 3, 7, 14)        
hidden_layers =list(c(2), c(4),c(6), c(7), c(9), c(3),c(5), c(8), c(10), c(10,10), c(20), c(100))
ensemble_sizes =c(10)
```


# Develop neural network models 

```{r}
df=train_val_test_split(data,val_split=val_split, test_split = 0.85)
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)

for (n_output in horizons){
  
  for (n_input in inputs){

       tryCatch({      # In case the input horizon is too long.
          # Generate data with certain colnames, extract data, X colnames, y colnames
          io_df<-gen_input_output(df, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL)
          io_df_orig<-gen_input_output(df_orig, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL, standardize=FALSE)
        
          for (ensemble_size in ensemble_sizes){
              for (hidden in hidden_layers){
                
                  if (sum(hidden)>=20) model_name="h2o: big" else model_name="h2o: small"
                  tryCatch({      # In case of some error with the model
                  train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name
                             , train_predict_model = train_predict_h2o_sep
                             , package="h2o", comments="multi NN", rolling=rolling, history_start=history_start
                             , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL
                             , io_df_orig=io_df_orig
                             , write_results_path= "", diff=diff, trend=trend)
                  },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                       , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})


                  tryCatch({      # In case of some error with the model
                  train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name
                             , train_predict_model = train_predict_h2o_rec
                             , package="h2o", comments=paste(comments, "iterative", sep=", "), rolling=rolling, history_start=history_start
                             , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL
                             , io_df_orig=io_df_orig
                             , write_results_path= "", diff=diff, trend=trend)
                  },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                       , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})

                  tryCatch({      # In case of some error with the model
                      train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name
                                 , train_predict_model = train_predict_neuralnet
                                 , package="neuralnet", comments="direct", rolling=rolling, history_start=history_start
                                 , diff=diff, trend=trend
                                 , mod_fn=mod_fn_h2o_reg, model_name="neuralnet", print_info=TRUE, hyper_params=NULL
                                 , io_df_orig=io_df_orig
                                 , write_results_path= "")
                  },error=function(e){cat("ERROR: Cannot handle the neuralnet model with horizon=",n_output
                                           , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})
 


          }}



          tryCatch({      # In case of some error with the model
          train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_arima
                     , package="ARIMA", comments, rolling, history_start
                     , mod_fn=NULL, model_name="SARIMA", print_info=TRUE, hyper_params=NULL
                     , seasonality=seasonality, io_df_orig=io_df_orig
                     , order=order, seasonal=seasonal
                     , diff=diff, trend=trend, val_set=TRUE
                     , write_results_path= "")

                    },error=function(e){cat("ERROR: Cannot handle arima model with horizon =",n_output
                                         , "and lookback=",n_input, "\n")})
          
          tryCatch({      # In case of some error with the model
          train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_rwf
                     , package="Naive", comments, rolling, history_start
                     , mod_fn=NULL, model_name="snaive", print_info=TRUE, hyper_params=NULL
                     , seasonality=seasonality, io_df_orig=io_df_orig
                     , write_results_path= data_dir, diff=diff, trend=trend)
          },error=function(e){cat("ERROR: Cannot handle naive model with horizon =",n_output
                               , "and lookback=",n_input, "\n")})


      },error=function(e){cat("ERROR: Cannot handle input-output split with horizon =",n_output
                           , "and lookback=",n_input, "\n")})

  }}
results
```

