# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

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("SPY", "EFA", "IJS", "EEM", "AGG")

prices <- tq_get(x = symbol,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2017-12-31")

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>%
    
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 type = "log") %>%
    slice(-1) %>%
    
    ungroup()

3 Assign a weight to each asset (change the weigting scheme)

symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
symbols
## [1] "AGG" "EEM" "EFA" "IJS" "SPY"
weight <- c(0.25,0.25,0.2,0.2,0.1)
weight
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weight)

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    tq_portfolio(assets_col = symbol,
                 returns_col = monthly.returns,
                 weights = w_tbl,
                 rebalance_on = "months")

5 Compute Standard Deviation

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)

6 Plot: Expected Returns versus Risk

sd_mean_tbl <- asset_returns_tbl %>%
    group_by(symbol) %>%
    tq_performance(Ra = monthly.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.
Instead of investing all of my money in portfolio I would divy it between SPY and EFA. While EFA is slightly more risky it does yeild a higher output. SPY is the obvious choice as it is slightly more risky than portfolio but the returns heavily outweigh portfolio. My ideal stock for risk vs reward would be SPY in the top left quadrant as that would be the highet return with the least risk.