# 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
symbols <- c("AAPL", "GOOG", "NFLX", "VOO")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2019-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"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "GOOG" "NFLX" "VOO"
# weights
weights <- c(.3, .3, .2, .2)
weights
## [1] 0.3 0.3 0.2 0.2
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.3
## 2 GOOG 0.3
## 3 NFLX 0.2
## 4 VOO 0.2
# ?tq_portfolio
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: 82 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2020-01-31 0.0496
## 2 2020-02-28 -0.0606
## 3 2020-03-31 -0.0872
## 4 2020-04-30 0.134
## 5 2020-05-29 0.0510
## 6 2020-06-30 0.0578
## 7 2020-07-31 0.0860
## 8 2020-08-31 0.117
## 9 2020-09-30 -0.0834
## 10 2020-10-30 -0.00426
## # ℹ 72 more rows
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4))
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.0642 0.0642
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.01613931
# Expected Returns vs Risk
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
# Add portfolio sd
add_row(tibble(asset = "Portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 5 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 AAPL 0.0192 0.0766
## 2 GOOG 0.0203 0.0849
## 3 NFLX 0.0095 0.126
## 4 VOO 0.012 0.0483
## 5 Portfolio 0.0161 0.0642
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_label_repel(aes(label = asset))
rolling_sd_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = 24,
FUN = sd,
col_rename = "rolling_sd") %>%
na.omit() %>%
select(date, rolling_sd)
rolling_sd_tbl
## # A tibble: 59 × 2
## date rolling_sd
## <date> <dbl>
## 1 2021-12-31 0.0586
## 2 2022-01-31 0.0644
## 3 2022-02-28 0.0634
## 4 2022-03-31 0.0588
## 5 2022-04-29 0.0769
## 6 2022-05-31 0.0766
## 7 2022-06-30 0.0780
## 8 2022-07-29 0.0811
## 9 2022-08-31 0.0781
## 10 2022-09-30 0.0782
## # ℹ 49 more rows
rolling_sd_tbl %>%
ggplot(aes(x = date, y = rolling_sd)) +
geom_line(color = "cornflowerblue") +
# Formatting
scale_y_continuous(label = scales::percent_format()) +
# Labeling
labs(x = NULL,
y = NULL,
title = "24-Month Rolling Volatility") +
theme(plot.title = element_text(hjust = .5))
How should you expect your portfolio to perform relative to its assets
in the portfolio? Would you invest all your money in any of the
individual stocks instead of the portfolio? Discuss both in terms of
expected return and risk.
I expect my portfolio to capture some of the potential returns that Apple and Google can bring, but deviate less or be a little less risky than if I were to put all of my money into either Apple or Google. I think that it could be worth getting rid of NFLX and trying to find a stock with a lower standard deviation, or less risk, as it appears that NFLX does not bring in the mean return of GOOG, but it has almost 2x GOOGs standard deviation. NFLX has the lowest return in the portfolio and the highest standard deviation.