# 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("LULU", "NKE", "UA")
prices <- tq_get(x = symbols,
from = "2019-12-31",
to = "2024-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "LULU" "NKE" "UA"
# weights
weights <- c(0.45, 0.35, 0.2)
weights
## [1] 0.45 0.35 0.20
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 LULU 0.45
## 2 NKE 0.35
## 3 UA 0.2
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
portfolio_returns_tbl
## # A tibble: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2020-03-31 -0.334
## 2 2020-06-30 0.303
## 3 2020-09-30 0.133
## 4 2020-12-31 0.150
## 5 2021-03-31 -0.0349
## 6 2021-06-30 0.133
## 7 2021-09-30 0.0138
## 8 2021-12-31 0.0397
## 9 2022-03-31 -0.135
## 10 2022-06-30 -0.371
## 11 2022-09-30 -0.108
## 12 2022-12-30 0.263
## 13 2023-03-31 0.0662
## 14 2023-06-30 -0.0664
## 15 2023-09-29 -0.0507
## 16 2023-12-29 0.226
## 17 2024-03-28 -0.202
## 18 2024-06-28 -0.214
## 19 2024-09-30 0.0636
## 20 2024-12-30 0.0737
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.186 0.186
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] -0.002529237
# 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: 4 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 LULU 0.0253 0.208
## 2 NKE -0.0124 0.184
## 3 UA -0.0478 0.320
## 4 Portfolio -0.00253 0.186
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
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. The portfolio performs better than two out of three of its assets. I would invest in Lululemon only rather than the portfolio if I wanted a greater return, however, this is riskier than investing in the portfolio because if Lululemon stocks start plummeting I would lose more money than if I had investments in the multiple assets which do well as a portfolio. Personally, I like the comfort of a steady return with less risk that the portfolio provides rather than the riskier but greater return from LULU.