# 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", "LOW", "GOOG", "AMZN")

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

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "GOOG" "LOW"  "TGT"  "WMT"
# 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)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.25
## 2 GOOG       0.25
## 3 LOW        0.2 
## 4 TGT        0.2 
## 5 WMT        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: 60 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0527 
##  2 2013-02-28  0.0238 
##  3 2013-03-28  0.0216 
##  4 2013-04-30  0.0105 
##  5 2013-05-31  0.0416 
##  6 2013-06-28  0.00222
##  7 2013-07-31  0.0518 
##  8 2013-08-30 -0.0520 
##  9 2013-09-30  0.0462 
## 10 2013-10-31  0.0946 
## # ℹ 50 more rows

5 Compute Skewness

# Asset Skewness
asset_skewness_tbl <- asset_returns_tbl %>%

    group_by(asset) %>%
       tq_performance(Ra = returns,
                   performance_fun = table.Stats) %>%
    
        ungroup() %>%
    select(asset, Skewness)
       
        asset_skewness_tbl 
## # A tibble: 5 × 2
##   asset Skewness
##   <chr>    <dbl>
## 1 AMZN    0.187 
## 2 GOOG    0.784 
## 3 LOW    -0.0308
## 4 TGT     0.148 
## 5 WMT     0.0723
# Portfolio Skewness
    portfolio_skewness_tbl <- portfolio_returns_tbl %>%
 tq_performance(Ra = returns, 
                  performance_fun = table.Stats) %>%
    
        select(Skewness) %>%
        rename(skew= Skewness) %>%
    mutate(asset = "Portfolio")
        
   portfolio_skewness_tbl      
## # A tibble: 1 × 2
##    skew asset    
##   <dbl> <chr>    
## 1 0.233 Portfolio

6 Plot: Skewness Comparison

# Compare Skewness

    ggplot(data = asset_skewness_tbl,
        aes(x = asset, y = Skewness, color = asset)) +
    geom_point(size = 2) +
     ggrepel:: geom_text_repel(aes(label = asset)) +
                             geom_point (data = asset_skewness_tbl, aes(x = asset, y = Skewness), color = "cornflowerblue", size = 2) +
    labs(title = "Portfolio Skewness",
        y = "Skewness",
         x = "asset") 

ggplot(data = asset_returns_tbl,
       aes(x = returns)) +
    geom_histogram(binwidth = 0.02,
                   fill = "cornflowerblue",
                   color = "white") +
    facet_wrap(~ asset) +
    labs(title = "Distribution of Monthly Asset Returns",
         x = "Monthly Log Returns",
         y = "Frequency") 

Based off of the portfolio, LOW has the lowest skew at -0.031, then WMT at 0.072, followed by TGT at 0.148, then AMZN at 0.187. GOOG is the most likely out of each of the five stocks to experience a positive return and the highest skewness. This comes from a positive skew of 0.784, with an overall portfolio skew of 0.233. All 4 stocks are skewed less positively than the portfolio as a whole. GOOG has the highest skew and distribution will move right, leading to the possibility of higher positive returns. As LOW has the only negative skew, this could lead to distribution going left on the chart. The histograms shows GOOG with the highest positive skewness. AMZN and TGT show mostly positive right distribution, as WMT follows with less. LOW shows a left distribution.