# 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 ## 1 Import stock prices
symbol <- c("AMZN", "BIG", "TSLA", "WM", "PLUG")
prices <- tq_get(x = symbol,
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()
symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
symbols
## [1] "AMZN" "BIG" "PLUG" "TSLA" "WM"
weight <- c(0.2,0.2,0.2,0.2,0.2)
weight
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weight)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = symbol,
returns_col = quarterly.returns,
weights = w_tbl,
rebalance_on = "quarter")
portfolio_sd_tq_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4))
portfolio_mean_sd_tq_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
sd_mean_tbl <- asset_returns_tbl %>%
group_by(symbol) %>%
tq_performance(Ra = quarterly.returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
add_row(tibble(symbol = "Portfolio",
Mean = portfolio_mean_sd_tq_builtin_percent,
Stdev = portfolio_sd_tq_builtin_percent$tq_sd))
sd_mean_tbl <- asset_returns_tbl %>%
group_by(symbol) %>%
tq_performance(Ra = quarterly.returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
add_row(tibble(symbol = "Portfolio",
Mean = portfolio_mean_sd_tq_builtin_percent,
Stdev = portfolio_sd_tq_builtin_percent$tq_sd))
sd_mean_tbl <- asset_returns_tbl %>%
group_by(symbol) %>%
tq_performance(Ra = quarterly.returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
add_row(tibble(symbol = "Portfolio",
Mean = portfolio_mean_sd_tq_builtin_percent,
Stdev = portfolio_sd_tq_builtin_percent$tq_sd))
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = symbol)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = symbol))
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.
If I was to invest in this portfolio I would most likely put higher
weights in PLUG and TSLA as they outperform the portfolio. I would also
stay invested in the other stocks because of the lowered risk that is
offered by these stocks. The return of some stocks (TSLA,Plug) is higher
but the risk also tends to follow and be higher than the portfolios.