# Load packages

# Core
library(tidyverse)
library(tidyquant)

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

# Choose stocks
symbols <- c("CRWD", "AMZN", "SHOP","TTD", "NVDA")

prices <- tq_get(x = symbols, 
                 get  = "stock.prices", 
                 from = "2022-01-01")

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"))

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "CRWD" "NVDA" "SHOP" "TTD"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)

w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 Ă— 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.25
## 2 CRWD       0.25
## 3 NVDA       0.2 
## 4 SHOP       0.2 
## 5 TTD        0.1

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: 33 Ă— 2
##    date       returns
##    <date>       <dbl>
##  1 2022-02-28 -0.0201
##  2 2022-03-31  0.0490
##  3 2022-04-29 -0.286 
##  4 2022-05-31 -0.0995
##  5 2022-06-30 -0.118 
##  6 2022-07-29  0.146 
##  7 2022-08-31 -0.0400
##  8 2022-09-30 -0.135 
##  9 2022-10-31  0.0275
## 10 2022-11-30 -0.0149
## # ℹ 23 more rows

5 Compute Skewness

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

portfolio_skew_tidyquant_builtin_percent
## # A tibble: 1 Ă— 1
##   Skewness
##      <dbl>
## 1   -0.456

6 Plot: Skewness Comparison

# Data transformation: calculate skewness
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: 6 Ă— 2
##   asset        skew
##   <chr>       <dbl>
## 1 AMZN      -0.173 
## 2 CRWD      -1.04  
## 3 NVDA      -0.673 
## 4 SHOP       0.0383
## 5 TTD        0.161 
## 6 Portfolio -0.456
# 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.

TTD has a skewness close to 0, while SHOP is right behind it with a skewness of .0364. These both have bell shaped curves due to their skewness being close to zero so they have a symetrical distribution of returns. The rest of the stocks are negative skewness. These stocks aren’t guaranteed to give you smaller returns but they are more likely to give you smaller, or negative returns These stocks would have a tail to the left.