Skip to content

Instantly share code, notes, and snippets.

@wesslen
Created May 29, 2020 14:46
Show Gist options
  • Select an option

  • Save wesslen/b2becf7609fd140dd55562b6eed4665f to your computer and use it in GitHub Desktop.

Select an option

Save wesslen/b2becf7609fd140dd55562b6eed4665f to your computer and use it in GitHub Desktop.
get-returns.R
library(tidyverse)
returns <- read_csv("data/returns.csv") %>%
select(Year, equities_sp, treasury_10yr) %>%
gather(key = "Asset", value = "Returns", -Year) %>%
mutate(Asset = ifelse(Asset=="equities_sp",
"Asset A: High risk, high return",
"Asset B: Low risk, low return"))
set.seed(123)
samplingA <- function(periods){
z <- filter(returns, Asset == "Asset A: High risk, high return") %>%
dplyr::sample_n(periods, replace = TRUE) %>%
select(Returns) %>%
mutate(Returns = Returns + 1)
return((prod(z$Returns) ^ (1 / periods)) - 1)
}
samplingB <- function(periods){
z <- filter(returns, Asset == "Asset B: Low risk, low return") %>%
dplyr::sample_n(periods, replace = TRUE) %>%
select(Returns) %>%
mutate(Returns = Returns + 1)
return((prod(z$Returns) ^ (1 / periods)) - 1)
}
getReturns <- function(n){
valA <- samplingA(n)
valA <- valA[0]
valB <- samplingB(n)
valB <- valB[0]
for (i in 1:60){
zA <- samplingA(n)
valA <- append(valA, zA)
zB <- samplingB(n)
valB <- append(valB, zB)
}
return(list(valA, valB))
}
getOrder <- function(val, name){
tibble(returns = val) %>%
arrange(desc(returns)) %>%
mutate(id = rep(1:30, each=2)) %>%
group_by(id) %>%
summarise(returns = mean(returns)) %>%
mutate(id = as.factor(id),
asset = name)
}
getCharts <- function(n){
val <- getReturns(n)
bind_rows(getOrder(val[[1]], "Asset A: High risk, high return"),getOrder(val[[2]], "Asset B: Low risk, low return")) %>%
ggplot(aes(x = fct_reorder(id, returns), y = returns)) +
geom_col(width = 0.5) +
facet_wrap(~asset, ncol = 2) +
scale_y_continuous(sec.axis = dup_axis(), labels = scales::percent_format(accuracy = 0.1)) +
cowplot::theme_minimal_hgrid() +
theme(axis.text.x=element_blank()) +
theme(text = element_text(size = 14)) +
labs(x = "Annualized Returns", y = " ", title = paste0("Investing over ",n," Year(s)"))
}
Year equities_sp treasury_3mo treasury_10yr
1928 0.4381 0.0308 0.0084
1929 -0.083 0.0316 0.042
1930 -0.2512 0.0455 0.0454
1931 -0.4384 0.0231 -0.0256
1932 -0.0864 0.0107 0.0879
1933 0.4998 0.0096 0.0186
1934 -0.0119 0.0032 0.0796
1935 0.4674 0.0018 0.0447
1936 0.3194 0.0017 0.0502
1937 -0.3534 0.003 0.0138
1938 0.2928 0.0008 0.0421
1939 -0.011 0.0004 0.0441
1940 -0.1067 0.0003 0.054
1941 -0.1277 0.0008 -0.0202
1942 0.1917 0.0034 0.0229
1943 0.2506 0.0038 0.0249
1944 0.1903 0.0038 0.0258
1945 0.3582 0.0038 0.038
1946 -0.0843 0.0038 0.0313
1947 0.052 0.0057 0.0092
1948 0.057 0.0102 0.0195
1949 0.183 0.011 0.0466
1950 0.3081 0.0117 0.0043
1951 0.2368 0.0148 -0.003
1952 0.1815 0.0167 0.0227
1953 -0.0121 0.0189 0.0414
1954 0.5256 0.0096 0.0329
1955 0.326 0.0166 -0.0134
1956 0.0744 0.0256 -0.0226
1957 -0.1046 0.0323 0.068
1958 0.4372 0.0178 -0.021
1959 0.1206 0.0326 -0.0265
1960 0.0034 0.0305 0.1164
1961 0.2664 0.0227 0.0206
1962 -0.0881 0.0278 0.0569
1963 0.2261 0.0311 0.0168
1964 0.1642 0.0351 0.0373
1965 0.124 0.039 0.0072
1966 -0.0997 0.0484 0.0291
1967 0.238 0.0433 -0.0158
1968 0.1081 0.0526 0.0327
1969 -0.0824 0.0656 -0.0501
1970 0.0356 0.0669 0.1675
1971 0.1422 0.0454 0.0979
1972 0.1876 0.0395 0.0282
1973 -0.1431 0.0673 0.0366
1974 -0.259 0.0778 0.0199
1975 0.37 0.0599 0.0361
1976 0.2383 0.0497 0.1598
1977 -0.0698 0.0513 0.0129
1978 0.0651 0.0693 -0.0078
1979 0.1852 0.0994 0.0067
1980 0.3174 0.1122 -0.0299
1981 -0.047 0.143 0.082
1982 0.2042 0.1101 0.3281
1983 0.2234 0.0845 0.032
1984 0.0615 0.0961 0.1373
1985 0.3124 0.0749 0.2571
1986 0.1849 0.0604 0.2428
1987 0.0581 0.0572 -0.0496
1988 0.1654 0.0645 0.0822
1989 0.3148 0.0811 0.1769
1990 -0.0306 0.0755 0.0624
1991 0.3023 0.0561 0.15
1992 0.0749 0.0341 0.0936
1993 0.0997 0.0298 0.1421
1994 0.0133 0.0399 -0.0804
1995 0.372 0.0552 0.2348
1996 0.2268 0.0502 0.0143
1997 0.331 0.0505 0.0994
1998 0.2834 0.0473 0.1492
1999 0.2089 0.0451 -0.0825
2000 -0.0903 0.0576 0.1666
2001 -0.1185 0.0367 0.0557
2002 -0.2197 0.0166 0.1512
2003 0.2836 0.0103 0.0038
2004 0.1074 0.0123 0.0449
2005 0.0483 0.0301 0.0287
2006 0.1561 0.0468 0.0196
2007 0.0548 0.0464 0.1021
2008 -0.3655 0.0159 0.201
2009 0.2594 0.0014 -0.1112
2010 0.1482 0.0013 0.0846
2011 0.021 0.0003 0.1604
2012 0.1589 0.0005 0.0297
2013 0.3215 0.0007 -0.091
2014 0.1352 0.0005 0.1075
2015 0.0138 0.0021 0.0128
2016 0.1177 0.0051 0.0069
2017 0.2161 0.0139 0.028
2018 -0.0423 0.0237 -0.0002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment