# Load packages
# Core
library(tidyverse)
library(tidyquant)
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
# Choose stocks
symbols <- c("WMT", "GM", "NVDA", "TSLA", "NFLX")
# Using tq_get() ----
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
# Calculate monthly returns
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
# remane
set_names(c("asset", "date", "returns"))
# period_returns = c("yearly", "quarterly", "monthly", "weekly")
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
w <- c(0.25,
0.25,
0.20,
0.20,
0.10)
w_tbl <- tibble(symbols, w)
portfolio_returns_rebalanced_monthly_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
col_rename = "returns",
rebalance_on = "months")
portfolio_returns_rebalanced_monthly_tbl
## # A tibble: 60 × 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.161
## 2 2013-02-28 0.0179
## 3 2013-03-28 0.0334
## 4 2013-04-30 0.148
## 5 2013-05-31 0.162
## 6 2013-06-28 -0.00942
## 7 2013-07-31 0.110
## 8 2013-08-30 0.0699
## 9 2013-09-30 0.0738
## 10 2013-10-31 -0.0218
## # ℹ 50 more rows
# write_rds(portfolio_returns_rebalanced_monthly_tbl,
# "00_data/Ch03_portfolio_returns_rebalanced_monthly_tbl.rds")
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_rebalanced_monthly_tbl %>%
tq_performance(Ra = returns,
Rb = NULL,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4) * 100)
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.0608 6.08
portfolio_mean_tidyquant_builtin_percent <- portfolio_returns_rebalanced_monthly_tbl %>%
tq_performance(Ra = returns,
Rb = NULL,
performance_fun = table.Stats) %>%
select(ArithmeticMean) %>%
mutate(Mean = round(ArithmeticMean, 4) * 100) %>%
pull(Mean)
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
mutate(Stdev = Stdev * 100,
Mean = Mean * 100) %>%
add_row(tibble(asset = "Portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 6 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 GM 0.86 6.1
## 2 NFLX 4.46 13.3
## 3 NVDA 4.71 8.81
## 4 TSLA 3.7 14.5
## 5 WMT 0.83 4.71
## 6 Portfolio 3.1 6.08
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point(size = 3) +
geom_text(aes(label = asset),
vjust = 1.5,
hjust = 0.5,
size = 4) +
labs(title = "Expected Returns vs Risk",
x = "Risk (Standard Deviation %)",
y = "Expected Return (%)") +
theme_minimal()
## 24 month rolling volatility
rolling_sd_tbl <- portfolio_returns_rebalanced_monthly_tbl %>%
tq_mutate(select = returns,
mutate_fun = rollapply,
width = 24,
FUN = sd,
col_rename = "rolling_sd") %>%
na.omit() %>%
select(date, rolling_sd)
rolling_sd_tbl
## # A tibble: 37 × 2
## date rolling_sd
## <date> <dbl>
## 1 2014-12-31 0.0717
## 2 2015-01-30 0.0668
## 3 2015-02-27 0.0674
## 4 2015-03-31 0.0698
## 5 2015-04-30 0.0669
## 6 2015-05-29 0.0608
## 7 2015-06-30 0.0610
## 8 2015-07-31 0.0581
## 9 2015-08-31 0.0575
## 10 2015-09-30 0.0563
## # ℹ 27 more rows
rolling_sd_tbl %>%
ggplot(aes(date, rolling_sd)) +
geom_line(color = "cornflowerblue") +
scale_y_continuous(labels = scales::percent) +
scale_x_date(breaks = scales::breaks_pretty(n = 7)) +
labs(title = "24-Month Rolling Volatility",
x = NULL,
y = NULL) +
theme(plot.title = element_text(hjust = 0.5))
My portfolio is expected to perform with a mix of returns and risks based on the stocks I picked, but overall it should have less risk because it’s diversified. Some individual stocks might have higher returns, but they also come with way more ups and downs. By investing in the portfolio instead of just one stock, I’m spreading out the risk so my investment is more stable. Unless I’m ready to take on a lot more risk for a chance at bigger returns, it just makes more sense to stick with the diversified portfolio instead of putting all my money into one stock.