# 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

symbols <- c("WMT", "TGT", "COST")

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: 180 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 COST  2013-01-31  0.0359 
##  2 COST  2013-02-28 -0.00765
##  3 COST  2013-03-28  0.0465 
##  4 COST  2013-04-30  0.0216 
##  5 COST  2013-05-31  0.0138 
##  6 COST  2013-06-28  0.00854
##  7 COST  2013-07-31  0.0601 
##  8 COST  2013-08-30 -0.0458 
##  9 COST  2013-09-30  0.0291 
## 10 COST  2013-10-31  0.0243 
## # ℹ 170 more rows

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "TGT"  "WMT"
weights <- c(0.35, 0.3, 0.25)
weights
## [1] 0.35 0.30 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 COST       0.35
## 2 TGT        0.3 
## 3 WMT        0.25

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>% 

    tq_portfolio(assets_col   = asset, 
                 returns_col = returns,
                 weights     = w_tbl, 
                 rebalance_on = "months" )

portfolio_returns_tbl
## # A tibble: 60 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-01-31          0.0250  
##  2 2013-02-28          0.0144  
##  3 2013-03-28          0.0569  
##  4 2013-04-30          0.0262  
##  5 2013-05-31         -0.00611 
##  6 2013-06-28         -0.000959
##  7 2013-07-31          0.0426  
##  8 2013-08-30         -0.0645  
##  9 2013-09-30          0.0167  
## 10 2013-10-31          0.0215  
## # ℹ 50 more rows

5 Compute Skewness

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

portfolio_skew_tidyquant_builtin_percent
## # A tibble: 1 × 1
##   Skewness
##      <dbl>
## 1   0.0347

6 Plot: Skewness Comparison

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

asset_skewness_tbl
## # A tibble: 4 × 2
##   asset        skew
##   <chr>       <dbl>
## 1 COST      -0.244 
## 2 TGT        0.148 
## 3 WMT        0.0723
## 4 Portfolio  0.0347
# 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.

As you stated in the zoom video, “skewness alone does not tell us whether a large gain is likely. It only tells us if we have a long tail from the right or the left from the center.” This is exactly why I am choosing to look at both this skewness plot and the rate of returns from code along 4. Within the skewness plot, it is clear that both Target and Walmart have a positive skew above the portfolio, while Costco has a negative skew being below -0.2. At first glace, I would say that Target would be the asset that is most likely to have an extreme positive return. I then pushed my focus to code along 4 where I see that Walmart has a tail pulling both positive and negative. Taking this into consideration, since target doesn’t have a tail from the middle and instead had data located mainly in the same area, I am choosing to stick with Target from being the most likely one to return extreme positive. Target has a leg up from Walmart being the highest positive skewness, but since Walmart has a double tail I don’t think it would be able to surpass Target, therefore I feel target is the safest asset to pick at this time.