Skip to content

Commit fe770a4

Browse files
committed
saved mse simulated files.
1 parent 215fa8a commit fe770a4

15 files changed

+126
-31
lines changed

binary-Q1Uni-GARCH.Rmd

+126-31
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,17 @@ if (!file.exists('data/fx/mse.fGARCH.ALLGARCH2.rds')) {
15791579
roll <- ugarchroll(spec, data = x, n.start = ns, forecast.length = n,
15801580
refit.every = 1, refit.window = 'moving',
15811581
cluster = .cl)
1582-
attributes(roll)$forecast$density %>%
1582+
1583+
if (!is.null(roll)) {
1584+
res <- attributes(roll)$forecast$density %>%
15831585
tbl_df %>% mutate(MSE = mean((Mu - Realized)^2)) %>%
15841586
.$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))
15861593
saveRDS(mse.fGARCH.ALLGARCH2, 'data/fx/mse.fGARCH.ALLGARCH2.rds')
15871594
15881595
} else {
@@ -1644,7 +1651,7 @@ for (dt in timeID) {
16441651

16451652
The default setting is `forecast.length = 500, refit.every = 25, refit.window = 'recursive'`.
16461653

1647-
```{r eGARCH, echo=FALSE}
1654+
```{r mseeGARCH, echo=FALSE}
16481655
if (!file.exists('data/fx/mse.eGARCH.rds')) {
16491656
mse.eGARCH <- ldply(mbase, function(x) {
16501657
x <- Cl(x)
@@ -1699,7 +1706,7 @@ mse.eGARCH %>%
16991706

17001707
Set `n.start = ns`, `forecast.length = nrow(x) - ns`, `refit.every = 1`, `refit.window = 'moving'`.
17011708

1702-
```{r eGARCH2, echo=FALSE}
1709+
```{r mseeGARCH2, echo=FALSE}
17031710
if (!file.exists('data/fx/mse.eGARCH2.rds')) {
17041711
mse.eGARCH2 <- ldply(mbase, function(x) {
17051712
x <- Cl(x)
@@ -2438,28 +2445,46 @@ for (dt in timeID) {
24382445

24392446
The default setting is `forecast.length = 500, refit.every = 25, refit.window = 'recursive'`.
24402447

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')
24602469
```
24612470

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+
24622485
```{r m-mse, echo=FALSE}
2486+
m.mse <- models %>% spread(.id, MSE)
2487+
24632488
tagList(
24642489
tags$div(align = "center",
24652490
class = "bg-info",
@@ -2468,13 +2493,13 @@ tagList(
24682493
"GARCH models")),
24692494
as.htmlwidget(m.mse %>% formattable(list(
24702495
2471-
.id = color_tile('white', 'darkgoldenrod'),
2496+
Cat = color_tile('white', 'darkgoldenrod'),
24722497
24732498
USDAUD = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
24742499
24752500
USDEUR = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
24762501
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)), ')')),
24782503
24792504
USDCHF = formatter('span', style = x ~ formattable::style(color = ifelse(rank(x) <= 3, 'green', 'gray')), x ~ paste0(round(x, 7), ' (rank: ', sprintf('%02d', rank(x)), ')')),
24802505
@@ -2488,24 +2513,94 @@ tagList(
24882513

24892514
## Markov Method
24902515

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+
24912588
### MSE and AIC
24922589

24932590
Below check the progress of the saved files.
24942591

24952592
```{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-
24982593
## check how many data saved in progress.
24992594
l_ply(gmds, function(x) {
25002595
x2 <- ifelse(x == 'gjrGARCH', 'pred2', x)
25012596
task_progress(.pattern = paste0('^', x2, '.'), .loops = FALSE)
25022597
})
25032598
25042599
## 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+
#' @ })
25092604
```
25102605

25112606
```{r read-models}

data/fx/mse.apARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.csGARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.eGARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.fGARCH.ALLGARCH.rds

-13 Bytes
Binary file not shown.

data/fx/mse.fGARCH.ALLGARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.fGARCH.APARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.fGARCH.AVGARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.fGARCH.GARCH2.rds

217 Bytes
Binary file not shown.

data/fx/mse.fGARCH.GJRGARCH2.rds

148 Bytes
Binary file not shown.

data/fx/mse.fGARCH.NAGARCH2.rds

135 Bytes
Binary file not shown.

data/fx/mse.fGARCH.TGARCH2.rds

135 Bytes
Binary file not shown.

data/fx/mse.gjrGARCH2.rds

217 Bytes
Binary file not shown.

data/fx/mse.iGARCH2.rds

37 Bytes
Binary file not shown.

data/fx/mse.sGARCH2.rds

217 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)