# 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("TSLA", "NVDA", "GOOGL", "ORCL", "JNJ")
prices <- tq_get(x = symbols,
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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GOOGL" "JNJ" "NVDA" "ORCL" "TSLA"
# weights
weight <- c(0.2, 0.1, 0.25, 0.1, 0.25)
weight
## [1] 0.20 0.10 0.25 0.10 0.25
w_tbl <- tibble(symbols, weight)
w_tbl
## # A tibble: 5 × 2
## symbols weight
## <chr> <dbl>
## 1 GOOGL 0.2
## 2 JNJ 0.1
## 3 NVDA 0.25
## 4 ORCL 0.1
## 5 TSLA 0.25
# ?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: 60 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-01-31 0.0504
## 2 2013-02-28 0.00156
## 3 2013-03-28 0.0234
## 4 2013-04-30 0.120
## 5 2013-05-31 0.205
## 6 2013-06-28 0.0270
## 7 2013-07-31 0.0787
## 8 2013-08-30 0.0540
## 9 2013-09-30 0.0666
## 10 2013-10-31 -0.0130
## # ℹ 50 more rows
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.0511 0.0511
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.02786222
# 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: 6 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 GOOGL 0.0182 0.0539
## 2 JNJ 0.0138 0.0364
## 3 NVDA 0.0471 0.0881
## 4 ORCL 0.0069 0.0471
## 5 TSLA 0.037 0.145
## 6 Portfolio 0.0279 0.0511
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
geom_text(aes(label = asset),
vjust = 1.5, # Nudges labels down
hjust = 0.5, # Centers labels horizontally
size = 4) # Sets text size
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.
Relative, to the assets in my portfolio, it’s expected to perform right
at the average of .03 or 3%. I would say it’s pretty properly
diversified, with higher earning stocks having more volatility, and then
some lower averaged prices with less deviation. I would not invest all
my money into one stock for this reason, as it might prove to be
successful at times, it’s so much more risky to have all of your eggs in
one basket that if the stock plummets, you’ll lose all of your
investment. A diversified portfolio allows you to spread apart your
investments so if one asset plummets, you still have other investments.
You can still earn high returns and not carry nearly as much risk
investing this way.