# 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("COST", "LOW", "HD", "KR")
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 = "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 COST 2013-03-28 0.0748
## 2 COST 2013-06-28 0.0440
## 3 COST 2013-09-30 0.0434
## 4 COST 2013-12-31 0.0354
## 5 COST 2014-03-31 -0.0610
## 6 COST 2014-06-30 0.0337
## 7 COST 2014-09-30 0.0876
## 8 COST 2014-12-31 0.126
## 9 COST 2015-03-31 0.102
## 10 COST 2015-06-30 -0.112
## # ℹ 70 more rows
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "HD" "KR" "LOW"
# weights
weights <- c(0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 COST 0.25
## 2 HD 0.2
## 3 KR 0.2
## 4 LOW 0.1
portfolio_retuns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
portfolio_retuns_tbl
## # A tibble: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.100
## 2 2013-06-28 0.0500
## 3 2013-09-30 0.0551
## 4 2013-12-31 0.0274
## 5 2014-03-31 -0.00227
## 6 2014-06-30 0.0383
## 7 2014-09-30 0.0690
## 8 2014-12-31 0.129
## 9 2015-03-31 0.0863
## 10 2015-06-30 -0.0522
## 11 2015-09-30 0.0292
## 12 2015-12-31 0.0967
## 13 2016-03-31 -0.0199
## 14 2016-06-30 -0.0103
## 15 2016-09-30 -0.0551
## 16 2016-12-30 0.0521
## 17 2017-03-31 0.0159
## 18 2017-06-30 -0.0429
## 19 2017-09-29 -0.00391
## 20 2017-12-29 0.142
portfolio_sd_tidyquant_builtin_percent <- portfolio_retuns_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.0577 0.0577
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_retuns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.03522184
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 COST 0.0381 0.0625
## 2 HD 0.0613 0.0615
## 3 KR 0.0411 0.157
## 4 LOW 0.0523 0.0901
## 5 Portfolio 0.0352 0.0577
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 has little return at the cost of almost no risk. KR is the most risky and would be the worst stock to invest all of your money in solely. The stock I would invest all of my money in out of these four would be HD as it has the most return and a slight more risk than the portfolio. The risk would be worth the return I would get out of this stock.