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("NKE", "MSFT", "GOOG", "TSLA", "AMZN")
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 = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
w <- c(0.25,
0.10,
0.10,
0.15,
0.40)
w_tbl <- tibble(symbols, w)
portfolio_returns_rebalanced_monthly_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
col_rename = "returns",
rebalance_on = "months")
portfolio_returns_rebalanced_monthly_tbl
## # A tibble: 60 Ă— 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0713
## 2 2013-02-28 -0.0212
## 3 2013-03-28 0.0498
## 4 2013-04-30 0.159
## 5 2013-05-31 0.260
## 6 2013-06-28 0.0501
## 7 2013-07-31 0.101
## 8 2013-08-30 0.0756
## 9 2013-09-30 0.105
## 10 2013-10-31 -0.00910
## # … with 50 more rows
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_rebalanced_monthly_tbl %>%
tq_performance(Ra = returns,
Rb = NULL,
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.0656 6.56
portfolio_returns_rebalanced_monthly_tbl %>%
ggplot(aes(date, returns)) +
geom_point(color = "cornflowerblue", size = 2) +
labs(title = "Scatterplot of Returns by Date") +
theme(plot.title = element_text(hjust = 0.5))
sd_plot <- sd(portfolio_returns_rebalanced_monthly_tbl$returns)
mean_plot <- mean(portfolio_returns_rebalanced_monthly_tbl$returns)
portfolio_returns_rebalanced_monthly_tbl %>%
mutate(hist_col = case_when(
returns > mean_plot + sd_plot ~ "high",
returns < mean_plot - sd_plot ~ "middle",
TRUE ~ "low"
)) %>%
ggplot(aes(date, returns, col = hist_col)) +
geom_point(size = 2) +
labs(title = "Colored Scatter") +
theme(plot.title = element_text(hjust = 0.5))
sd_plot <- sd(portfolio_returns_rebalanced_monthly_tbl$returns)
mean_plot <- mean(portfolio_returns_rebalanced_monthly_tbl$returns)
portfolio_returns_rebalanced_monthly_tbl %>%
mutate(hist_col = case_when(
returns > mean_plot + sd_plot ~ "high",
returns < mean_plot - sd_plot ~ "middle",
TRUE ~ "low"
)) %>%
ggplot(aes(date, returns, col = hist_col)) +
geom_point(size = 2) +
labs(title = "Colored Scatter with Line") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_hline(yintercept = mean_plot + sd_plot, linetype = "dotted", color = "purple") +
geom_hline(yintercept = mean_plot - sd_plot, linetype = "dotted", color = "purple")
portfolio_returns_rebalanced_monthly_tbl
## # A tibble: 60 Ă— 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0713
## 2 2013-02-28 -0.0212
## 3 2013-03-28 0.0498
## 4 2013-04-30 0.159
## 5 2013-05-31 0.260
## 6 2013-06-28 0.0501
## 7 2013-07-31 0.101
## 8 2013-08-30 0.0756
## 9 2013-09-30 0.105
## 10 2013-10-31 -0.00910
## # … with 50 more rows
asset_returns_sd_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
Rb = NULL,
performance_fun = table.Stats) %>%
select(asset, Stdev) %>%
ungroup() %>%
add_row(tibble(asset = "Portfolio",
Stdev = sd(portfolio_returns_rebalanced_monthly_tbl$returns)))
asset_returns_sd_tbl %>%
ggplot(aes(asset, Stdev, col = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset),
data = asset_returns_sd_tbl %>%
filter(asset == "Portfolio")) +
labs(title = "")
asset_returns_sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
Rb = NULL,
performance_fun = table.Stats) %>%
select(asset, Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
add_row(tibble(asset = "Portfolio",
Mean = mean(portfolio_returns_rebalanced_monthly_tbl$returns),
Stdev = sd(portfolio_returns_rebalanced_monthly_tbl$returns)))
asset_returns_sd_mean_tbl %>%
ggplot(aes(Stdev, Mean, col = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
window <- 24
port_rolling_sd_tbl <- portfolio_returns_rebalanced_monthly_tbl %>%
tq_mutate(select = returns,
mutate_fun = rollapply,
width = window,
FUN = sd,
col_rename = "rolling_sd") %>%
select(date, rolling_sd) %>%
na.omit()
port_rolling_sd_tbl %>%
ggplot(aes(date, rolling_sd)) +
geom_line(color = "cornflowerblue") +
scale_y_continuous(labels = scales::percent) +
scale_x_date(breaks = scales::breaks_pretty(n = 7))+
labs(title = "24-Month Rolling Volatility",
x = NULL,
y = NULL) +
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 should expect my portfolio to perform poorly, as it has decreased every year since 2013. I would still invest in the portfolio itself, however, I would change the weight of my distribution on which stocks I invest more into. The risk is very great due to it’s poor perfomance over time, yet I expect it to bounce back in the long run.