# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(plotly)
Visualize expected returns and risk to make it easier to compare the performance of multiple assets and portfolios.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("FNV", "WPM", "NEM", "CCJ", "BWXT", "NVDA", "TSM", "ASML", "CAT", "ITW", "HON", "PLD", "EQIX", "PSA", "TPL", "XOM", "CVX")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 1,020 Ă— 3
## asset date returns
## <chr> <date> <dbl>
## 1 ASML 2013-01-31 0.154
## 2 ASML 2013-02-28 -0.0563
## 3 ASML 2013-03-28 -0.0430
## 4 ASML 2013-04-30 0.0992
## 5 ASML 2013-05-31 0.0887
## 6 ASML 2013-06-28 -0.0269
## 7 ASML 2013-07-31 0.128
## 8 ASML 2013-08-30 -0.0327
## 9 ASML 2013-09-30 0.126
## 10 ASML 2013-10-31 -0.0420
## # ℹ 1,010 more rows
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "ASML" "BWXT" "CAT" "CCJ" "CVX" "EQIX" "FNV" "HON" "ITW" "NEM"
## [11] "NVDA" "PLD" "PSA" "TPL" "TSM" "WPM" "XOM"
weights <- c(0.10, 0.10, 0.10, 0.05, 0.05, 0.04, 0.03, 0.03, 0.04, 0.03, 0.03, 0.07, 0.07, 0.06, 0.07, 0.07, 0.06)
weights
## [1] 0.10 0.10 0.10 0.05 0.05 0.04 0.03 0.03 0.04 0.03 0.03 0.07 0.07 0.06 0.07
## [16] 0.07 0.06
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 17 Ă— 2
## symbols weights
## <chr> <dbl>
## 1 ASML 0.1
## 2 BWXT 0.1
## 3 CAT 0.1
## 4 CCJ 0.05
## 5 CVX 0.05
## 6 EQIX 0.04
## 7 FNV 0.03
## 8 HON 0.03
## 9 ITW 0.04
## 10 NEM 0.03
## 11 NVDA 0.03
## 12 PLD 0.07
## 13 PSA 0.07
## 14 TPL 0.06
## 15 TSM 0.07
## 16 WPM 0.07
## 17 XOM 0.06
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months")
portfolio_returns_tbl
## # A tibble: 60 Ă— 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-01-31 0.0457
## 2 2013-02-28 -0.0170
## 3 2013-03-28 0.0106
## 4 2013-04-30 -0.00468
## 5 2013-05-31 0.0276
## 6 2013-06-28 -0.0378
## 7 2013-07-31 0.0389
## 8 2013-08-30 -0.00842
## 9 2013-09-30 0.0294
## 10 2013-10-31 0.00176
## # ℹ 50 more rows
portfoltio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round (Stdev, 4))
portfoltio_sd_tidyquant_builtin_percent
## # A tibble: 1 Ă— 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.0326 0.0326
# Mean of Portfolio Returns
portfoltio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfoltio_mean_tidyquant_builtin_percent
## [1] 0.01253176
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
add_row(tibble(asset = "Portfolio",
Mean = portfoltio_mean_tidyquant_builtin_percent,
Stdev = portfoltio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 18 Ă— 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 ASML 0.0174 0.0625
## 2 BWXT 0.0204 0.0458
## 3 CAT 0.012 0.0656
## 4 CCJ -0.0106 0.0949
## 5 CVX 0.0057 0.0524
## 6 EQIX 0.0155 0.0505
## 7 FNV 0.0069 0.0951
## 8 HON 0.0165 0.034
## 9 ITW 0.0186 0.043
## 10 NEM -0.0025 0.120
## 11 NVDA 0.0471 0.0881
## 12 PLD 0.0122 0.0518
## 13 PSA 0.0089 0.0501
## 14 TPL 0.0356 0.105
## 15 TSM 0.0165 0.0523
## 16 WPM -0.007 0.125
## 17 XOM 0.0021 0.0405
## 18 Portfolio 0.0125 0.0326
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
My current portfolio would have an expected return (mean) of 1.25% and expected risk (stdev) of 3.26%. Through diversification, my portfolio holds the least risk among all of the individual stocks. That said, there is an argument for increasing exposure in HON, ITW, EQIX, TSM, ASML, or BWXT due to their similar (if greater) risk because of their greater expected returns. I still value the importance and safety of diversification so I wouldn’t invest everything into one stock, but with this chart, I can see the benefit of removing some stocks from the portfolio (or altering my weights to each stock) to enhance not only my expected returns, but my risk as well. As shown in the plot above, not only do FNV, CCJ, NEM, and WPM hold greater risk, they have lower than my portfolio returns as well. That is my review over the portfolio.