@@ -1579,10 +1579,17 @@ if (!file.exists('data/fx/mse.fGARCH.ALLGARCH2.rds')) {
1579
1579
roll <- ugarchroll(spec, data = x, n.start = ns, forecast.length = n,
1580
1580
refit.every = 1, refit.window = 'moving',
1581
1581
cluster = .cl)
1582
- attributes(roll)$forecast$density %>%
1582
+
1583
+ if (!is.null(roll)) {
1584
+ res <- attributes(roll)$forecast$density %>%
1583
1585
tbl_df %>% mutate(MSE = mean((Mu - Realized)^2)) %>%
1584
1586
.$MSE %>% unique
1585
- }) %>% rename(MSE = V1)
1587
+ } else {
1588
+ res <- NULL
1589
+ }
1590
+ return(res)
1591
+ }) %>% tbl_df
1592
+ mse.fGARCH.ALLGARCH2 %<>% ddply(.(.id), summarise, MSE = mean((Mu - Realized)^2))
1586
1593
saveRDS(mse.fGARCH.ALLGARCH2, 'data/fx/mse.fGARCH.ALLGARCH2.rds')
1587
1594
1588
1595
} else {
@@ -1644,7 +1651,7 @@ for (dt in timeID) {
1644
1651
1645
1652
The default setting is ` forecast.length = 500, refit.every = 25, refit.window = 'recursive' ` .
1646
1653
1647
- ``` {r eGARCH , echo=FALSE}
1654
+ ``` {r mseeGARCH , echo=FALSE}
1648
1655
if (!file.exists('data/fx/mse.eGARCH.rds')) {
1649
1656
mse.eGARCH <- ldply(mbase, function(x) {
1650
1657
x <- Cl(x)
@@ -1699,7 +1706,7 @@ mse.eGARCH %>%
1699
1706
1700
1707
Set ` n.start = ns ` , ` forecast.length = nrow(x) - ns ` , ` refit.every = 1 ` , ` refit.window = 'moving' ` .
1701
1708
1702
- ``` {r eGARCH2 , echo=FALSE}
1709
+ ``` {r mseeGARCH2 , echo=FALSE}
1703
1710
if (!file.exists('data/fx/mse.eGARCH2.rds')) {
1704
1711
mse.eGARCH2 <- ldply(mbase, function(x) {
1705
1712
x <- Cl(x)
@@ -2438,28 +2445,46 @@ for (dt in timeID) {
2438
2445
2439
2446
The default setting is ` forecast.length = 500, refit.every = 25, refit.window = 'recursive' ` .
2440
2447
2441
- ``` {r models}
2442
- models <- list(sGARCH = mse.sGARCH,
2443
- fGARCH.GARCH = mse.fGARCH.GARCH,
2444
- fGARCH.TGARCH = mse.fGARCH.TGARCH,
2445
- fGARCH.AVGARCH = mse.fGARCH.AVGARCH,
2446
- fGARCH.NGARCH = mse.fGARCH.NGARCH,
2447
- fGARCH.NAGARCH = mse.fGARCH.NAGARCH,
2448
- fGARCH.APARCH = mse.fGARCH.APARCH,
2449
- fGARCH.GJRGARCH = mse.fGARCH.GJRGARCH,
2450
- fGARCH.ALLGARCH = mse.fGARCH.ALLGARCH,
2451
- eGARCH = mse.eGARCH,
2452
- gjrGARCH = mse.gjrGARCH,
2453
- apARCH = mse.apARCH,
2454
- iGARCH = mse.iGARCH,
2455
- csGARCH = mse.csGARCH)
2456
-
2457
- m.mse <- ldply(models, function(x) {
2458
- x %>% mutate(Currency = .id) %>% select(Currency, MSE)
2459
- }) %>% tbl_df %>% spread(Currency, MSE)
2448
+ ``` {r pick-mse1A}
2449
+ models <- llply(gmds, function(txt) {
2450
+ readRDS(paste0('data/fx/mse.', txt, '.rds')) %>%
2451
+ data.frame(Cat = txt, .)
2452
+ })
2453
+ names(models) <- gmds
2454
+
2455
+ models <- suppressAll(bind_rows(models)) %>% tbl_df %>%
2456
+ mutate(Cat = factor(Cat), .id = factor(.id))
2457
+
2458
+ models %>% ddply(.(.id, Cat), summarise, MSE = mean(MSE, na.rm=TRUE)) %>%
2459
+ kable(caption = 'Summary') %>%
2460
+ kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
2461
+ group_rows('USD/AUD', 1, 14, label_row_css = 'background-color: #003399; color: #fff;') %>%
2462
+ group_rows('USD/CAD', 15, 25, label_row_css = 'background-color: #003399; color: #fff;') %>%
2463
+ group_rows('USD/CHF', 26, 38, label_row_css = 'background-color: #003399; color: #fff;') %>%
2464
+ group_rows('USD/CNY', 39, 49, label_row_css = 'background-color: #003399; color: #fff;') %>%
2465
+ group_rows('USD/EUR', 50, 63, label_row_css = 'background-color: #003399; color: #fff;') %>%
2466
+ group_rows('USD/GBP', 64, 76, label_row_css = 'background-color: #003399; color: #fff;') %>%
2467
+ group_rows('USD/JPY', 77, 88, label_row_css = 'background-color: #003399; color: #fff;') %>%
2468
+ scroll_box(width = '100%', height = '400px')
2460
2469
```
2461
2470
2471
+ Due to some models unable produced a result, here I only filter and display the models with 7 currencies as below.
2472
+
2473
+ ``` {r pick-mse1B}
2474
+ #'@ dplyr::count(models, Cat) %>% dplyr::filter(n == 7)
2475
+ cats <- dplyr::count(models, Cat) %>% dplyr::filter(n == 7) %>% .[1] %>% unlist %>% factor
2476
+
2477
+ models %>% ddply(.(Cat), summarise, MSE = mean(MSE, na.rm=TRUE)) %>%
2478
+ dplyr::filter(Cat %in% cats) %>%
2479
+ kable(caption = 'Summary') %>%
2480
+ kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))
2481
+ ```
2482
+
2483
+ Now I plot a table with rank where shows all possible models.
2484
+
2462
2485
``` {r m-mse, echo=FALSE}
2486
+ m.mse <- models %>% spread(.id, MSE)
2487
+
2463
2488
tagList(
2464
2489
tags$div(align = "center",
2465
2490
class = "bg-info",
@@ -2468,13 +2493,13 @@ tagList(
2468
2493
"GARCH models")),
2469
2494
as.htmlwidget(m.mse %>% formattable(list(
2470
2495
2471
- .id = color_tile('white', 'darkgoldenrod'),
2496
+ Cat = color_tile('white', 'darkgoldenrod'),
2472
2497
2473
2498
USDAUD = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2474
2499
2475
2500
USDEUR = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2476
2501
2477
- USDGBP = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2502
+ # USDGBP = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2478
2503
2479
2504
USDCHF = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2480
2505
@@ -2488,24 +2513,94 @@ tagList(
2488
2513
2489
2514
## Markov Method
2490
2515
2516
+ Set ` n.start = ns ` , ` forecast.length = nrow(x) - ns ` , ` refit.every = 1 ` , ` refit.window = 'moving' ` .
2517
+
2518
+ ``` {r pick-mse2A, warning=FALSE}
2519
+ models <- llply(gmds, function(txt) {
2520
+ dfm <- tryCatch(readRDS(paste0('data/fx/mse.', txt, '2.rds')), error = function(e) cat(paste0('data/fx/mse.', txt, '2.rds error!\n')))
2521
+ if(!is.null(dfm)) dfm %>% data.frame(Cat = txt, .)
2522
+ })
2523
+ names(models) <- gmds
2524
+
2525
+ models <- suppressAll(bind_rows(models)) %>% tbl_df %>%
2526
+ mutate(Cat = factor(Cat), .id = factor(.id))
2527
+
2528
+ models %>% ddply(.(.id, Cat), summarise, MSE = mean(MSE, na.rm=TRUE)) %>%
2529
+ kable(caption = 'Summary') %>%
2530
+ kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
2531
+ group_rows('USD/AUD', 1, 14, label_row_css = 'background-color: #003399; color: #fff;') %>%
2532
+ group_rows('USD/CAD', 15, 25, label_row_css = 'background-color: #003399; color: #fff;') %>%
2533
+ group_rows('USD/CHF', 26, 38, label_row_css = 'background-color: #003399; color: #fff;') %>%
2534
+ group_rows('USD/CNY', 39, 49, label_row_css = 'background-color: #003399; color: #fff;') %>%
2535
+ group_rows('USD/EUR', 50, 63, label_row_css = 'background-color: #003399; color: #fff;') %>%
2536
+ group_rows('USD/GBP', 64, 76, label_row_css = 'background-color: #003399; color: #fff;') %>%
2537
+ group_rows('USD/JPY', 77, 88, label_row_css = 'background-color: #003399; color: #fff;') %>%
2538
+ scroll_box(width = '100%', height = '400px')
2539
+ ```
2540
+
2541
+ Due to some models unable produced a result, here I only filter and display the models with 7 currencies as below.
2542
+
2543
+ ``` {r pick-mse2B}
2544
+ #'@ dplyr::count(models, Cat) %>% dplyr::filter(n == 7)
2545
+ cats <- dplyr::count(models, Cat) %>% dplyr::filter(n == 7) %>% .[1] %>% unlist %>% factor
2546
+
2547
+ models %>% ddply(.(Cat), summarise, MSE = mean(MSE, na.rm=TRUE)) %>%
2548
+ dplyr::filter(Cat %in% cats) %>%
2549
+ kable(caption = 'Summary') %>%
2550
+ kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))
2551
+ ```
2552
+
2553
+ Now I plot a table with rank where shows all possible models.
2554
+
2555
+ ``` {r m-mse2, echo=FALSE}
2556
+ m.mse <- models %>% spread(.id, MSE)
2557
+
2558
+ tagList(
2559
+ tags$div(align = "center",
2560
+ class = "bg-info",
2561
+ tags$h3(class = "bg-primary", "MSE Comparison"),
2562
+ tags$h5(align = "center", class = "text-muted",
2563
+ "GARCH models")),
2564
+ as.htmlwidget(m.mse %>% formattable(list(
2565
+
2566
+ Cat = color_tile('white', 'darkgoldenrod'),
2567
+
2568
+ USDAUD = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2569
+
2570
+ USDEUR = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2571
+
2572
+ # USDGBP = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2573
+
2574
+ USDCHF = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2575
+
2576
+ USDCAD = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2577
+
2578
+ USDCNY = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
2579
+
2580
+ USDJPY = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')'))
2581
+ ))))
2582
+ ```
2583
+
2584
+ ## Markov Method 2
2585
+
2586
+ Now, we look at the mse where I use ` ugarchfit() ` and ` ugarchforecast ` , actually it is same with above [ Markov Method] but just seperate all prediction result as single file where we able to filter the error to find the most accurate model (as well as know he frequence of bias and precise among the models.).
2587
+
2491
2588
### MSE and AIC
2492
2589
2493
2590
Below check the progress of the saved files.
2494
2591
2495
2592
``` {r check-progress}
2496
- #'@ gmds <- c('sGARCH', 'fGARCH.GARCH', 'fGARCH.TGARCH', 'fGARCH.AVGARCH', 'fGARCH.NGARCH', 'fGARCH.NAGARCH', 'fGARCH.APARCH', 'fGARCH.GJRGARCH', 'fGARCH.ALLGARCH', 'eGARCH', 'gjrGARCH', 'apARCH', 'iGARCH', 'csGARCH')
2497
-
2498
2593
## check how many data saved in progress.
2499
2594
l_ply(gmds, function(x) {
2500
2595
x2 <- ifelse(x == 'gjrGARCH', 'pred2', x)
2501
2596
task_progress(.pattern = paste0('^', x2, '.'), .loops = FALSE)
2502
2597
})
2503
2598
2504
2599
## check latest date saved in progress.
2505
- l_ply(gmds, function(x) {
2506
- x2 <- ifelse(x == 'gjrGARCH', 'pred2', x)
2507
- task_progress(.date = TRUE, .pattern = paste0('^', x2, '.'), .loops = FALSE)
2508
- })
2600
+ #' @ l_ply(gmds, function(x) {
2601
+ #' @ x2 <- ifelse(x == 'gjrGARCH', 'pred2', x)
2602
+ #' @ task_progress(.date = TRUE, .pattern = paste0('^', x2, '.'), .loops = FALSE)
2603
+ #' @ })
2509
2604
```
2510
2605
2511
2606
``` {r read-models}
0 commit comments