# 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

# Choose stocks


symbols <- c("MELI", "TTD", "AFL", "SHOP", "NVDA")

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 %>%
    # Calculate monthly returns
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 type = "log") %>%
    slice(-1) %>%
    ungroup() %>%
    # remane
set_names(c("asset", "date", "returns"))

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
w <- c(0.35,
       0.15,
       0.20,
       0.10,
       0.20)
w_tbl <- tibble(symbols, w)

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset,
                 returns_col  = returns,
                 weights      = w_tbl,
                 col_rename   = "returns",
                 rebalance_on = "months")
portfolio_returns_tbl
## # A tibble: 60 × 2
##    date         returns
##    <date>         <dbl>
##  1 2013-01-31  0.0173  
##  2 2013-02-28 -0.0158  
##  3 2013-03-28  0.0352  
##  4 2013-04-30  0.0362  
##  5 2013-05-31  0.0407  
##  6 2013-06-28 -0.000141
##  7 2013-07-31  0.0393  
##  8 2013-08-30 -0.0139  
##  9 2013-09-30  0.0546  
## 10 2013-10-31  0.0114  
## # … with 50 more rows

5 Compute kurtosis

portfolio_kurt_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
    

    tq_performance(Ra = returns,
                   performance_fun = table.Stats) %>%
    
    select(Kurtosis) 

6 Plot: Rolling kurtosis

window <- 24
port_rolling_kurtosis_tbl <- portfolio_returns_tbl %>%
    tq_mutate(select = returns,
              mutate_fun = rollapply,
              width      = window,
              FUN        = kurtosis,
              col_rename = "rolling_kurtosis") %>%
    select(date, rolling_kurtosis) %>%
    na.omit()

port_rolling_kurtosis_tbl %>%
    ggplot(aes(date, rolling_kurtosis)) +
    geom_line(color = "cornflowerblue") +
    scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
    scale_x_date(breaks = scales::breaks_pretty(n = 7)) +
    labs(title = paste0("Rolling ", window, "-Month Kurtosis"),
         x = NULL,
         y = "kurtosis") +
    theme(plot.title = element_text(hjust = 0.5)) +
    annotate(geom = "text",
             x = as.Date("2016-12-01"), y = 3,
             color = "red", size = 5,
             label = str_glue("The risk level has increased at the end of the period
                              with the 24-month kurtosis rising to exactly 1.5."))

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.

The overall skewness of my portfolio is moderately positive with a longer tail to the right so as the kurtosis increases positive returns are likely to increase meaning my portfolio has minimal downside risk.