# Load packages

# Core
library(tidyverse)
library(tidyquant)

1 Import stock prices

# Load packages

# Core
library(tidyverse)
library(tidyquant)

1 Import stock prices

symbols <- c("LME", "NOC", "LOC", "UPS", "UNH")

prices <- tq_get(x    = symbols, 
                 get  = "stock.prices",
                 from = "2012-12-31",
                 to   = "2017-12-31")

2 Convert prices to returns

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "LME" "LOC" "NOC" "UNH" "UPS"
# weights
weights <- c(0.15, 0.15, 0.2, 0.2, 0.3)
weights
## [1] 0.15 0.15 0.20 0.20 0.30
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 LME        0.15
## 2 LOC        0.15
## 3 NOC        0.2 
## 4 UNH        0.2 
## 5 UPS        0.3

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: 76 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0682 
##  2 2013-02-28 -0.0113 
##  3 2013-03-28  0.0118 
##  4 2013-04-30  0.0518 
##  5 2013-05-31 -0.0314 
##  6 2013-06-28  0.0463 
##  7 2013-07-08  0      
##  8 2013-07-31  0.132  
##  9 2013-08-30 -0.0411 
## 10 2013-09-30  0.00328
## # ℹ 66 more rows

6 Plot

Scatterplot of 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 LME        0.728
## 2 LOC        0.458
## 3 NOC        0.108
## 4 UNH       -0.261
## 5 UPS       -0.628
## 6 portfolio  0.635
# Plot Skewness
asset_skewness_tbl %>%
    
    ggplot(aes(x = asset, y = skew, color = asset)) +
    geom_point() +
    
    ggrepel::geom_text_repel(aes(label = asset)) + 
                             
                                 
    labs(y = "skewness") + theme(legend.position = "none")

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.

LME is the only individual stock in my portfolio with a higher positive skewness, this would not though provide higher returns as a higher positive skewness refers to many values of smaller returns skewing the results, though UPS and UNH can be seen at the other end, showing more consistent returns, skewing the data to the left, NOC shows a more wide spread in the frequency of returns, as that’s what skewness measure, my portfolio is skewed towards positive because I weighted the stocks with greater positive skewness heavier than the others.