# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize and examine changes in the underlying trend in the downside risk of your portfolio in terms of kurtosis.

Choose your stocks.

from 2012-12-31 to present

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 kurtosis

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

portfolio_kurt_tidyquant_builtin_percent
## # A tibble: 1 × 1
##   Kurtosis
##      <dbl>
## 1    0.404

6 Plot: Rolling kurtosis

window = 24

# Transform Data: calculate 24-Month rolling kurtosis
rolling_kurt_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(select     = portfolio.returns, 
              mutate_fun = rollapply, 
              width      = window, 
              FUN        = kurtosis, 
              col_rename = "kurt") %>%
    
    na.omit() %>%
    select(-portfolio.returns)

# Plot 
rolling_kurt_tbl %>%
    
    ggplot(aes(x = date, y = kurt)) +
    geom_line(color = "cornflowerblue") +
    
    # Formatting 
    scale_y_continuous(breaks = seq(-1, 4, 0.5)) +
    scale_x_date(breaks = scales::pretty_breaks(n = 7)) + 
    theme(plot.title = element_text(hjust = 0.5)) +
    
    # Labeling
    labs(x = NULL,
         y = "Kurtosis",
         title = paste0("Rolling ", window, " Month Kurtosis")) +
    
    annotate(geom = "text", 
             x = as.Date("2016-07-01"), y = 3,
             size = 5, color = "red",
             label = str_glue("Downside risk dropped towards the end of 2016"))

Has the downside risk of your portfolio increased or decreased over time? Explain using the plot you created. You may also refer to the skewness of the returns distribution you plotted in the previous assignment.

Ovetime, the downside risk of the portfolio has decreased drastically. At the beginning of 2015 the kurtosis was around 1.25, which grew to 3.15 at the beginning of 2016, only to drop to -1.15 towards the end of 2016. Although we can see it starting to make its way back up to positive kurtosise around 2018 where it is settled at -0.75, it's still not quiet positive. As a whole, the downside risk has decreased overtime.