# 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("AMZN", "MSFT", "HD", "WMT")

prices <- tq_get(x    = symbols, 
                 get  = "stock.prices", 
                 from = "2012-12-31",
                 to   = "2017-12-31")
prices
## # A tibble: 5,040 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 AMZN   2012-12-31  12.2  12.6  12.1  12.5 68380000     12.5
##  2 AMZN   2013-01-02  12.8  12.9  12.7  12.9 65420000     12.9
##  3 AMZN   2013-01-03  12.9  13.0  12.8  12.9 55018000     12.9
##  4 AMZN   2013-01-04  12.9  13.0  12.8  13.0 37484000     13.0
##  5 AMZN   2013-01-07  13.1  13.5  13.1  13.4 98200000     13.4
##  6 AMZN   2013-01-08  13.4  13.4  13.2  13.3 60214000     13.3
##  7 AMZN   2013-01-09  13.4  13.5  13.3  13.3 45312000     13.3
##  8 AMZN   2013-01-10  13.4  13.4  13.1  13.3 57268000     13.3
##  9 AMZN   2013-01-11  13.3  13.4  13.2  13.4 48266000     13.4
## 10 AMZN   2013-01-14  13.4  13.7  13.4  13.6 85500000     13.6
## # … with 5,030 more rows

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>% 
    
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "quarterly", 
                 type       = "log") %>%
    
    slice(-1) %>%
    
    ungroup() %>%
    
    set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 80 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AMZN  2013-03-28  0.0604 
##  2 AMZN  2013-06-28  0.0412 
##  3 AMZN  2013-09-30  0.119  
##  4 AMZN  2013-12-31  0.243  
##  5 AMZN  2014-03-31 -0.170  
##  6 AMZN  2014-06-30 -0.0351 
##  7 AMZN  2014-09-30 -0.00723
##  8 AMZN  2014-12-31 -0.0382 
##  9 AMZN  2015-03-31  0.181  
## 10 AMZN  2015-06-30  0.154  
## # … with 70 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull() 
symbols
## [1] "AMZN" "HD"   "MSFT" "WMT"
# weights
weights <- c(0.30, 0.30, 0.15, 0.25)
weights
## [1] 0.30 0.30 0.15 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.3 
## 2 HD         0.3 
## 3 MSFT       0.15
## 4 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",
                 col_rename = "returns")

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-03-28  0.0922 
##  2 2013-06-28  0.0749 
##  3 2013-09-30  0.0260 
##  4 2013-12-31  0.135  
##  5 2014-03-31 -0.0521 
##  6 2014-06-30 -0.00123
##  7 2014-09-30  0.0599 
##  8 2014-12-31  0.0620 
##  9 2015-03-31  0.0515 
## 10 2015-06-30  0.0191 
## 11 2015-09-30  0.0431 
## 12 2015-12-31  0.148  
## 13 2016-03-31 -0.00465
## 14 2016-06-30  0.0518 
## 15 2016-09-30  0.0683 
## 16 2016-12-30 -0.0156 
## 17 2017-03-31  0.101  
## 18 2017-06-30  0.0628 
## 19 2017-09-29  0.0409 
## 20 2017-12-29  0.186

5 Compute Skewness

# 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: 5 × 2
##   asset       skew
##   <chr>      <dbl>
## 1 AMZN      -0.387
## 2 HD        -0.130
## 3 MSFT      -0.248
## 4 WMT        0.480
## 5 portfolio  0.353
# 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") 

6 Plot: Skewness Comparison

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.

Walmart is more likely to return extreme positive returns than my portfolio collectively. Amazon, Home Depot, and Microsoft have extreme negative returns and my portfolio has a positive return, but Walmart has the highest return with its skewness at 0.480.