# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(plotly)

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("FNV", "WPM", "NEM", "CCJ", "BWXT", "NVDA", "TSM", "ASML", "CAT", "ITW", "HON", "PLD", "EQIX", "PSA", "TPL", "XOM", "CVX")

prices <- tq_get(x = symbols, 
                 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() %>%
    
    set_names(c("asset", "date", "returns"))

asset_returns_tbl
## # A tibble: 1,020 Ă— 3
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 ASML  2013-01-31  0.154 
##  2 ASML  2013-02-28 -0.0563
##  3 ASML  2013-03-28 -0.0430
##  4 ASML  2013-04-30  0.0992
##  5 ASML  2013-05-31  0.0887
##  6 ASML  2013-06-28 -0.0269
##  7 ASML  2013-07-31  0.128 
##  8 ASML  2013-08-30 -0.0327
##  9 ASML  2013-09-30  0.126 
## 10 ASML  2013-10-31 -0.0420
## # ℹ 1,010 more rows

3 Assign a Weight to Each Asset (Change the Weighting Scheme)

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
##  [1] "ASML" "BWXT" "CAT"  "CCJ"  "CVX"  "EQIX" "FNV"  "HON"  "ITW"  "NEM" 
## [11] "NVDA" "PLD"  "PSA"  "TPL"  "TSM"  "WPM"  "XOM"
weights <- c(0.10, 0.10, 0.10, 0.05, 0.05, 0.04, 0.03, 0.03, 0.04, 0.03, 0.03, 0.07, 0.07, 0.06, 0.07, 0.07, 0.06)
weights
##  [1] 0.10 0.10 0.10 0.05 0.05 0.04 0.03 0.03 0.04 0.03 0.03 0.07 0.07 0.06 0.07
## [16] 0.07 0.06
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 17 Ă— 2
##    symbols weights
##    <chr>     <dbl>
##  1 ASML       0.1 
##  2 BWXT       0.1 
##  3 CAT        0.1 
##  4 CCJ        0.05
##  5 CVX        0.05
##  6 EQIX       0.04
##  7 FNV        0.03
##  8 HON        0.03
##  9 ITW        0.04
## 10 NEM        0.03
## 11 NVDA       0.03
## 12 PLD        0.07
## 13 PSA        0.07
## 14 TPL        0.06
## 15 TSM        0.07
## 16 WPM        0.07
## 17 XOM        0.06

4 Build a 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 2013-01-31  0.0457 
##  2 2013-02-28 -0.0170 
##  3 2013-03-28  0.0106 
##  4 2013-04-30 -0.00468
##  5 2013-05-31  0.0276 
##  6 2013-06-28 -0.0378 
##  7 2013-07-31  0.0389 
##  8 2013-08-30 -0.00842
##  9 2013-09-30  0.0294 
## 10 2013-10-31  0.00176
## # ℹ 50 more rows

5 Compute Skewness

portfoltio_skew_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
    
    tq_performance(Ra = returns, 
                   performance_fun = table.Stats) %>%
    
    select(Skewness)

portfoltio_skew_tidyquant_builtin_percent
## # A tibble: 1 Ă— 1
##   Skewness
##      <dbl>
## 1     0.17

6 Plot: Skewness Comparison

# Data Transformation: Calculate Skewness
asset_skewness_tbl <- asset_returns_tbl %>%
    
    group_by(asset) %>%
    summarize(skew = skewness(returns)) %>%
    ungroup() %>%

    # Add Portfolio Skewness
    add_row(tibble(asset = "Portfolio",
                   skew  = skewness(portfolio_returns_tbl$returns)))

asset_skewness_tbl
## # A tibble: 18 Ă— 2
##    asset         skew
##    <chr>        <dbl>
##  1 ASML       0.115  
##  2 BWXT      -0.276  
##  3 CAT       -0.248  
##  4 CCJ        0.504  
##  5 CVX        0.0278 
##  6 EQIX      -0.229  
##  7 FNV        0.706  
##  8 HON        0.168  
##  9 ITW        0.157  
## 10 NEM        0.156  
## 11 NVDA       0.899  
## 12 PLD        0.0293 
## 13 PSA       -0.0989 
## 14 TPL        0.281  
## 15 TSM       -0.174  
## 16 WPM        0.152  
## 17 XOM       -0.00356
## 18 Portfolio  0.170
# 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") +
    
    theme(
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_text(margin = margin(t = 10)))

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 Apply 4.

No, none of my assets would return an extreme positive return. The top two assets that are more likely to produce a moderate positive return are FNV and NVDA. I can say this confidently for two reasons: 1. They have a skewness greater than 0.5 but below 1 and 2. Looking back at my Apply 4 histograms, NVDA has a couple massive positive returns and greater distribution of positive returns than of negative returns. FNV doesn’t have any similar outliers according to the past histogram, so I would need to research further into how it has such a large positive skewness against my other assets, even higher than CCJ (though it does also have a greater distribution of positive returns).