# 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("AMZN", "MSFT", "HD", "WMT")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
prices
## # A tibble: 5,040 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AMZN 2012-12-31 12.2 12.6 12.1 12.5 68380000 12.5
## 2 AMZN 2013-01-02 12.8 12.9 12.7 12.9 65420000 12.9
## 3 AMZN 2013-01-03 12.9 13.0 12.8 12.9 55018000 12.9
## 4 AMZN 2013-01-04 12.9 13.0 12.8 13.0 37484000 13.0
## 5 AMZN 2013-01-07 13.1 13.5 13.1 13.4 98200000 13.4
## 6 AMZN 2013-01-08 13.4 13.4 13.2 13.3 60214000 13.3
## 7 AMZN 2013-01-09 13.4 13.5 13.3 13.3 45312000 13.3
## 8 AMZN 2013-01-10 13.4 13.4 13.1 13.3 57268000 13.3
## 9 AMZN 2013-01-11 13.3 13.4 13.2 13.4 48266000 13.4
## 10 AMZN 2013-01-14 13.4 13.7 13.4 13.6 85500000 13.6
## # … with 5,030 more rows
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"))
asset_returns_tbl
## # A tibble: 80 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AMZN 2013-03-28 0.0604
## 2 AMZN 2013-06-28 0.0412
## 3 AMZN 2013-09-30 0.119
## 4 AMZN 2013-12-31 0.243
## 5 AMZN 2014-03-31 -0.170
## 6 AMZN 2014-06-30 -0.0351
## 7 AMZN 2014-09-30 -0.00723
## 8 AMZN 2014-12-31 -0.0382
## 9 AMZN 2015-03-31 0.181
## 10 AMZN 2015-06-30 0.154
## # … with 70 more rows
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "HD" "MSFT" "WMT"
# weights
weights <- c(0.30, 0.30, 0.15, 0.25)
weights
## [1] 0.30 0.30 0.15 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AMZN 0.3
## 2 HD 0.3
## 3 MSFT 0.15
## 4 WMT 0.25
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 2013-03-28 0.0922
## 2 2013-06-28 0.0749
## 3 2013-09-30 0.0260
## 4 2013-12-31 0.135
## 5 2014-03-31 -0.0521
## 6 2014-06-30 -0.00123
## 7 2014-09-30 0.0599
## 8 2014-12-31 0.0620
## 9 2015-03-31 0.0515
## 10 2015-06-30 0.0191
## 11 2015-09-30 0.0431
## 12 2015-12-31 0.148
## 13 2016-03-31 -0.00465
## 14 2016-06-30 0.0518
## 15 2016-09-30 0.0683
## 16 2016-12-30 -0.0156
## 17 2017-03-31 0.101
## 18 2017-06-30 0.0628
## 19 2017-09-29 0.0409
## 20 2017-12-29 0.186
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)*100)
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.0571 5.71
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.05745327
# 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() %>%
mutate(Stdev = Stdev *100,
Mean = Mean *100) %>%
# Add portfolio sd
add_row(tibble(asset = "Portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent *100,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 5 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 AMZN 7.7 12.9
## 2 HD 6.13 6.15
## 3 MSFT 6.48 8.55
## 4 WMT 2.5 8.37
## 5 Portfolio 5.75 5.71
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
sd_mean_tbl
## # A tibble: 5 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 AMZN 7.7 12.9
## 2 HD 6.13 6.15
## 3 MSFT 6.48 8.55
## 4 WMT 2.5 8.37
## 5 Portfolio 5.75 5.71
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 can expect my portfolio to perform well. If I were to invest all of my money in one stock, I would choose Walmart because of its low volatility. Volatility is sometimes a good things, but I would prefer a stock that is less volatile because that means I have a lesser risk for losing money.