# 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("MSFT", "AAPL", "GOOG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
prices
## # A tibble: 3,780 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2012-12-31 26.6 26.8 26.4 26.7 42749500 21.6
## 2 MSFT 2013-01-02 27.2 27.7 27.1 27.6 52899300 22.3
## 3 MSFT 2013-01-03 27.6 27.6 27.2 27.2 48294400 22.0
## 4 MSFT 2013-01-04 27.3 27.3 26.7 26.7 52521100 21.6
## 5 MSFT 2013-01-07 26.8 26.9 26.6 26.7 37110400 21.5
## 6 MSFT 2013-01-08 26.8 26.8 26.5 26.5 44703100 21.4
## 7 MSFT 2013-01-09 26.7 26.8 26.6 26.7 49047900 21.5
## 8 MSFT 2013-01-10 26.6 27.0 26.3 26.5 71431300 21.3
## 9 MSFT 2013-01-11 26.5 26.9 26.3 26.8 55512100 21.6
## 10 MSFT 2013-01-14 26.9 27.1 26.8 26.9 48324400 21.7
## # ℹ 3,770 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: 60 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AAPL 2013-03-28 -0.178
## 2 AAPL 2013-06-28 -0.103
## 3 AAPL 2013-09-30 0.191
## 4 AAPL 2013-12-31 0.169
## 5 AAPL 2014-03-31 -0.0383
## 6 AAPL 2014-06-30 0.198
## 7 AAPL 2014-09-30 0.0858
## 8 AAPL 2014-12-31 0.0956
## 9 AAPL 2015-03-31 0.124
## 10 AAPL 2015-06-30 0.0122
## # ℹ 50 more rows
symbols <- asset_returns_tbl %>%
distinct(asset) %>%
pull()
weights <- c(0.25, 0.25, 0.5)
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.25
## 2 GOOG 0.25
## 3 MSFT 0.5
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: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0228
## 2 2013-06-28 0.0976
## 3 2013-09-30 0.0314
## 4 2013-12-31 0.166
## 5 2014-03-31 0.0386
## 6 2014-06-30 0.0696
## 7 2014-09-30 0.0784
## 8 2014-12-31 0.00492
## 9 2015-03-31 -0.0220
## 10 2015-06-30 0.0353
## 11 2015-09-30 0.0125
## 12 2015-12-31 0.161
## 13 2016-03-31 0.00675
## 14 2016-06-30 -0.0843
## 15 2016-09-30 0.135
## 16 2016-12-30 0.0469
## 17 2017-03-31 0.105
## 18 2017-06-30 0.0501
## 19 2017-09-29 0.0729
## 20 2017-12-29 0.118
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.0622 0.0622
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.0572837
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 AAPL 0.045 0.119
## 2 GOOG 0.0544 0.0905
## 3 MSFT 0.0648 0.0855
## 4 Portfolio 0.0573 0.0622
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
### 24 Months Rolling Volatility
rolling_sd_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = 3,
FUN = sd, col_rename = "rolling_sd") %>%
na.omit() %>%
select(date,rolling_sd)
rolling_sd_tbl
## # A tibble: 18 × 2
## date rolling_sd
## <date> <dbl>
## 1 2013-09-30 0.0409
## 2 2013-12-31 0.0673
## 3 2014-03-31 0.0757
## 4 2014-06-30 0.0664
## 5 2014-09-30 0.0209
## 6 2014-12-31 0.0401
## 7 2015-03-31 0.0520
## 8 2015-06-30 0.0287
## 9 2015-09-30 0.0289
## 10 2015-12-31 0.0799
## 11 2016-03-31 0.0874
## 12 2016-06-30 0.124
## 13 2016-09-30 0.110
## 14 2016-12-30 0.110
## 15 2017-03-31 0.0446
## 16 2017-06-30 0.0327
## 17 2017-09-29 0.0276
## 18 2017-12-29 0.0344
rolling_sd_tbl %>%
ggplot(aes(x = date, y = rolling_sd)) +
geom_line(color = "cornflowerblue") +
#Formatting
scale_y_continuous(labels = scales::percent_format()) +
# Labeling
labs(x = NULL,
y = NULL,
title = "24-Month Rolling Volatility") +
theme(plot.title = element_text(hjust = 0.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 would expect the portfolio to be less risky than the individual assets
because it has a lower standard deviation. The expected return of the
portfolio is also higher than 2 out of the 3 individual stocks. This is
a very diversified portfolio that I would invest my money into rather
than an individual stock.