# 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

symbols <- c("NKE", "TSLA", "NVDA", "GOOGL", "ADDYY")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",
                 from = "2025-12-31",
                 to   = "2026-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() %>%

set_names(c("asset", "date", "return"))

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "ADDYY" "GOOGL" "NKE"   "NVDA"  "TSLA"
#weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)

4 Build a portfolio

?tg_portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col = asset,
                 returns_col = return,
                 weights     = w_tbl,
                 rebalace_on = "months")

portfolio_returns_tbl
## # A tibble: 10 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2026-01-30          -0.0144 
##  2 2026-02-27          -0.0310 
##  3 2026-03-31          -0.100  
##  4 2026-04-30           0.0931 
##  5 2026-05-29           0.0575 
##  6 2026-06-30          -0.0358 
##  7 2026-07-31          -0.0551 
##  8 2026-08-31          -0.00268
##  9 2026-09-30          -0.0174 
## 10 2026-10-28           0.00102

5 Compute Standard Deviation

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 * 100
##   Stdev tq_sd
## 1  5.45  5.45
# Mean of portfolio returns
portfolio_sd_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_sd_tidyquant_builtin_percent
## [1] -0.01049805
# Calculate asset means and standard deviations, then combine with portfolio
asset_means <- asset_returns_tbl %>%
  group_by(asset) %>%
  summarise(Mean = mean(return))

asset_sds <- asset_returns_tbl %>%
  group_by(asset) %>%
  summarise(Stdev = sd(return))

asset_summary <- left_join(asset_means, asset_sds, by = "asset")

portfolio_summary <- tibble(
  asset = "Portfolio",
  Mean = mean(portfolio_returns_tbl$portfolio.returns),
  Stdev = sd(portfolio_returns_tbl$portfolio.returns)
)

sd_mean_tbl <- bind_rows(asset_summary, portfolio_summary)

6 Plot: Expected Returns versus Risk

# Plot Expected Returns vs Risk
sd_mean_tbl %>%
  ggplot(aes(x = Stdev, y = Mean, label = asset)) +
  geom_point(aes(color = asset == "Portfolio"), size = 3) +
  geom_text(vjust = -0.7, check_overlap = TRUE) +
  scale_color_manual(values = c("FALSE" = "black", "TRUE" = "red"), guide = "none") +
  labs(
    title = "Expected Returns vs. Risk (Standard Deviation)",
    x = "Risk (Standard Deviation %)",
    y = "Expected Return (Mean %)"
  ) +
  theme_minimal()

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.
Based on the plot, the portfolio achieves a lower standard deviation (risk of 0.0545) than every individual asset (ranging from 0.0673 to 0.136), successfully mitigating extreme volatility through diversification. While its expected mean return (-0.0105) is moderate compared to top individual performers like NVDA, it avoids the deep negative returns of stocks like NKE and TSLA. Therefore, I would not invest all my money in a single stock, as single assets expose an investor to much higher, unprotected company-specific risk on the right side of the chart.