This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c8a426
commit 0becd67
Showing
2 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
--- | ||
title: "plots-for-blog1" | ||
html_document: | ||
keep_md: yes | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
library(tidyverse) | ||
# Multiple plot function | ||
# | ||
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) | ||
# - cols: Number of columns in layout | ||
# - layout: A matrix specifying the layout. If present, 'cols' is ignored. | ||
# | ||
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), | ||
# then plot 1 will go in the upper left, 2 will go in the upper right, and | ||
# 3 will go all the way across the bottom. | ||
# | ||
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { | ||
library(grid) | ||
# Make a list from the ... arguments and plotlist | ||
plots <- c(list(...), plotlist) | ||
numPlots = length(plots) | ||
# If layout is NULL, then use 'cols' to determine layout | ||
if (is.null(layout)) { | ||
# Make the panel | ||
# ncol: Number of columns of plots | ||
# nrow: Number of rows needed, calculated from # of cols | ||
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), | ||
ncol = cols, nrow = ceiling(numPlots/cols)) | ||
} | ||
if (numPlots==1) { | ||
print(plots[[1]]) | ||
} else { | ||
# Set up the page | ||
grid.newpage() | ||
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) | ||
# Make each plot, in the correct location | ||
for (i in 1:numPlots) { | ||
# Get the i,j matrix positions of the regions that contain this subplot | ||
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) | ||
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, | ||
layout.pos.col = matchidx$col)) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### get data | ||
```{r} | ||
## load data: | ||
AU_returns <- read_csv("../data/AU-returns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
AU_drawdowns <- read_csv("../data/AU-drawdowns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
INDEX_backtest_returns <- read_csv("../data/INDEX-backtest-returns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
INDEX_backtest_drawdowns <- read_csv("../data/INDEX-backtest-drawdowns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
INDEX_backtest_TR_returns <- read_csv("../data/INDEX-backtest-TR-returns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
INDEX_backtest_TR_drawdowns <- read_csv("../data/INDEX-backtest-TR-drawdowns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) %>% | ||
select(-X1) | ||
Global_IL_bonds_drawdowns <- read_csv("../data/Global-IL-bonds-drawdowns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) | ||
Global_IL_bonds_returns <- read_csv("../data/Global-IL-bonds-returns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) | ||
Global_drawdowns <- read_csv("../data/Global-drawdowns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) | ||
Global_returns <- read_csv("../data/Global-returns.csv", | ||
col_types = cols(date = col_date(format = "%Y-%m-%d"))) | ||
``` | ||
|
||
## Australian INDEX from 2001 vs. real ticker return from 2012 (our algo.'s weights) vs. TR All-Seasons weights | ||
```{r} | ||
myColors <- c("#CC79A7", "#0072B2", "#F0E442", "#000000") | ||
##### plot returns: | ||
INDEX_backtest_returns %>% | ||
filter(date == min(AU_returns$date)-1 & key == "portfolio") %>% | ||
select(returns) %>% | ||
as.numeric() -> to_add | ||
AU_returns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "Our Version with IL-bonds\n(ETFs available 2012)") %>% | ||
mutate(returns = returns + to_add) -> to_bind | ||
INDEX_backtest_TR_returns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "All-Seasons ") -> to_bind2 | ||
INDEX_backtest_returns %>% | ||
mutate(key = ifelse(key == "portfolio", "Our Version ", key)) %>% | ||
mutate(key = ifelse(key == "benchmark", "ASX 300 ", key)) %>% | ||
bind_rows(to_bind) %>% | ||
bind_rows(to_bind2) %>% | ||
mutate(returns = returns/100) %>% | ||
mutate(key = fct_relevel(as_factor(key), "ASX 300 ","All-Seasons ","Our Version ", | ||
"Our Version with IL-bonds\n(ETFs available 2012)")) %>% | ||
ggplot(aes(x = date, y = returns, colour = key)) + | ||
geom_line(alpha = .8) + | ||
scale_x_date(date_breaks = "1 year", date_labels = "%Y") + | ||
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + | ||
theme(axis.text.x=element_text(angle=60, hjust=1), | ||
legend.title=element_blank(), | ||
legend.position = "bottom") + | ||
labs(x = NULL, y = NULL, title = "Cumulative Total Return") + | ||
scale_color_manual(values=myColors) -> p | ||
p + theme(legend.position = "none") -> p1 | ||
p + guides(color = guide_legend(override.aes = list(size=2))) -> p | ||
## Plot drawdowns | ||
AU_drawdowns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "Our Version with IL-bonds\n(ETFs available 2012)") -> to_bind | ||
INDEX_backtest_TR_drawdowns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "All-Seasons") -> to_bind2 | ||
INDEX_backtest_drawdowns %>% | ||
mutate(key = ifelse(key == "portfolio", "Our Version", key)) %>% | ||
mutate(key = ifelse(key == "benchmark", "ASX 300", key)) %>% | ||
bind_rows(to_bind) %>% | ||
bind_rows(to_bind2) %>% | ||
mutate(key = fct_relevel(as_factor(key), "ASX 300")) %>% | ||
mutate(drawdown = drawdown/100) %>% | ||
mutate(key = fct_relevel(as_factor(key), "ASX 300","All-Seasons","Our Version", | ||
"Our Version with IL-bonds\n(ETFs available 2012)")) %>% | ||
ggplot(aes(x = date, y = drawdown, colour = key)) + | ||
geom_line(alpha = .8) + | ||
scale_x_date(date_breaks = "1 year", date_labels = "%Y") + | ||
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + | ||
theme(axis.text.x=element_text(angle=60, hjust=1), | ||
legend.position="none") + | ||
labs(x = NULL, y = NULL, title = "Drawdowns (Peak to Trough)") + | ||
scale_color_manual(values=myColors) -> p2 | ||
png("AU.png", res = 1000, width = 7300, height = 5500) | ||
multiplot(p1, p2, cols=1) | ||
dev.off() | ||
legend <- cowplot::get_legend(p) | ||
png("legend-AU.png", res = 1050, height = 290, width = 4750) | ||
grid.newpage() | ||
grid.draw(legend) | ||
dev.off() | ||
``` | ||
|
||
## Global from 2001 | ||
```{r} | ||
##### plot returns: | ||
Global_returns %>% | ||
filter(date == min(Global_IL_bonds_returns$date)-1 & key == "portfolio") %>% | ||
select(returns) %>% | ||
as.numeric() -> to_add | ||
Global_IL_bonds_returns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "Our Version\nwith IL-bonds") %>% | ||
mutate(returns = returns + to_add) -> to_bind | ||
Global_returns %>% | ||
mutate(key = ifelse(key == "portfolio", "Our Version ", key)) %>% | ||
mutate(key = ifelse(key == "benchmark", "Global Equities ", key)) %>% | ||
bind_rows(to_bind) %>% | ||
mutate(returns = returns/100) %>% | ||
mutate(key = fct_relevel(as_factor(key), "Global Equities ","Our Version ","Our Version\nwith IL-bonds")) %>% | ||
ggplot(aes(x = date, y = returns, colour = key)) + | ||
geom_line(alpha = .8) + | ||
scale_x_date(date_breaks = "1 year", date_labels = "%Y") + | ||
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + | ||
theme(axis.text.x=element_text(angle=60, hjust=1), | ||
legend.title=element_blank(), | ||
legend.position = "bottom") + | ||
labs(x = NULL, y = NULL, title = "Cumulative Total Return") + | ||
scale_color_manual(values=myColors) -> p | ||
p + theme(legend.position = "none") -> p1 | ||
p + guides(color = guide_legend(override.aes = list(size=2))) -> p | ||
## Plot drawdowns | ||
Global_IL_bonds_drawdowns %>% | ||
filter(key == "portfolio") %>% | ||
mutate(key = "Our Version\nwith IL-bonds") -> to_bind | ||
Global_drawdowns %>% | ||
mutate(key = ifelse(key == "portfolio", "Our Version", key)) %>% | ||
mutate(key = ifelse(key == "benchmark", "Global Equities", key)) %>% | ||
bind_rows(to_bind) %>% | ||
mutate(key = fct_relevel(as_factor(key), "Global Equities")) %>% | ||
mutate(drawdown = drawdown/100) %>% | ||
ggplot(aes(x = date, y = drawdown, colour = key)) + | ||
geom_line(alpha = .8) + | ||
scale_x_date(date_breaks = "1 year", date_labels = "%Y") + | ||
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + | ||
theme(axis.text.x=element_text(angle=60, hjust=1), | ||
legend.position="none") + | ||
labs(x = NULL, y = NULL, title = "Drawdowns (Peak to Trough)") + | ||
scale_color_manual(values=myColors) -> p2 | ||
png("Global.png", res = 1000, width = 7300, height = 5500) | ||
multiplot(p1, p2, cols=1) | ||
dev.off() | ||
legend <- cowplot::get_legend(p) | ||
png("legend-Global.png", res = 875, height = 240, width = 3000) | ||
grid.newpage() | ||
grid.draw(legend) | ||
dev.off() | ||
``` | ||
|