# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(scales)
library(ggrepel)
library(scales)

Goal

Visualize and compare skewness of your portfolio and its assets.

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <-c("INTL", "NVDA", "MSFT", "AMD")
prices <- tq_get(x    = symbols, 
                 get  = "stock.prices",
                 from = "2018-12-31",
                 to   = "2023-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", "returns"))

asset_returns_tbl
## # A tibble: 192 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AMD   2019-01-31  0.279  
##  2 AMD   2019-02-28 -0.0367 
##  3 AMD   2019-03-29  0.0812 
##  4 AMD   2019-04-30  0.0794 
##  5 AMD   2019-05-31 -0.00799
##  6 AMD   2019-06-28  0.103  
##  7 AMD   2019-07-31  0.00263
##  8 AMD   2019-08-30  0.0323 
##  9 AMD   2019-09-30 -0.0814 
## 10 AMD   2019-10-31  0.157  
## # ℹ 182 more rows

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMD"  "INTL" "MSFT" "NVDA"
# weights
weights <- c(0.25, 0.25, 0.25, 0.25)
weights 
## [1] 0.25 0.25 0.25 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMD        0.25
## 2 INTL       0.25
## 3 MSFT       0.25
## 4 NVDA       0.25

4 Build a portfolio

# ?tq_portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset, 
                 returns_col  = returns,
                 weights      = w_tbl,
                 rebalance_on = "months", 
                 col_rename   = "returns")

portfolio_returns_tbl
## # A tibble: 60 × 2
##    date        returns
##    <date>        <dbl>
##  1 2019-01-31  0.0953 
##  2 2019-02-28  0.0273 
##  3 2019-03-29  0.0711 
##  4 2019-04-30  0.0473 
##  5 2019-05-31 -0.0869 
##  6 2019-06-28  0.0937 
##  7 2019-07-31  0.0117 
##  8 2019-08-30  0.0103 
##  9 2019-09-30 -0.00865
## 10 2019-10-31  0.0830 
## # ℹ 50 more rows

5 Compute Skewness

portfolia_skew_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
    
    tq_performance(Ra = returns, 
                   performance_fun = table.Stats) %>%
    select(Skewness)
   
portfolia_skew_tidyquant_builtin_percent
## # A tibble: 1 × 1
##   Skewness
##      <dbl>
## 1   -0.516
# Mean of portfolio returns
porfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

porfolio_mean_tidyquant_builtin_percent
## [1] NA

6 Plot: Skewness Comparison

asset_skewness_tbl <- asset_returns_tbl %>%
    
    group_by(asset) %>%
    summarise(skew = skewness(returns)) %>%
    ungroup() %>%
    
    # Add portfolio skewness
    add_row(tibble(asset = "Portfolio",
                   skew  = skewness(portfolio_returns_tbl$returns)))

asset_skewness_tbl 
## # A tibble: 5 × 2
##   asset        skew
##   <chr>       <dbl>
## 1 AMD       -0.0111
## 2 INTL       0.0710
## 3 MSFT      -0.0768
## 4 NVDA      -0.706 
## 5 Portfolio -0.516
# Plot skewness
asset_skewness_tbl %>%
    
    ggplot(aes(x = asset, y = skew, color = asset)) +
    geom_point() +
    
    ggrepel::geom_text_repel(aes(label = asset), 
                             data = asset_skewness_tbl %>%
                                 filter(asset == "Portfolio")) +
    
    labs(y = "skewness") 

Is any asset in your portfolio more likely to return extreme positive returns than your portfolio collectively? Discuss in terms of skewness. You may also refer to the distribution of returns you plotted in Code along 4.

When analyzing the skewness of individual assets and the overall portfolio reveals a tendency toward negative returns. AMD and INTEL exhibit slight negative skewness, indicating a minor risk of negative outcomes. MSFT shows a bit more pronounced negative skewness, while NVIDIA stands out with the most significant negative skewness, suggesting a higher chance of substantial negative returns.

I would say that the portfolio’s overall skewness, is slightly negative, mirrors the trends seen in its components, indicating a general risk of negative returns. In conclusion I think no individual asset shows any real chance for extreme positive returns that exceeds the overall portfolio, especially not NVIDIA, which poses the highest risk for negative returns.