# 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("UAL", "AAL", "LUV", "DAL")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",
                 from = "2012-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] "AAL" "DAL" "LUV" "UAL"
# weights
weights <- c(0.2, 0.1, 0.4, 0.3)
weights
## [1] 0.2 0.1 0.4 0.3
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAL         0.2
## 2 DAL         0.1
## 3 LUV         0.4
## 4 UAL         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: 150 × 2
##    date       returns
##    <date>       <dbl>
##  1 2013-01-31  0.0729
##  2 2013-02-28  0.0378
##  3 2013-03-28  0.173 
##  4 2013-04-30  0.0121
##  5 2013-05-31  0.0277
##  6 2013-06-28 -0.0575
##  7 2013-07-31  0.106 
##  8 2013-08-30 -0.133 
##  9 2013-09-30  0.124 
## 10 2013-10-31  0.138 
## # ℹ 140 more rows

5 Compute kurtosis

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

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

6 Plot: Rolling kurtosis

window = 24


rolling_kurt_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(select     = returns,
              mutate_fun = rollapply,
              width      = window,
              FUN        = kurtosis,
              col_rename = "kurt") %>%
    
    na.omit() %>%
    select(-returns)


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

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.

This graph indicates a sharp spike in downside risk from 2020 to 2022, peaking above 3.0. There is a notable drop in downside risk, particularly around Q2 of 2022 from 3.08 to 0.41, sustained until Q3 2022 when it went back up to 1.37 and higher. Downside risk has decreased quickly from Q3 2022 until now, with it somewhat stabilizing (comparative to the volatile 2020-2023 period) in 2024 and onward. From this time period onward, along with any time period prior to 2020 but after 2015, rolling kurtosis seems to be negative, moving between -0.1 and -1.0.

This data indicates that the distribution of returns is fairly normal from 2015-2020, and 2024-present. Extreme returns from the 2020-2024 timeframe can be supported by the COVID-19 crisis and the fallout from the pandemic. Since each of the stocks in this portfolio are airliners, it can be inferred that the increased volatility was mostly due to the strain the pandemic was putting on airlines at the time.

From an investing perspective, this indicates that investing now would be investing in a more stable risk environment, but it is hard to tell completely due to the lack of time that has passed since the incredibly volatile period.