# 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", "BA", "DIS", "NKE")
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"))
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
w <- c(0.30,
0.25,
0.20,
0.15)
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.0289
## 2 2013-02-28 0.00830
## 3 2013-03-28 0.0484
## 4 2013-04-30 0.0472
## 5 2013-05-31 0.0245
## 6 2013-06-28 -0.0242
## 7 2013-07-31 0.0489
## 8 2013-08-30 0.0105
## 9 2013-09-30 0.0577
## 10 2013-10-31 0.0725
## # ℹ 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))
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.038 0.038
# mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_rebalanced_monthly_tbl$returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.01588262
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.
Diversifying across stocks like AAPL, BA, DIS, and NKE can help mitigate risks associated with individual stocks and provide a more balanced expected return. However, investing all your money in a single individual stock would typically be riskier, as it’s subject to the specific performance of that stock alone, potentially leading to higher returns or losses, depending on the stock’s performance during that period.